Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to enforce inlining of all subschemas instead of using references #44

Merged
merged 2 commits into from
Sep 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion schemars/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ pub struct SchemaSettings {
pub meta_schema: Option<String>,
/// A list of visitors that get applied to all generated root schemas.
pub visitors: Vec<Box<dyn GenVisitor>>,
/// Inline all subschemas instead of using references.
///
/// Defaults to `false`.
pub inline_subschemas: bool,
_hidden: (),
}

Expand All @@ -57,6 +61,7 @@ impl SchemaSettings {
definitions_path: "#/definitions/".to_owned(),
meta_schema: Some("http://json-schema.org/draft-07/schema#".to_owned()),
visitors: vec![Box::new(RemoveRefSiblings)],
inline_subschemas: false,
_hidden: (),
}
}
Expand All @@ -69,6 +74,7 @@ impl SchemaSettings {
definitions_path: "#/definitions/".to_owned(),
meta_schema: Some("https://json-schema.org/draft/2019-09/schema".to_owned()),
visitors: Vec::default(),
inline_subschemas: false,
_hidden: (),
}
}
Expand All @@ -92,6 +98,7 @@ impl SchemaSettings {
retain_examples: false,
}),
],
inline_subschemas: false,
_hidden: (),
}
}
Expand Down Expand Up @@ -196,7 +203,7 @@ impl SchemaGenerator {
/// If `T`'s schema depends on any [referenceable](JsonSchema::is_referenceable) schemas, then this method will
/// add them to the `SchemaGenerator`'s schema definitions.
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);
}

Expand Down
23 changes: 23 additions & 0 deletions schemars/tests/expected/inline-subschemas.json
Original file line number Diff line number Diff line change
@@ -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 changes: 21 additions & 0 deletions schemars/tests/inline_subschemas.rs
Original file line number Diff line number Diff line change
@@ -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)
}