Skip to content
This repository was archived by the owner on Nov 24, 2022. It is now read-only.

Commit b06bd06

Browse files
authored
Merge pull request swagger-api#1077 from swagger-api/issue-1071
Fix parsing of MapSchema
2 parents ada75b6 + fecc747 commit b06bd06

3 files changed

Lines changed: 71 additions & 20 deletions

File tree

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

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import io.swagger.v3.oas.models.media.DateTimeSchema;
2525
import io.swagger.v3.oas.models.media.Discriminator;
2626
import io.swagger.v3.oas.models.media.Encoding;
27+
import io.swagger.v3.oas.models.media.MapSchema;
2728
import io.swagger.v3.oas.models.media.MediaType;
2829
import io.swagger.v3.oas.models.media.Schema;
2930
import io.swagger.v3.oas.models.media.XML;
@@ -2023,7 +2024,7 @@ public Schema getSchema(ObjectNode node, String location, ParseResult result){
20232024
ArrayNode allOfArray = getArray("allOf", node, false, location, result);
20242025
ArrayNode anyOfArray = getArray("anyOf", node, false, location, result);
20252026
ObjectNode itemsNode = getObject("items", node, false, location, result);
2026-
2027+
ObjectNode additionalPropertiesNode = getObject("additionalProperties", node, false, location, result);
20272028

20282029

20292030
if((allOfArray != null )||(anyOfArray != null)|| (oneOfArray != null)) {
@@ -2075,6 +2076,31 @@ public Schema getSchema(ObjectNode node, String location, ParseResult result){
20752076
schema = items;
20762077
}
20772078

2079+
if(additionalPropertiesNode != null){
2080+
MapSchema mapSchema = new MapSchema();
2081+
if (additionalPropertiesNode.getNodeType().equals(JsonNodeType.OBJECT)) {
2082+
ObjectNode additionalPropertiesObj = getObject("additionalProperties", node, false, location, result);
2083+
if (additionalPropertiesObj != null) {
2084+
Schema additionalProperties = getSchema(additionalPropertiesObj, location, result);
2085+
if (additionalProperties != null) {
2086+
mapSchema.setAdditionalProperties(additionalProperties);
2087+
}
2088+
}
2089+
} else if (additionalPropertiesNode.getNodeType().equals(JsonNodeType.BOOLEAN)) {
2090+
Boolean additionalProperties = getBoolean("additionalProperties", node, false, location, result);
2091+
if (additionalProperties != null) {
2092+
if (additionalProperties == true) {
2093+
mapSchema.setAdditionalProperties(additionalProperties);
2094+
}else{
2095+
schema.setAdditionalProperties(additionalProperties);
2096+
}
2097+
}
2098+
}
2099+
schema = mapSchema;
2100+
}
2101+
2102+
2103+
20782104
if (schema == null){
20792105
schema = SchemaTypeUtil.createSchemaByType(node);
20802106
}
@@ -2267,24 +2293,6 @@ public Schema getSchema(ObjectNode node, String location, ParseResult result){
22672293
schema.setProperties(properties);
22682294
}
22692295

2270-
2271-
if (node.get("additionalProperties") != null) {
2272-
if (node.get("additionalProperties").getNodeType().equals(JsonNodeType.OBJECT)) {
2273-
ObjectNode additionalPropertiesObj = getObject("additionalProperties", node, false, location, result);
2274-
if (additionalPropertiesObj != null) {
2275-
Schema additionalProperties = getSchema(additionalPropertiesObj, location, result);
2276-
if (additionalProperties != null) {
2277-
schema.setAdditionalProperties(additionalProperties);
2278-
}
2279-
}
2280-
} else if (node.get("additionalProperties").getNodeType().equals(JsonNodeType.BOOLEAN)) {
2281-
Boolean additionalProperties = getBoolean("additionalProperties", node, false, location, result);
2282-
if (additionalProperties != null) {
2283-
schema.setAdditionalProperties(additionalProperties);
2284-
}
2285-
2286-
}
2287-
}
22882296
value = getString("description",node,false,location,result);
22892297
if (StringUtils.isNotBlank(value)) {
22902298
schema.setDescription(value);

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,20 @@ public class OpenAPIV3ParserTest {
6565
protected int serverPort = getDynamicPort();
6666
protected WireMockServer wireMockServer;
6767

68+
@Test
69+
public void testIssue1071() {
70+
71+
ParseOptions options = new ParseOptions();
72+
options.setResolve(true);
73+
74+
SwaggerParseResult parseResult = new OpenAPIV3Parser().readLocation("issue-1071.yaml", null, options);
75+
OpenAPI apispec = parseResult.getOpenAPI();
76+
assertNotNull(apispec);
77+
Schema test = apispec.getPaths().get("/mapschema").getGet().getResponses().get("200").getContent().get("application/json").getSchema();
78+
System.out.println(test.getClass());
79+
assertTrue(test instanceof MapSchema);
80+
81+
}
6882

6983
@Test
7084
public void testIssue1039() {
@@ -76,7 +90,7 @@ public void testIssue1039() {
7690
OpenAPI apispec = parseResult.getOpenAPI();
7791
assertNotNull(apispec);
7892
assertEquals(apispec.getPaths().get("/pets").getGet().getParameters().get(0).getSchema().getType(),"array");
79-
93+
8094
}
8195

8296
@Test
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
openapi: "3.0.0"
2+
info:
3+
version: 1.0.0
4+
title: Swagger Petstore
5+
description: A sample API that uses a petstore as an example to demonstrate features in the OpenAPI 3.0 specification
6+
termsOfService: http://swagger.io/terms/
7+
contact:
8+
name: Swagger API Team
9+
email: apiteam@swagger.io
10+
url: http://swagger.io
11+
license:
12+
name: Apache 2.0
13+
url: https://www.apache.org/licenses/LICENSE-2.0.html
14+
servers:
15+
- url: http://petstore.swagger.io/api
16+
paths:
17+
'/mapschema':
18+
get:
19+
description: MapSchema
20+
operationId: MapSchema
21+
responses:
22+
'200':
23+
description: Successful response.
24+
content:
25+
application/json:
26+
schema:
27+
type: object
28+
additionalProperties:
29+
type: string

0 commit comments

Comments
 (0)