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

Fix multiple flatten with all_of #264

Closed
wants to merge 1 commit into from
Closed
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
63 changes: 60 additions & 3 deletions schemars/src/flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,66 @@ impl Schema {
} else if is_null_type(&other) {
return self;
}
let s1: SchemaObject = self.into();
let s2: SchemaObject = other.into();
Schema::Object(s1.merge(s2))
let mut s1: SchemaObject = self.into();
let mut s2: SchemaObject = other.into();

let s1_subschemas = &mut s1.subschemas;
let s1_all_of = s1_subschemas.as_mut().and_then(|sub_sch| sub_sch.all_of.as_mut());
let s2_subschemas = &mut s2.subschemas;
let s2_all_of = s2_subschemas.as_mut().and_then(|sub_sch| sub_sch.all_of.as_mut());

match (s1_all_of, s2_all_of) {
(Some(s1_all_of), Some(s2_all_of)) => {
let mut s1_all_of = std::mem::take(s1_all_of);
s1_all_of.append(s2_all_of);
Schema::Object(SchemaObject {
subschemas: Some(Box::new(SubschemaValidation {
all_of: Some(s1_all_of),
..Default::default()
})),
..Default::default()
})
}
(Some(s1_all_of), None) => {
let mut s1_all_of = std::mem::take(s1_all_of);
s1_all_of.push(Schema::Object(s2));
Schema::Object(SchemaObject {
subschemas: Some(Box::new(SubschemaValidation {
all_of: Some(s1_all_of),
..Default::default()
})),
..Default::default()
})
}
(None, Some(s2_all_of)) => {
let mut s2_all_of = std::mem::take(s2_all_of);
s2_all_of.push(Schema::Object(s1));
Schema::Object(SchemaObject {
subschemas: Some(Box::new(SubschemaValidation {
all_of: Some(s2_all_of),
..Default::default()
})),
..Default::default()
})
}
(None, None) => {
match (s1_subschemas.is_some(), s2_subschemas.is_some()) {
(true, true) => Schema::Object(SchemaObject {
subschemas: Some(Box::new(SubschemaValidation {
all_of: Some(vec![
Schema::Object(s1),
Schema::Object(s2),
]),
..Default::default()
})),
..Default::default()
}),
(true, false) |
(false, true) |
(false, false) => Schema::Object(s1.merge(s2)),
}
}
}
}
}

Expand Down
75 changes: 75 additions & 0 deletions schemars/tests/expected/multiple_enum_flatten.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Flat",
"allOf": [
{
"type": "object",
"oneOf": [
{
"type": "object",
"required": [
"B"
],
"properties": {
"B": {
"type": "boolean"
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"S"
],
"properties": {
"S": {
"type": "string"
}
},
"additionalProperties": false
}
],
"required": [
"f"
],
"properties": {
"f": {
"type": "number",
"format": "float"
}
}
},
{
"oneOf": [
{
"type": "object",
"required": [
"U"
],
"properties": {
"U": {
"type": "integer",
"format": "uint32",
"minimum": 0.0
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"F"
],
"properties": {
"F": {
"type": "number",
"format": "double"
}
},
"additionalProperties": false
}
]
}
]
}
33 changes: 33 additions & 0 deletions schemars/tests/multiple_enum_flatten.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
mod util;
use schemars::JsonSchema;
use util::*;

#[allow(dead_code)]
#[derive(JsonSchema)]
#[schemars(rename = "Flat")]
struct Flat {
f: f32,
#[schemars(flatten)]
e1: Enum1,
#[schemars(flatten)]
e2: Enum2,
}

#[allow(dead_code)]
#[derive(JsonSchema)]
enum Enum1 {
B(bool),
S(String),
}

#[allow(dead_code)]
#[derive(JsonSchema)]
enum Enum2 {
U(u32),
F(f64)
}

#[test]
fn test_flat_schema() -> TestResult {
test_default_generated_schema::<Flat>("multiple_enum_flatten")
}
Loading