Skip to content

Commit

Permalink
Fix nested schemas referencing
Browse files Browse the repository at this point in the history
  • Loading branch information
Maksim Zvankovich committed Apr 6, 2023
1 parent 416f062 commit ae9ddde
Show file tree
Hide file tree
Showing 34 changed files with 15,851 additions and 56,052 deletions.
2,569 changes: 482 additions & 2,087 deletions 27.0.json

Large diffs are not rendered by default.

1,485 changes: 437 additions & 1,048 deletions 27.0.yml

Large diffs are not rendered by default.

2,800 changes: 515 additions & 2,285 deletions 29.0.json

Large diffs are not rendered by default.

1,617 changes: 470 additions & 1,147 deletions 29.0.yml

Large diffs are not rendered by default.

2,829 changes: 519 additions & 2,310 deletions 30.0.json

Large diffs are not rendered by default.

1,634 changes: 474 additions & 1,160 deletions 30.0.yml

Large diffs are not rendered by default.

2,920 changes: 532 additions & 2,388 deletions 31.0.json

Large diffs are not rendered by default.

1,686 changes: 487 additions & 1,199 deletions 31.0.yml

Large diffs are not rendered by default.

2,990 changes: 542 additions & 2,448 deletions 32.0.json

Large diffs are not rendered by default.

1,726 changes: 497 additions & 1,229 deletions 32.0.yml

Large diffs are not rendered by default.

3,050 changes: 550 additions & 2,500 deletions 33.0.json

Large diffs are not rendered by default.

1,762 changes: 505 additions & 1,257 deletions 33.0.yml

Large diffs are not rendered by default.

3,110 changes: 558 additions & 2,552 deletions 34.0.json

Large diffs are not rendered by default.

1,798 changes: 513 additions & 1,285 deletions 34.0.yml

Large diffs are not rendered by default.

3,153 changes: 565 additions & 2,588 deletions 35.0.json

Large diffs are not rendered by default.

1,820 changes: 520 additions & 1,300 deletions 35.0.yml

Large diffs are not rendered by default.

3,153 changes: 565 additions & 2,588 deletions 35.2.json

Large diffs are not rendered by default.

1,820 changes: 520 additions & 1,300 deletions 35.2.yml

Large diffs are not rendered by default.

3,138 changes: 563 additions & 2,575 deletions 36.0.json

Large diffs are not rendered by default.

1,811 changes: 518 additions & 1,293 deletions 36.0.yml

Large diffs are not rendered by default.

3,138 changes: 563 additions & 2,575 deletions 36.1.json

Large diffs are not rendered by default.

1,811 changes: 518 additions & 1,293 deletions 36.1.yml

Large diffs are not rendered by default.

3,138 changes: 563 additions & 2,575 deletions 36.2.json

Large diffs are not rendered by default.

1,811 changes: 518 additions & 1,293 deletions 36.2.yml

Large diffs are not rendered by default.

3,139 changes: 563 additions & 2,576 deletions 36.3.json

Large diffs are not rendered by default.

1,812 changes: 518 additions & 1,294 deletions 36.3.yml

Large diffs are not rendered by default.

3,167 changes: 567 additions & 2,600 deletions 37.0.json

Large diffs are not rendered by default.

1,828 changes: 522 additions & 1,306 deletions 37.0.yml

Large diffs are not rendered by default.

3,181 changes: 569 additions & 2,612 deletions 37.1.json

Large diffs are not rendered by default.

1,836 changes: 524 additions & 1,312 deletions 37.1.yml

Large diffs are not rendered by default.

151 changes: 84 additions & 67 deletions transformer/src/parsers/doc/etc/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ impl
..
}) => attributes.get("name").map_or(false, |name| {
match type_name.split_once(':') {
Some((ns, tn)) if ns.eq("class") && tn.eq(name) => {
true
}
Some((ns, tn)) if tn.eq(name) => {
attributes.get("type").map_or(false, |t| {
t.split_once(':')
Expand All @@ -98,12 +101,12 @@ impl
attributes,
..
}) => SimpleType::try_from((ns, xml))
.map(|s| openapiv3::ReferenceOr::Item(s))
.ok()
.or(attributes.get("type").map(|type_name| {
str_to_simple_type_or_reference(ns, type_name, None)
}))
.map(|r#type| (name, r#type)),
.map(|s| openapiv3::ReferenceOr::Item(s))
.ok()
.or(attributes.get("type").map(|type_name| {
str_to_simple_type_or_reference(ns, type_name, None)
}))
.map(|r#type| (name, r#type)),
_ => None,
})
})
Expand Down Expand Up @@ -143,12 +146,12 @@ impl
}
}
xmltree::XMLNode::Element(xmltree::Element {
namespace: Some(_xml_schema_ns),
namespace: Some(namespace),
name,
attributes,
children,
..
}) if name == "attribute" => {
}) if namespace == XML_SCHEMA_NS && name == "attribute" => {
let name = attributes
.get("name")
.map(|name| decapitalize(name))
Expand Down Expand Up @@ -183,9 +186,9 @@ impl
}
}

impl From<&Field> for openapiv3::Schema {
impl From<&Field> for openapiv3::ReferenceOr<openapiv3::Type> {
fn from(s: &Field) -> Self {
let reference_or_schema_type = match &s.r#type {
match &s.r#type {
openapiv3::ReferenceOr::Item(s) => {
openapiv3::ReferenceOr::Item(openapiv3::Type::from(&RestrictedPrimitiveType {
r#type: s.parent,
Expand All @@ -197,48 +200,62 @@ impl From<&Field> for openapiv3::Schema {
openapiv3::ReferenceOr::Reference { reference } => openapiv3::ReferenceOr::Reference {
reference: format!("#/components/schemas/{}", reference),
},
};
}
}
}

openapiv3::Schema {
schema_data: openapiv3::SchemaData {
nullable: false,
read_only: false,
deprecated: s.annotation.as_ref().map(|a| a.deprecated) == Some(true),
description: s.annotation.as_ref().and_then(|a| a.description.clone()),
..Default::default()
},
schema_kind: match (s.occurrences, reference_or_schema_type) {
(Occurrences::Array, openapiv3::ReferenceOr::Item(schema_type)) => {
openapiv3::SchemaKind::Type(openapiv3::Type::Array(openapiv3::ArrayType {
items: Some(openapiv3::ReferenceOr::boxed_item(openapiv3::Schema {
schema_data: Default::default(),
schema_kind: openapiv3::SchemaKind::Type(schema_type),
})),
min_items: None,
max_items: None,
unique_items: false,
}))
}
impl From<&Field> for openapiv3::ReferenceOr<openapiv3::Schema> {
fn from(s: &Field) -> Self {
let reference_or_schema_type = openapiv3::ReferenceOr::from(s);
let schema_data = openapiv3::SchemaData {
nullable: false,
read_only: false,
deprecated: s.annotation.as_ref().map(|a| a.deprecated) == Some(true),
description: s.annotation.as_ref().and_then(|a| a.description.clone()),
..Default::default()
};
match (s.occurrences, reference_or_schema_type) {
(Occurrences::Array, openapiv3::ReferenceOr::Item(schema_type)) => {
openapiv3::ReferenceOr::Item(openapiv3::Schema {
schema_data,
schema_kind: openapiv3::SchemaKind::Type(openapiv3::Type::Array(
openapiv3::ArrayType {
items: Some(openapiv3::ReferenceOr::boxed_item(openapiv3::Schema {
schema_data: Default::default(),
schema_kind: openapiv3::SchemaKind::Type(schema_type),
})),
min_items: None,
max_items: None,
unique_items: false,
},
)),
})
}

(Occurrences::Array, openapiv3::ReferenceOr::Reference { reference }) => {
openapiv3::SchemaKind::Type(openapiv3::Type::Array(openapiv3::ArrayType {
items: Some(openapiv3::ReferenceOr::Reference { reference }),
min_items: None,
max_items: None,
unique_items: false,
}))
}
(Occurrences::Array, openapiv3::ReferenceOr::Reference { reference }) => {
openapiv3::ReferenceOr::Item(openapiv3::Schema {
schema_data,
schema_kind: openapiv3::SchemaKind::Type(openapiv3::Type::Array(
openapiv3::ArrayType {
items: Some(openapiv3::ReferenceOr::Reference { reference }),
min_items: None,
max_items: None,
unique_items: false,
},
)),
})
}

(_, openapiv3::ReferenceOr::Item(schema_type)) => {
openapiv3::SchemaKind::Type(schema_type)
}
(_, openapiv3::ReferenceOr::Item(schema_type)) => {
openapiv3::ReferenceOr::Item(openapiv3::Schema {
schema_data,
schema_kind: openapiv3::SchemaKind::Type(schema_type),
})
}

(_, openapiv3::ReferenceOr::Reference { reference }) => {
openapiv3::SchemaKind::AllOf {
all_of: vec![openapiv3::ReferenceOr::Reference { reference }],
}
}
},
(_, openapiv3::ReferenceOr::Reference { reference }) => {
openapiv3::ReferenceOr::Reference { reference }
}
}
}
}
Expand Down Expand Up @@ -297,7 +314,7 @@ fn test_parse_field_from_required_attribute() {
&vec![(ns, xmltree::XMLNode::Element(types))],
))
.unwrap();
let value = openapiv3::Schema::from(&s);
let value: openapiv3::ReferenceOr<openapiv3::Schema> = openapiv3::ReferenceOr::from(&s);
assert_eq!(
serde_json::to_value(value).unwrap(),
json!({
Expand Down Expand Up @@ -329,7 +346,7 @@ fn test_parse_field_from_optional_attribute() {
&vec![(ns, xmltree::XMLNode::Element(types))],
))
.unwrap();
let value = openapiv3::Schema::from(&s);
let value: openapiv3::ReferenceOr<openapiv3::Schema> = openapiv3::ReferenceOr::from(&s);
assert_eq!(
serde_json::to_value(value).unwrap(),
json!({
Expand Down Expand Up @@ -361,7 +378,7 @@ fn test_field_optional_into_schema() {
&vec![(ns, xmltree::XMLNode::Element(types))],
))
.unwrap();
let value = openapiv3::Schema::from(&s);
let value: openapiv3::ReferenceOr<openapiv3::Schema> = openapiv3::ReferenceOr::from(&s);
assert_eq!(
serde_json::to_value(value).unwrap(),
json!({
Expand Down Expand Up @@ -393,7 +410,7 @@ fn test_field_array_into_schema() {
&vec![(ns, xmltree::XMLNode::Element(types))],
))
.unwrap();
let value = openapiv3::Schema::from(&s);
let value: openapiv3::ReferenceOr<openapiv3::Schema> = openapiv3::ReferenceOr::from(&s);
assert_eq!(
serde_json::to_value(value).unwrap(),
json!({
Expand Down Expand Up @@ -429,7 +446,7 @@ fn test_field_exactly_one_into_schema() {
&vec![(ns, xmltree::XMLNode::Element(types))],
))
.unwrap();
let value = openapiv3::Schema::from(&s);
let value: openapiv3::ReferenceOr<openapiv3::Schema> = openapiv3::ReferenceOr::from(&s);
assert_eq!(
serde_json::to_value(value).unwrap(),
json!({
Expand Down Expand Up @@ -461,7 +478,7 @@ fn test_anyuri_into_schema() {
&vec![(ns, xmltree::XMLNode::Element(types))],
))
.unwrap();
let value = openapiv3::Schema::from(&s);
let value: openapiv3::ReferenceOr<openapiv3::Schema> = openapiv3::ReferenceOr::from(&s);
assert_eq!(
serde_json::to_value(value).unwrap(),
json!({
Expand Down Expand Up @@ -494,7 +511,7 @@ fn test_double_into_schema() {
&vec![(ns, xmltree::XMLNode::Element(types))],
))
.unwrap();
let value = openapiv3::Schema::from(&s);
let value: openapiv3::ReferenceOr<openapiv3::Schema> = openapiv3::ReferenceOr::from(&s);
assert_eq!(
serde_json::to_value(value).unwrap(),
json!({
Expand Down Expand Up @@ -527,7 +544,7 @@ fn test_long_into_schema() {
&vec![(ns, xmltree::XMLNode::Element(types))],
))
.unwrap();
let value = openapiv3::Schema::from(&s);
let value: openapiv3::ReferenceOr<openapiv3::Schema> = openapiv3::ReferenceOr::from(&s);
assert_eq!(
serde_json::to_value(value).unwrap(),
json!({
Expand Down Expand Up @@ -560,7 +577,7 @@ fn test_datetime_into_schema() {
&vec![(ns, xmltree::XMLNode::Element(types))],
))
.unwrap();
let value = openapiv3::Schema::from(&s);
let value: openapiv3::ReferenceOr<openapiv3::Schema> = openapiv3::ReferenceOr::from(&s);
assert_eq!(
serde_json::to_value(value).unwrap(),
json!({
Expand Down Expand Up @@ -593,7 +610,7 @@ fn test_base64_binary_into_schema() {
&vec![(ns, xmltree::XMLNode::Element(types))],
))
.unwrap();
let value = openapiv3::Schema::from(&s);
let value: openapiv3::ReferenceOr<openapiv3::Schema> = openapiv3::ReferenceOr::from(&s);
assert_eq!(
serde_json::to_value(value).unwrap(),
json!({
Expand Down Expand Up @@ -626,7 +643,7 @@ fn test_normalized_string_into_schema() {
&vec![(ns, xmltree::XMLNode::Element(types))],
))
.unwrap();
let value = openapiv3::Schema::from(&s);
let value: openapiv3::ReferenceOr<openapiv3::Schema> = openapiv3::ReferenceOr::from(&s);
assert_eq!(
serde_json::to_value(value).unwrap(),
json!({
Expand Down Expand Up @@ -658,7 +675,7 @@ fn test_short_into_schema() {
&vec![(ns, xmltree::XMLNode::Element(types))],
))
.unwrap();
let value = openapiv3::Schema::from(&s);
let value: openapiv3::ReferenceOr<openapiv3::Schema> = openapiv3::ReferenceOr::from(&s);
assert_eq!(
serde_json::to_value(value).unwrap(),
json!({
Expand Down Expand Up @@ -690,7 +707,7 @@ fn test_decimal_into_schema() {
&vec![(ns, xmltree::XMLNode::Element(types))],
))
.unwrap();
let value = openapiv3::Schema::from(&s);
let value: openapiv3::ReferenceOr<openapiv3::Schema> = openapiv3::ReferenceOr::from(&s);
assert_eq!(
serde_json::to_value(value).unwrap(),
json!({
Expand Down Expand Up @@ -722,7 +739,7 @@ fn test_float_into_schema() {
&vec![(ns, xmltree::XMLNode::Element(types))],
))
.unwrap();
let value = openapiv3::Schema::from(&s);
let value: openapiv3::ReferenceOr<openapiv3::Schema> = openapiv3::ReferenceOr::from(&s);
assert_eq!(
serde_json::to_value(value).unwrap(),
json!({
Expand Down Expand Up @@ -755,7 +772,7 @@ fn test_hex_binary_into_schema() {
&vec![(ns, xmltree::XMLNode::Element(types))],
))
.unwrap();
let value = openapiv3::Schema::from(&s);
let value: openapiv3::ReferenceOr<openapiv3::Schema> = openapiv3::ReferenceOr::from(&s);
assert_eq!(
serde_json::to_value(value).unwrap(),
json!({
Expand Down Expand Up @@ -787,7 +804,7 @@ fn test_integer_into_schema() {
&vec![(ns, xmltree::XMLNode::Element(types))],
))
.unwrap();
let value = openapiv3::Schema::from(&s);
let value: openapiv3::ReferenceOr<openapiv3::Schema> = openapiv3::ReferenceOr::from(&s);
assert_eq!(
serde_json::to_value(value).unwrap(),
json!({
Expand Down Expand Up @@ -819,7 +836,7 @@ fn test_any_type_into_schema() {
&vec![(ns, xmltree::XMLNode::Element(types))],
))
.unwrap();
let value = openapiv3::Schema::from(&s);
let value: openapiv3::ReferenceOr<openapiv3::Schema> = openapiv3::ReferenceOr::from(&s);
assert_eq!(
serde_json::to_value(value).unwrap(),
json!({
Expand Down Expand Up @@ -856,7 +873,7 @@ fn test_element_with_simple_type() {
&vec![(ns, xmltree::XMLNode::Element(types))],
))
.unwrap();
let value = openapiv3::Schema::from(&s);
let value: openapiv3::ReferenceOr<openapiv3::Schema> = openapiv3::ReferenceOr::from(&s);
assert_eq!(
serde_json::to_value(value).unwrap(),
json!({
Expand Down Expand Up @@ -887,7 +904,7 @@ fn test_attribute_with_simple_type() {
&vec![(ns, xmltree::XMLNode::Element(types))],
))
.unwrap();
let value = openapiv3::Schema::from(&s);
let value: openapiv3::ReferenceOr<openapiv3::Schema> = openapiv3::ReferenceOr::from(&s);
assert_eq!(
serde_json::to_value(value).unwrap(),
json!({
Expand Down
7 changes: 6 additions & 1 deletion transformer/src/parsers/doc/etc/object_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,12 @@ impl From<&ObjectType> for openapiv3::Schema {
.map(|s| {
(
s.name.clone(),
openapiv3::ReferenceOr::boxed_item(openapiv3::Schema::from(s)),
match openapiv3::ReferenceOr::from(s) {
openapiv3::ReferenceOr::Item(v) => openapiv3::ReferenceOr::Item(Box::new(v)),
openapiv3::ReferenceOr::Reference { reference } => {
openapiv3::ReferenceOr::Reference { reference }
}
}
)
})
.collect(),
Expand Down
7 changes: 1 addition & 6 deletions transformer/src/parsers/doc/etc/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,7 @@ fn schema_into_schemas_test() {
"format": "int32"
},
"boundedCustom2": {
"description": "A reference to another type, but only one or none",
"allOf": [
{
"$ref": "#/components/schemas/test_Custom2Type"
}
]
"$ref": "#/components/schemas/test_Custom2Type"
},
"unboundedCustom2": {
"description": "A reference to many of another type",
Expand Down
6 changes: 3 additions & 3 deletions transformer/src/parsers/doc/etc/simple_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl TryFrom<(Option<&str>, &xmltree::XMLNode)> for SimpleType {
for child in children {
match child {
xmltree::XMLNode::Element(xmltree::Element {
namespace: Some(_xml_schema_ns),
namespace: Some(_),
name: node_name,
attributes,
..
Expand All @@ -57,7 +57,7 @@ impl TryFrom<(Option<&str>, &xmltree::XMLNode)> for SimpleType {
});
}
xmltree::XMLNode::Element(xmltree::Element {
namespace: Some(_xml_schema_ns),
namespace: Some(_),
name: node_name,
attributes,
children,
Expand Down Expand Up @@ -96,7 +96,7 @@ impl TryFrom<(Option<&str>, &xmltree::XMLNode)> for SimpleType {
.iter()
.filter_map(|child| match child {
xmltree::XMLNode::Element(xmltree::Element {
namespace: Some(_xml_schema_ns),
namespace: Some(_),
name,
attributes,
..
Expand Down

0 comments on commit ae9ddde

Please sign in to comment.