Skip to content

Commit 901d041

Browse files
committed
issue-2125: fix deserialization messages for schemas default values when nullable is set
1 parent b7768de commit 901d041

File tree

8 files changed

+118
-12
lines changed

8 files changed

+118
-12
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public SwaggerParseResult readContents(String swaggerAsString, List<Authorizatio
176176
SwaggerParseResult result;
177177
if (options != null) {
178178
result = parseJsonNode(location, rootNode, options);
179-
}else {
179+
} else {
180180
result = parseJsonNode(location, rootNode);
181181
}
182182
if (result.getOpenAPI() != null) {

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

+18-10
Original file line numberDiff line numberDiff line change
@@ -1879,7 +1879,6 @@ public BigDecimal getBigDecimal(String key, ObjectNode node, boolean required, S
18791879
return value;
18801880
}
18811881

1882-
18831882
public Integer getInteger(String key, ObjectNode node, boolean required, String location, ParseResult result) {
18841883
Integer value = null;
18851884
JsonNode v = node.get(key);
@@ -2747,7 +2746,7 @@ at the moment path passed as string (basePath) from upper components can be both
27472746
}
27482747
}
27492748
schema = items;
2750-
}else if (itemsNode != null){
2749+
} else if (itemsNode != null) {
27512750
Schema items = new Schema();
27522751
if (itemsNode.getNodeType().equals(JsonNodeType.OBJECT)) {
27532752
items.setItems(getSchema(itemsNode, location, result));
@@ -2905,7 +2904,7 @@ at the moment path passed as string (basePath) from upper components can be both
29052904

29062905
Map<String, Schema> properties = new LinkedHashMap<>();
29072906
ObjectNode propertiesObj = getObject("properties", node, false, location, result);
2908-
Schema property = null;
2907+
Schema property;
29092908

29102909
Set<String> keys = getKeys(propertiesObj);
29112910
for (String name : keys) {
@@ -2925,10 +2924,24 @@ at the moment path passed as string (basePath) from upper components can be both
29252924
schema.setProperties(properties);
29262925
}
29272926

2927+
bool = getBoolean("nullable", node, false, location, result);
2928+
if (bool != null) {
2929+
schema.setNullable(bool);
2930+
}
2931+
29282932
//sets default value according to the schema type
29292933
if (node.get("default") != null && result.isInferSchemaType()) {
2934+
boolean nullable = schema.getNullable() == null || schema.getNullable();
2935+
boolean isDefaultNodeTypeNull = node.get("default") != null && node.get("default").isNull();
29302936
if (!StringUtils.isBlank(schema.getType())) {
2931-
if (schema.getType().equals("array")) {
2937+
if (isDefaultNodeTypeNull) {
2938+
if (nullable) {
2939+
schema.setDefault(null);
2940+
} else {
2941+
String expectedType = String.format("non-null %s", schema.getType());
2942+
result.invalidType(location, "default", expectedType, node);
2943+
}
2944+
} else if (schema.getType().equals("array")) {
29322945
ArrayNode array = getArray("default", node, false, location, result);
29332946
if (array != null) {
29342947
schema.setDefault(array);
@@ -2975,15 +2988,10 @@ at the moment path passed as string (basePath) from upper components can be both
29752988
if (defaultObject != null) {
29762989
schema.setDefault(defaultObject);
29772990
}
2978-
}else{
2991+
} else {
29792992
schema.setDefault(null);
29802993
}
29812994

2982-
bool = getBoolean("nullable", node, false, location, result);
2983-
if (bool != null) {
2984-
schema.setNullable(bool);
2985-
}
2986-
29872995
Map<String, Object> extensions = getExtensions(node);
29882996
if (extensions != null && extensions.size() > 0) {
29892997
schema.setExtensions(extensions);

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

+29-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import io.swagger.v3.oas.models.media.ComposedSchema;
1212
import io.swagger.v3.oas.models.media.Content;
1313
import io.swagger.v3.oas.models.media.MediaType;
14-
import io.swagger.v3.oas.models.media.ObjectSchema;
1514
import io.swagger.v3.oas.models.media.StringSchema;
1615
import io.swagger.v3.oas.models.parameters.RequestBody;
1716
import io.swagger.v3.oas.models.responses.ApiResponse;
@@ -137,4 +136,33 @@ public void testDeserializeYamlDefinitionMissingSchema_Issue1951() throws Except
137136
assertTrue(result.getMessages().contains("attribute components.responses.ErrorObj.content.'application/json'.schema.NotAddedYet is missing"));
138137
assertTrue(result.getMessages().contains("attribute paths.'/thingy'(post).requestBody.content.'application/json'.schema.#/components/schemas/ThingRequest is missing"));
139138
}
139+
140+
@Test
141+
public void testDeserializeSchemaWithDefaultProperty() {
142+
SwaggerParseResult result = new OpenAPIV3Parser().readLocation("/schemas-default-value/default.yaml", null, null);
143+
assertEquals(result.getMessages().size(),0);
144+
assertNotNull(result.getOpenAPI());
145+
}
146+
147+
@Test
148+
public void testDeserializeSchemaWithNullDefaultProperty() {
149+
SwaggerParseResult result = new OpenAPIV3Parser().readLocation("/schemas-default-value/defaultNull.yaml", null, null);
150+
assertEquals(result.getMessages().size(),0);
151+
assertNotNull(result.getOpenAPI());
152+
}
153+
154+
@Test
155+
public void testDeserializeSchemaWithNullDefaultAndNullableTrueProperty() {
156+
SwaggerParseResult result = new OpenAPIV3Parser().readLocation("/schemas-default-value/defaultNullAndNullableTrue.yaml", null, null);
157+
assertEquals(result.getMessages().size(),0);
158+
assertNotNull(result.getOpenAPI());
159+
}
160+
161+
@Test
162+
public void testDeserializeSchemaWithNullDefaultAndNullableFalseProperty() {
163+
SwaggerParseResult result = new OpenAPIV3Parser().readLocation("/schemas-default-value/defaultNullAndNullableFalse.yaml", null, null);
164+
assertEquals(result.getMessages().size(),1);
165+
assertEquals(result.getMessages().get(0), "attribute components.schemas.Test.default is not of type `non-null string`");
166+
assertNotNull(result.getOpenAPI());
167+
}
140168
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
openapi: '3.0.0'
2+
3+
info:
4+
title: Test
5+
version: 0.1.0
6+
7+
paths: {}
8+
9+
components:
10+
schemas:
11+
Test:
12+
type: string
13+
items:
14+
type: string
15+
default: null
16+
nullable: false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
openapi: '3.0.0'
2+
3+
info:
4+
title: Test
5+
version: 0.1.0
6+
7+
paths: {}
8+
9+
components:
10+
schemas:
11+
Test:
12+
type: string
13+
default: string
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
openapi: '3.0.0'
2+
3+
info:
4+
title: Test
5+
version: 0.1.0
6+
7+
paths: {}
8+
9+
components:
10+
schemas:
11+
Test:
12+
type: string
13+
default: null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
openapi: '3.0.0'
2+
3+
info:
4+
title: Test
5+
version: 0.1.0
6+
7+
paths: {}
8+
9+
components:
10+
schemas:
11+
Test:
12+
type: string
13+
default: null
14+
nullable: false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
openapi: '3.0.0'
2+
3+
info:
4+
title: Test
5+
version: 0.1.0
6+
7+
paths: {}
8+
9+
components:
10+
schemas:
11+
Test:
12+
type: string
13+
default: null
14+
nullable: true

0 commit comments

Comments
 (0)