Skip to content

deserialize into MapSchema for bool additionalProperties #1082

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

Merged
merged 1 commit into from
Apr 24, 2019
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.swagger.v3.oas.models.media.Encoding;
import io.swagger.v3.oas.models.media.MapSchema;
import io.swagger.v3.oas.models.media.MediaType;
import io.swagger.v3.oas.models.media.ObjectSchema;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.XML;
import io.swagger.v3.oas.models.security.OAuthFlow;
Expand All @@ -50,9 +51,7 @@
import io.swagger.v3.oas.models.servers.ServerVariables;
import io.swagger.v3.parser.core.models.SwaggerParseResult;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.core.util.RefUtils;

import io.swagger.v3.parser.models.RefFormat;
import org.apache.commons.lang3.StringUtils;

import static io.swagger.v3.core.util.RefUtils.extractSimpleName;
Expand Down Expand Up @@ -2034,6 +2033,7 @@ public Schema getSchema(ObjectNode node, String location, ParseResult result){
ArrayNode anyOfArray = getArray("anyOf", node, false, location, result);
ObjectNode itemsNode = getObject("items", node, false, location, result);
ObjectNode additionalPropertiesNode = getObject("additionalProperties", node, false, location, result);
Boolean additionalPropertiesBoolean = getBoolean("additionalProperties", node, false, location, result);


if((allOfArray != null )||(anyOfArray != null)|| (oneOfArray != null)) {
Expand Down Expand Up @@ -2085,27 +2085,28 @@ public Schema getSchema(ObjectNode node, String location, ParseResult result){
schema = items;
}

if(additionalPropertiesNode != null){
if(additionalPropertiesNode != null) {
MapSchema mapSchema = new MapSchema();
if (additionalPropertiesNode.getNodeType().equals(JsonNodeType.OBJECT)) {
ObjectNode additionalPropertiesObj = getObject("additionalProperties", node, false, location, result);
if (additionalPropertiesObj != null) {
Schema additionalProperties = getSchema(additionalPropertiesObj, location, result);
if (additionalProperties != null) {
mapSchema.setAdditionalProperties(additionalProperties);
schema = mapSchema;
}
}
} else if (additionalPropertiesNode.getNodeType().equals(JsonNodeType.BOOLEAN)) {
Boolean additionalProperties = getBoolean("additionalProperties", node, false, location, result);
if (additionalProperties != null) {
if (additionalProperties == true) {
mapSchema.setAdditionalProperties(additionalProperties);
}else{
schema.setAdditionalProperties(additionalProperties);
}
}
}
schema = mapSchema;
} else if(additionalPropertiesBoolean != null){
MapSchema mapSchema = new MapSchema();
if (additionalPropertiesBoolean) {
mapSchema.setAdditionalProperties(additionalPropertiesBoolean);
schema = mapSchema;
}else{
ObjectSchema objectSchema = new ObjectSchema();
objectSchema.setAdditionalProperties(additionalPropertiesBoolean);
schema = objectSchema;
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,42 @@ public void testIssue1071() {
OpenAPI apispec = parseResult.getOpenAPI();
assertNotNull(apispec);
Schema test = apispec.getPaths().get("/mapschema").getGet().getResponses().get("200").getContent().get("application/json").getSchema();
System.out.println(test.getClass());
assertTrue(test instanceof MapSchema);

}

@Test
public void testIssue1071True() {

ParseOptions options = new ParseOptions();
options.setResolve(true);

SwaggerParseResult parseResult = new OpenAPIV3Parser().readLocation("issue-1071-true.yaml", null, options);
OpenAPI apispec = parseResult.getOpenAPI();
assertNotNull(apispec);
Schema test = apispec.getPaths().get("/mapschema").getGet().getResponses().get("200").getContent().get("application/json").getSchema();
assertTrue(test instanceof MapSchema);
assertTrue(test.getAdditionalProperties() instanceof Boolean);
assertTrue((Boolean)test.getAdditionalProperties());

}

@Test
public void testIssue1071False() {

ParseOptions options = new ParseOptions();
options.setResolve(true);

SwaggerParseResult parseResult = new OpenAPIV3Parser().readLocation("issue-1071-false.yaml", null, options);
OpenAPI apispec = parseResult.getOpenAPI();
assertNotNull(apispec);
Schema test = apispec.getPaths().get("/mapschema").getGet().getResponses().get("200").getContent().get("application/json").getSchema();
assertTrue(test instanceof ObjectSchema);
assertTrue(test.getAdditionalProperties() instanceof Boolean);
assertFalse((Boolean)test.getAdditionalProperties());

}

@Test
public void testIssue1039() {

Expand Down
28 changes: 28 additions & 0 deletions modules/swagger-parser-v3/src/test/resources/issue-1071-false.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
openapi: "3.0.0"
info:
version: 1.0.0
title: Swagger Petstore
description: A sample API that uses a petstore as an example to demonstrate features in the OpenAPI 3.0 specification
termsOfService: http://swagger.io/terms/
contact:
name: Swagger API Team
email: [email protected]
url: http://swagger.io
license:
name: Apache 2.0
url: https://www.apache.org/licenses/LICENSE-2.0.html
servers:
- url: http://petstore.swagger.io/api
paths:
'/mapschema':
get:
description: MapSchema
operationId: MapSchema
responses:
'200':
description: Successful response.
content:
application/json:
schema:
type: object
additionalProperties: false
28 changes: 28 additions & 0 deletions modules/swagger-parser-v3/src/test/resources/issue-1071-true.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
openapi: "3.0.0"
info:
version: 1.0.0
title: Swagger Petstore
description: A sample API that uses a petstore as an example to demonstrate features in the OpenAPI 3.0 specification
termsOfService: http://swagger.io/terms/
contact:
name: Swagger API Team
email: [email protected]
url: http://swagger.io
license:
name: Apache 2.0
url: https://www.apache.org/licenses/LICENSE-2.0.html
servers:
- url: http://petstore.swagger.io/api
paths:
'/mapschema':
get:
description: MapSchema
operationId: MapSchema
responses:
'200':
description: Successful response.
content:
application/json:
schema:
type: object
additionalProperties: true
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@
<swagger-parser-v2-version>1.0.45-SNAPSHOT</swagger-parser-v2-version>
<commons-io-version>2.4</commons-io-version>
<slf4j-version>1.6.3</slf4j-version>
<swagger-core-version>2.0.7</swagger-core-version>
<swagger-core-version>2.0.8-SNAPSHOT</swagger-core-version>
<junit-version>4.8.1</junit-version>
<testng-version>6.14.2</testng-version>
<jmockit-version>1.35</jmockit-version>
Expand Down