Skip to content

Commit ff37eac

Browse files
committed
includes support for #1603
1 parent bb81a64 commit ff37eac

File tree

5 files changed

+194
-38
lines changed

5 files changed

+194
-38
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
@@ -9,6 +9,7 @@ public class ParseOptions {
99
private boolean camelCaseFlattenNaming;
1010
private boolean skipMatches;
1111
private boolean oaiAuthor;
12+
private boolean defaultSchemaTypeObject = true;
1213

1314
public boolean isResolve() {
1415
return resolve;
@@ -69,4 +70,12 @@ public boolean isOaiAuthor() {
6970
return oaiAuthor;
7071
}
7172

73+
public boolean isDefaultSchemaTypeObject() {
74+
return defaultSchemaTypeObject;
75+
}
76+
77+
public void setDefaultSchemaTypeObject(boolean defaultSchemaTypeObject) {
78+
this.defaultSchemaTypeObject = defaultSchemaTypeObject;
79+
}
80+
7281
}

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/OpenAPIV3Parser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,10 @@ public SwaggerParseResult readWithInfo(String path, JsonNode node) {
131131
}
132132

133133
public SwaggerParseResult parseJsonNode(String path, JsonNode node) {
134-
return new OpenAPIDeserializer().deserialize(node, path, false);
134+
return new OpenAPIDeserializer().deserialize(node, path, new ParseOptions());
135135
}
136136
public SwaggerParseResult parseJsonNode(String path, JsonNode node, ParseOptions options) {
137-
return new OpenAPIDeserializer().deserialize(node, path, options.isOaiAuthor());
137+
return new OpenAPIDeserializer().deserialize(node, path, options);
138138
}
139139

140140
public SwaggerParseResult readContents(String yaml) {

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

Lines changed: 75 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import io.swagger.v3.oas.models.servers.Server;
5353
import io.swagger.v3.oas.models.servers.ServerVariable;
5454
import io.swagger.v3.oas.models.servers.ServerVariables;
55+
import io.swagger.v3.parser.core.models.ParseOptions;
5556
import io.swagger.v3.parser.core.models.SwaggerParseResult;
5657
import io.swagger.v3.core.util.Json;
5758

@@ -270,18 +271,19 @@ public SwaggerParseResult deserialize(JsonNode rootNode) {
270271
}
271272

272273
public SwaggerParseResult deserialize(JsonNode rootNode, String path) {
273-
return deserialize(rootNode, null, false);
274+
return deserialize(rootNode, null, new ParseOptions());
274275
}
275276

276-
public SwaggerParseResult deserialize(JsonNode rootNode, String path, boolean isOaiAuthor) {
277+
public SwaggerParseResult deserialize(JsonNode rootNode, String path, ParseOptions options) {
277278
basePath = path;
278279
this.rootNode = rootNode;
279280
rootMap = new ObjectMapper().convertValue(rootNode, Map.class);
280281
SwaggerParseResult result = new SwaggerParseResult();
281282
try {
282283

283284
ParseResult rootParse = new ParseResult();
284-
rootParse.setOaiAuthor(isOaiAuthor);
285+
rootParse.setOaiAuthor(options.isOaiAuthor());
286+
rootParse.setDefaultSchemaTypeObject(options.isDefaultSchemaTypeObject());
285287
OpenAPI api = parseRoot(rootNode, rootParse, path);
286288
result.openapi31(rootParse.isOpenapi31());
287289
result.setOpenAPI(api);
@@ -1356,7 +1358,7 @@ public MediaType getMediaType(ObjectNode contentNode, String location, ParseResu
13561358
, false));
13571359
}
13581360

1359-
Object example = getAnyExample("example", contentNode, location, result);
1361+
Object example = getAnyType("example", contentNode, location, result);
13601362
if (example != null) {
13611363
if (examplesObject != null) {
13621364
result.warning(location, "examples already defined -- ignoring \"example\" field");
@@ -1971,7 +1973,7 @@ public Parameter getParameter(ObjectNode obj, String location, ParseResult resul
19711973
, false));
19721974
}
19731975

1974-
Object example = getAnyExample("example", obj, location, result);
1976+
Object example = getAnyType("example", obj, location, result);
19751977
if (example != null) {
19761978
if (examplesObject != null) {
19771979
result.warning(location, "examples already defined -- ignoring \"example\" field");
@@ -2104,7 +2106,7 @@ public Header getHeader(ObjectNode headerNode, String location, ParseResult resu
21042106
header.setExamples(getExamples(examplesObject, location, result, false));
21052107
}
21062108

2107-
Object example = getAnyExample("example", headerNode, location, result);
2109+
Object example = getAnyType("example", headerNode, location, result);
21082110
if (example != null) {
21092111
if (examplesObject != null) {
21102112
result.warning(location, "examples already defined -- ignoring \"example\" field");
@@ -2133,8 +2135,8 @@ public Header getHeader(ObjectNode headerNode, String location, ParseResult resu
21332135

21342136
return header;
21352137
}
2136-
//TODO rename method as is used by different objects not only to get examples
2137-
public Object getAnyExample(String nodeKey, ObjectNode node, String location, ParseResult result) {
2138+
2139+
public Object getAnyType(String nodeKey, ObjectNode node, String location, ParseResult result) {
21382140
JsonNode example = node.get(nodeKey);
21392141
if (example != null) {
21402142
if (example.getNodeType().equals(JsonNodeType.STRING)) {
@@ -2550,7 +2552,7 @@ at the moment path passed as string (basePath) from upper components can be both
25502552
}
25512553
}
25522554

2553-
if (itemsNode != null) {
2555+
if (itemsNode != null && result.isDefaultSchemaTypeObject()) {
25542556
ArraySchema items = new ArraySchema();
25552557
if (itemsNode.getNodeType().equals(JsonNodeType.OBJECT)) {
25562558
items.setItems(getSchema(itemsNode, location, result));
@@ -2562,6 +2564,18 @@ at the moment path passed as string (basePath) from upper components can be both
25622564
}
25632565
}
25642566
schema = items;
2567+
}else if (itemsNode != null){
2568+
Schema items = new Schema();
2569+
if (itemsNode.getNodeType().equals(JsonNodeType.OBJECT)) {
2570+
items.setItems(getSchema(itemsNode, location, result));
2571+
} else if (itemsNode.getNodeType().equals(JsonNodeType.ARRAY)) {
2572+
for (JsonNode n : itemsNode) {
2573+
if (n.isValueNode()) {
2574+
items.setItems(getSchema(itemsNode, location, result));
2575+
}
2576+
}
2577+
}
2578+
schema = items;
25652579
}
25662580

25672581
Boolean additionalPropertiesBoolean = getBoolean("additionalProperties", node, false, location, result);
@@ -2576,7 +2590,7 @@ at the moment path passed as string (basePath) from upper components can be both
25762590
? getSchema(additionalPropertiesObject, location, result)
25772591
: additionalPropertiesBoolean;
25782592

2579-
if (additionalProperties != null) {
2593+
if (additionalProperties != null && result.isDefaultSchemaTypeObject()) {
25802594
if (schema == null) {
25812595
schema =
25822596
additionalProperties.equals(Boolean.FALSE)
@@ -2801,7 +2815,7 @@ at the moment path passed as string (basePath) from upper components can be both
28012815
}
28022816

28032817
//sets default value according to the schema type
2804-
if (node.get("default") != null) {
2818+
if (node.get("default") != null && result.isDefaultSchemaTypeObject()) {
28052819
if (!StringUtils.isBlank(schema.getType())) {
28062820
if (schema.getType().equals("array")) {
28072821
ArrayNode array = getArray("default", node, false, location, result);
@@ -2840,6 +2854,13 @@ at the moment path passed as string (basePath) from upper components can be both
28402854
}
28412855
}
28422856
}
2857+
}else{
2858+
schema.setDefault(null);
2859+
}
2860+
2861+
bool = getBoolean("nullable", node, false, location, result);
2862+
if (bool != null) {
2863+
schema.setNullable(bool);
28432864
}
28442865

28452866
bool = getBoolean("readOnly", node, false, location, result);
@@ -2875,7 +2896,7 @@ at the moment path passed as string (basePath) from upper components can be both
28752896
}
28762897
}
28772898

2878-
Object example = getAnyExample("example", node, location, result);
2899+
Object example = getAnyType("example", node, location, result);
28792900
if (example != null) {
28802901
schema.setExample(example instanceof NullNode ? null : example);
28812902
}
@@ -3083,7 +3104,7 @@ public Example getExample(ObjectNode node, String location, ParseResult result)
30833104
example.setDescription(value);
30843105
}
30853106

3086-
Object sample = getAnyExample("value", node, location, result);
3107+
Object sample = getAnyType("value", node, location, result);
30873108
if (sample != null) {
30883109
example.setValue(sample instanceof NullNode ? null : sample);
30893110
}
@@ -3586,8 +3607,19 @@ public Schema getJsonSchema(ObjectNode node, String location, ParseResult result
35863607
schema = composedSchema;
35873608
}
35883609
}
3589-
3590-
if (itemsNode != null) {
3610+
if (itemsNode != null && result.isDefaultSchemaTypeObject()) {
3611+
ArraySchema items = new ArraySchema();
3612+
if (itemsNode.getNodeType().equals(JsonNodeType.OBJECT)) {
3613+
items.setItems(getSchema(itemsNode, location, result));
3614+
} else if (itemsNode.getNodeType().equals(JsonNodeType.ARRAY)) {
3615+
for (JsonNode n : itemsNode) {
3616+
if (n.isValueNode()) {
3617+
items.setItems(getSchema(itemsNode, location, result));
3618+
}
3619+
}
3620+
}
3621+
schema = items;
3622+
}else if (itemsNode != null) {
35913623
JsonSchema items = new JsonSchema();
35923624
if (itemsNode.getNodeType().equals(JsonNodeType.OBJECT)) {
35933625
items.setItems(getJsonSchema(itemsNode, location, result));
@@ -3613,7 +3645,16 @@ public Schema getJsonSchema(ObjectNode node, String location, ParseResult result
36133645
? getJsonSchema(additionalPropertiesObject, location, result)
36143646
: additionalPropertiesBoolean;
36153647

3616-
if (additionalProperties != null) {
3648+
3649+
if (additionalProperties != null && result.isDefaultSchemaTypeObject()) {
3650+
if (schema == null) {
3651+
schema =
3652+
additionalProperties.equals(Boolean.FALSE)
3653+
? new ObjectSchema()
3654+
: new MapSchema();
3655+
}
3656+
schema.setAdditionalProperties(additionalProperties);
3657+
}else if (additionalProperties != null) {
36173658
if (schema == null) {
36183659
schema = new JsonSchema();
36193660
}
@@ -3689,7 +3730,9 @@ public Schema getJsonSchema(ObjectNode node, String location, ParseResult result
36893730

36903731
if (node.get("default") != null) {
36913732
//TODO rename method
3692-
schema.setDefault(getAnyExample("default",node,location,result));
3733+
if(result.isDefaultSchemaTypeObject()) {
3734+
schema.setDefault(getAnyType("default", node, location, result));
3735+
}
36933736
}
36943737

36953738
ObjectNode discriminatorNode = getObject("discriminator", node, false, location, result);
@@ -4074,7 +4117,7 @@ public Schema getJsonSchema(ObjectNode node, String location, ParseResult result
40744117
schema.setExamples(exampleList);
40754118
}
40764119

4077-
Object example = getAnyExample("example", node, location, result);
4120+
Object example = getAnyType("example", node, location, result);
40784121
if (example != null) {
40794122
schema.setExample(example instanceof NullNode ? null : example);
40804123
}
@@ -4131,10 +4174,23 @@ public static class ParseResult {
41314174
private List<Location> unique = new ArrayList<>();
41324175
private List<Location> uniqueTags = new ArrayList<>();
41334176
private List<Location> reserved = new ArrayList<>();
4134-
4177+
private boolean defaultSchemaTypeObject = true;
41354178
private boolean openapi31 = false;
41364179
private boolean oaiAuthor = false;
41374180

4181+
public boolean isDefaultSchemaTypeObject() {
4182+
return defaultSchemaTypeObject;
4183+
}
4184+
4185+
public void setDefaultSchemaTypeObject(boolean defaultSchemaTypeObject) {
4186+
this.defaultSchemaTypeObject = defaultSchemaTypeObject;
4187+
}
4188+
4189+
public ParseResult defaultSchemaTypeObject(boolean defaultSchemaTypeObject) {
4190+
this.defaultSchemaTypeObject = defaultSchemaTypeObject;
4191+
return this;
4192+
}
4193+
41384194
public ParseResult() {
41394195
}
41404196

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)