Skip to content

Commit cd0e2a8

Browse files
gracekarinafrantuma
authored andcommitted
oas 3.1 - includes support for #1603
1 parent dc15b72 commit cd0e2a8

File tree

4 files changed

+189
-37
lines changed

4 files changed

+189
-37
lines changed

modules/swagger-parser-core/src/main/java/io/swagger/v3/parser/core/models/ParseOptions.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class ParseOptions {
1515
private boolean resolveRequestBody = false;
1616

1717
private boolean oaiAuthor;
18+
private boolean defaultSchemaTypeObject = true;
1819

1920
public boolean isResolve() {
2021
return resolve;
@@ -122,4 +123,12 @@ public void setValidateInternalRefs(boolean validateInternalRefs) {
122123
public boolean isValidateInternalRefs() {
123124
return validateInternalRefs;
124125
}
126+
127+
public boolean isDefaultSchemaTypeObject() {
128+
return defaultSchemaTypeObject;
129+
}
130+
131+
public void setDefaultSchemaTypeObject(boolean defaultSchemaTypeObject) {
132+
this.defaultSchemaTypeObject = defaultSchemaTypeObject;
133+
}
125134
}

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java

Lines changed: 72 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,6 @@ public SwaggerParseResult deserialize(JsonNode rootNode) {
271271
return deserialize(rootNode, null);
272272
}
273273

274-
public SwaggerParseResult deserialize(JsonNode rootNode, String path) {
275-
return deserialize(rootNode, null, false);
276-
}
277-
278274
public SwaggerParseResult deserialize(JsonNode rootNode, String path) {
279275
return deserialize(rootNode,path, new ParseOptions());
280276
}
@@ -290,7 +286,8 @@ public SwaggerParseResult deserialize(JsonNode rootNode, String path, ParseOptio
290286
SwaggerParseResult result = new SwaggerParseResult();
291287
try {
292288
ParseResult rootParse = new ParseResult();
293-
rootParse.setOaiAuthor(isOaiAuthor);
289+
rootParse.setOaiAuthor(options.isOaiAuthor());
290+
rootParse.setDefaultSchemaTypeObject(options.isDefaultSchemaTypeObject());
294291
rootParse.setAllowEmptyStrings(options.isAllowEmptyString());
295292
rootParse.setValidateInternalRefs(options.isValidateInternalRefs());
296293
OpenAPI api = parseRoot(rootNode, rootParse, path);
@@ -1376,7 +1373,7 @@ public MediaType getMediaType(ObjectNode contentNode, String location, ParseResu
13761373
, false));
13771374
}
13781375

1379-
Object example = getAnyExample("example", contentNode, location, result);
1376+
Object example = getAnyType("example", contentNode, location, result);
13801377
if (example != null) {
13811378
if (examplesObject != null) {
13821379
result.warning(location, "examples already defined -- ignoring \"example\" field");
@@ -1986,7 +1983,7 @@ public Parameter getParameter(ObjectNode obj, String location, ParseResult resul
19861983
, false));
19871984
}
19881985

1989-
Object example = getAnyExample("example", obj, location, result);
1986+
Object example = getAnyType("example", obj, location, result);
19901987
if (example != null) {
19911988
if (examplesObject != null) {
19921989
result.warning(location, "examples already defined -- ignoring \"example\" field");
@@ -2151,7 +2148,7 @@ public Header getHeader(ObjectNode headerNode, String location, ParseResult resu
21512148
header.setExamples(getExamples(examplesObject, location, result, false));
21522149
}
21532150

2154-
Object example = getAnyExample("example", headerNode, location, result);
2151+
Object example = getAnyType("example", headerNode, location, result);
21552152
if (example != null) {
21562153
if (examplesObject != null) {
21572154
result.warning(location, "examples already defined -- ignoring \"example\" field");
@@ -2180,8 +2177,8 @@ public Header getHeader(ObjectNode headerNode, String location, ParseResult resu
21802177

21812178
return header;
21822179
}
2183-
//TODO rename method as is used by different objects not only to get examples
2184-
public Object getAnyExample(String nodeKey, ObjectNode node, String location, ParseResult result) {
2180+
2181+
public Object getAnyType(String nodeKey, ObjectNode node, String location, ParseResult result) {
21852182
JsonNode example = node.get(nodeKey);
21862183
if (example != null) {
21872184
if (example.getNodeType().equals(JsonNodeType.STRING)) {
@@ -2602,7 +2599,7 @@ at the moment path passed as string (basePath) from upper components can be both
26022599
}
26032600
}
26042601

2605-
if (itemsNode != null) {
2602+
if (itemsNode != null && result.isDefaultSchemaTypeObject()) {
26062603
ArraySchema items = new ArraySchema();
26072604
if (itemsNode.getNodeType().equals(JsonNodeType.OBJECT)) {
26082605
items.setItems(getSchema(itemsNode, location, result));
@@ -2614,6 +2611,18 @@ at the moment path passed as string (basePath) from upper components can be both
26142611
}
26152612
}
26162613
schema = items;
2614+
}else if (itemsNode != null){
2615+
Schema items = new Schema();
2616+
if (itemsNode.getNodeType().equals(JsonNodeType.OBJECT)) {
2617+
items.setItems(getSchema(itemsNode, location, result));
2618+
} else if (itemsNode.getNodeType().equals(JsonNodeType.ARRAY)) {
2619+
for (JsonNode n : itemsNode) {
2620+
if (n.isValueNode()) {
2621+
items.setItems(getSchema(itemsNode, location, result));
2622+
}
2623+
}
2624+
}
2625+
schema = items;
26172626
}
26182627

26192628
Boolean additionalPropertiesBoolean = getBoolean("additionalProperties", node, false, location, result);
@@ -2628,7 +2637,7 @@ at the moment path passed as string (basePath) from upper components can be both
26282637
? getSchema(additionalPropertiesObject, location, result)
26292638
: additionalPropertiesBoolean;
26302639

2631-
if (additionalProperties != null) {
2640+
if (additionalProperties != null && result.isDefaultSchemaTypeObject()) {
26322641
if (schema == null) {
26332642
schema =
26342643
additionalProperties.equals(Boolean.FALSE)
@@ -2857,7 +2866,7 @@ at the moment path passed as string (basePath) from upper components can be both
28572866
}
28582867

28592868
//sets default value according to the schema type
2860-
if (node.get("default") != null) {
2869+
if (node.get("default") != null && result.isDefaultSchemaTypeObject()) {
28612870
if (!StringUtils.isBlank(schema.getType())) {
28622871
if (schema.getType().equals("array")) {
28632872
ArrayNode array = getArray("default", node, false, location, result);
@@ -2896,6 +2905,13 @@ at the moment path passed as string (basePath) from upper components can be both
28962905
}
28972906
}
28982907
}
2908+
}else{
2909+
schema.setDefault(null);
2910+
}
2911+
2912+
bool = getBoolean("nullable", node, false, location, result);
2913+
if (bool != null) {
2914+
schema.setNullable(bool);
28992915
}
29002916

29012917
bool = getBoolean("readOnly", node, false, location, result);
@@ -2931,7 +2947,7 @@ at the moment path passed as string (basePath) from upper components can be both
29312947
}
29322948
}
29332949

2934-
Object example = getAnyExample("example", node, location, result);
2950+
Object example = getAnyType("example", node, location, result);
29352951
if (example != null) {
29362952
schema.setExample(example instanceof NullNode ? null : example);
29372953
}
@@ -3139,7 +3155,7 @@ public Example getExample(ObjectNode node, String location, ParseResult result)
31393155
example.setDescription(value);
31403156
}
31413157

3142-
Object sample = getAnyExample("value", node, location, result);
3158+
Object sample = getAnyType("value", node, location, result);
31433159
if (sample != null) {
31443160
example.setValue(sample instanceof NullNode ? null : sample);
31453161
}
@@ -3648,8 +3664,19 @@ public Schema getJsonSchema(ObjectNode node, String location, ParseResult result
36483664
schema = composedSchema;
36493665
}
36503666
}
3651-
3652-
if (itemsNode != null) {
3667+
if (itemsNode != null && result.isDefaultSchemaTypeObject()) {
3668+
ArraySchema items = new ArraySchema();
3669+
if (itemsNode.getNodeType().equals(JsonNodeType.OBJECT)) {
3670+
items.setItems(getSchema(itemsNode, location, result));
3671+
} else if (itemsNode.getNodeType().equals(JsonNodeType.ARRAY)) {
3672+
for (JsonNode n : itemsNode) {
3673+
if (n.isValueNode()) {
3674+
items.setItems(getSchema(itemsNode, location, result));
3675+
}
3676+
}
3677+
}
3678+
schema = items;
3679+
}else if (itemsNode != null) {
36533680
JsonSchema items = new JsonSchema();
36543681
if (itemsNode.getNodeType().equals(JsonNodeType.OBJECT)) {
36553682
items.setItems(getJsonSchema(itemsNode, location, result));
@@ -3675,7 +3702,16 @@ public Schema getJsonSchema(ObjectNode node, String location, ParseResult result
36753702
? getJsonSchema(additionalPropertiesObject, location, result)
36763703
: additionalPropertiesBoolean;
36773704

3678-
if (additionalProperties != null) {
3705+
3706+
if (additionalProperties != null && result.isDefaultSchemaTypeObject()) {
3707+
if (schema == null) {
3708+
schema =
3709+
additionalProperties.equals(Boolean.FALSE)
3710+
? new ObjectSchema()
3711+
: new MapSchema();
3712+
}
3713+
schema.setAdditionalProperties(additionalProperties);
3714+
}else if (additionalProperties != null) {
36793715
if (schema == null) {
36803716
schema = new JsonSchema();
36813717
}
@@ -3751,7 +3787,9 @@ public Schema getJsonSchema(ObjectNode node, String location, ParseResult result
37513787

37523788
if (node.get("default") != null) {
37533789
//TODO rename method
3754-
schema.setDefault(getAnyExample("default",node,location,result));
3790+
if(result.isDefaultSchemaTypeObject()) {
3791+
schema.setDefault(getAnyType("default", node, location, result));
3792+
}
37553793
}
37563794

37573795
ObjectNode discriminatorNode = getObject("discriminator", node, false, location, result);
@@ -4136,7 +4174,7 @@ public Schema getJsonSchema(ObjectNode node, String location, ParseResult result
41364174
schema.setExamples(exampleList);
41374175
}
41384176

4139-
Object example = getAnyExample("example", node, location, result);
4177+
Object example = getAnyType("example", node, location, result);
41404178
if (example != null) {
41414179
schema.setExample(example instanceof NullNode ? null : example);
41424180
}
@@ -4196,9 +4234,23 @@ public static class ParseResult {
41964234
private List<Location> reserved = new ArrayList<>();
41974235
private boolean validateInternalRefs;
41984236

4237+
private boolean defaultSchemaTypeObject = true;
41994238
private boolean openapi31 = false;
42004239
private boolean oaiAuthor = false;
42014240

4241+
public boolean isDefaultSchemaTypeObject() {
4242+
return defaultSchemaTypeObject;
4243+
}
4244+
4245+
public void setDefaultSchemaTypeObject(boolean defaultSchemaTypeObject) {
4246+
this.defaultSchemaTypeObject = defaultSchemaTypeObject;
4247+
}
4248+
4249+
public ParseResult defaultSchemaTypeObject(boolean defaultSchemaTypeObject) {
4250+
this.defaultSchemaTypeObject = defaultSchemaTypeObject;
4251+
return this;
4252+
}
4253+
42024254
public ParseResult() {
42034255
}
42044256

modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OAI31DeserializationTest.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,7 @@ public void testTuplesJSONSchema() {
719719
@Test(description = "Test for not setting the schema type as default")
720720
public void testNotDefaultSchemaType() {
721721
ParseOptions options = new ParseOptions();
722+
options.setDefaultSchemaTypeObject(false);
722723
String defaultSchemaType = "openapi: 3.1.0\n" +
723724
"info:\n" +
724725
" title: ping test\n" +
@@ -785,42 +786,39 @@ public void testNotDefaultSchemaType() {
785786
//map_any_value as object when it should be null
786787
assertNotNull(openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("map_any_value"));
787788
assertTrue(openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("map_any_value") instanceof Schema);
788-
Schema mapProperty = (Schema)openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("map_any_value");
789-
assertNull(mapProperty.getType());
789+
Schema schema = (Schema)openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("map_any_value");
790+
assertNull(schema.getType());
790791

791792
//map_any_value_with_desc as object when it should be null
792793
assertNotNull(openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("map_any_value_with_desc"));
793794
assertTrue(openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("map_any_value_with_desc") instanceof Schema);
794-
mapProperty = (Schema)openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("map_any_value_with_desc");
795-
assertNull(mapProperty.getType());
795+
schema = (Schema)openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("map_any_value_with_desc");
796+
assertNull(schema.getType());
796797

797798
//map_any_value_nullable as object when it should be null
798799
assertNotNull(openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("map_any_value_nullable"));
799800
assertTrue(openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("map_any_value_nullable") instanceof Schema);
800-
mapProperty = (Schema)openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("map_any_value_nullable");
801-
assertNull(mapProperty.getType());
801+
schema = (Schema)openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("map_any_value_nullable");
802+
assertNull(schema.getType());
802803

803804
//array_any_value as array when it should be null
804805
assertNotNull(openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("array_any_value"));
805806
assertTrue(openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("array_any_value") instanceof Schema);
806-
mapProperty = (Schema)openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("array_any_value");
807-
assertNull(mapProperty.getType());
807+
schema = (Schema)openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("array_any_value");
808+
assertNull(schema.getType());
808809

809810
//array_any_value_with_desc as array when it should be null
810811
assertNotNull(openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("array_any_value_with_desc"));
811812
assertTrue(openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("array_any_value_with_desc") instanceof Schema);
812-
mapProperty = (Schema)openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("array_any_value_with_desc");
813-
assertNull(mapProperty.getType());
813+
schema = (Schema)openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("array_any_value_with_desc");
814+
assertNull(schema.getType());
814815

815816
//array_any_value_nullable
816817
assertNotNull(openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("array_any_value_nullable"));
817818
assertTrue(openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("array_any_value_nullable") instanceof Schema);
818-
mapProperty = (Schema)openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("array_any_value_nullable");
819-
assertNull(mapProperty.getType());
820-
assertNull(mapProperty.getItems().getNullable());
821-
Yaml31.prettyPrint(mapProperty);
822-
assertNotNull(mapProperty.getItems().getExtensions().get("nullable"));
823-
//TODO check the toString Method difference between SOUT and YAML31 prettyprint
824-
System.out.println(mapProperty);
819+
schema = (Schema)openAPI.getComponents().getSchemas().get("AnyValueModelInline").getProperties().get("array_any_value_nullable");
820+
assertNull(schema.getType());
821+
assertNull(schema.getItems().getNullable());
822+
assertNotNull(schema.getItems().getExtensions().get("nullable"));
825823
}
826824
}

0 commit comments

Comments
 (0)