Add support to enforce inlining of all subschemas instead of using references. (#44)
This is needed to support use cases like openAPIV3Schema in Kubernetes CustomResourceDefinitions. Co-authored-by: alex.berger@nexiot.ch <alex.berger@nexiot.ch>
This commit is contained in:
parent
40d9bfd517
commit
364d0e0192
3 changed files with 52 additions and 1 deletions
|
@ -39,6 +39,10 @@ pub struct SchemaSettings {
|
||||||
pub meta_schema: Option<String>,
|
pub meta_schema: Option<String>,
|
||||||
/// A list of visitors that get applied to all generated root schemas.
|
/// A list of visitors that get applied to all generated root schemas.
|
||||||
pub visitors: Vec<Box<dyn GenVisitor>>,
|
pub visitors: Vec<Box<dyn GenVisitor>>,
|
||||||
|
/// Inline all subschemas instead of using references.
|
||||||
|
///
|
||||||
|
/// Defaults to `false`.
|
||||||
|
pub inline_subschemas: bool,
|
||||||
_hidden: (),
|
_hidden: (),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,6 +61,7 @@ impl SchemaSettings {
|
||||||
definitions_path: "#/definitions/".to_owned(),
|
definitions_path: "#/definitions/".to_owned(),
|
||||||
meta_schema: Some("http://json-schema.org/draft-07/schema#".to_owned()),
|
meta_schema: Some("http://json-schema.org/draft-07/schema#".to_owned()),
|
||||||
visitors: vec![Box::new(RemoveRefSiblings)],
|
visitors: vec![Box::new(RemoveRefSiblings)],
|
||||||
|
inline_subschemas: false,
|
||||||
_hidden: (),
|
_hidden: (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -69,6 +74,7 @@ impl SchemaSettings {
|
||||||
definitions_path: "#/definitions/".to_owned(),
|
definitions_path: "#/definitions/".to_owned(),
|
||||||
meta_schema: Some("https://json-schema.org/draft/2019-09/schema".to_owned()),
|
meta_schema: Some("https://json-schema.org/draft/2019-09/schema".to_owned()),
|
||||||
visitors: Vec::default(),
|
visitors: Vec::default(),
|
||||||
|
inline_subschemas: false,
|
||||||
_hidden: (),
|
_hidden: (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -92,6 +98,7 @@ impl SchemaSettings {
|
||||||
retain_examples: false,
|
retain_examples: false,
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
|
inline_subschemas: false,
|
||||||
_hidden: (),
|
_hidden: (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -196,7 +203,7 @@ impl SchemaGenerator {
|
||||||
/// If `T`'s schema depends on any [referenceable](JsonSchema::is_referenceable) schemas, then this method will
|
/// If `T`'s schema depends on any [referenceable](JsonSchema::is_referenceable) schemas, then this method will
|
||||||
/// add them to the `SchemaGenerator`'s schema definitions.
|
/// add them to the `SchemaGenerator`'s schema definitions.
|
||||||
pub fn subschema_for<T: ?Sized + JsonSchema>(&mut self) -> Schema {
|
pub fn subschema_for<T: ?Sized + JsonSchema>(&mut self) -> Schema {
|
||||||
if !T::is_referenceable() {
|
if !T::is_referenceable() || self.settings.inline_subschemas {
|
||||||
return T::json_schema(self);
|
return T::json_schema(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
23
schemars/tests/expected/inline-subschemas.json
Normal file
23
schemars/tests/expected/inline-subschemas.json
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
{
|
||||||
|
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
|
||||||
|
"properties": {
|
||||||
|
"spec": {
|
||||||
|
"properties": {
|
||||||
|
"replicas": {
|
||||||
|
"format": "uint32",
|
||||||
|
"minimum": 0,
|
||||||
|
"type": "integer"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"replicas"
|
||||||
|
],
|
||||||
|
"type": "object"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"spec"
|
||||||
|
],
|
||||||
|
"title": "MyJob",
|
||||||
|
"type": "object"
|
||||||
|
}
|
21
schemars/tests/inline_subschemas.rs
Normal file
21
schemars/tests/inline_subschemas.rs
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
mod util;
|
||||||
|
use schemars::gen::SchemaSettings;
|
||||||
|
use schemars::JsonSchema;
|
||||||
|
use util::*;
|
||||||
|
|
||||||
|
#[derive(Debug, JsonSchema)]
|
||||||
|
pub struct MyJob {
|
||||||
|
pub spec: MyJobSpec,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, JsonSchema)]
|
||||||
|
pub struct MyJobSpec {
|
||||||
|
pub replicas: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn struct_normal() -> TestResult {
|
||||||
|
let mut settings = SchemaSettings::openapi3();
|
||||||
|
settings.inline_subschemas = true;
|
||||||
|
test_generated_schema::<MyJob>("inline-subschemas", settings)
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue