diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java index 59175dffcf..87834e1f89 100644 --- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java +++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java @@ -3,6 +3,7 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.JsonNodeType; +import com.fasterxml.jackson.databind.node.NullNode; import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.TextNode; import io.swagger.v3.oas.models.Components; @@ -997,7 +998,7 @@ public MediaType getMediaType(ObjectNode contentNode, String location, ParseResu Object example = getAnyExample("example",contentNode, location,result); if (example != null){ - mediaType.setExample(example); + mediaType.setExample(example instanceof NullNode ? null : example); } @@ -1579,7 +1580,7 @@ public Parameter getParameter(ObjectNode obj, String location, ParseResult resul Object example = getAnyExample("example", obj, location,result); if (example != null){ - parameter.setExample(example); + parameter.setExample(example instanceof NullNode ? null : example); } Boolean allowReserved = getBoolean("allowReserved", obj, false, location, result); @@ -1699,7 +1700,7 @@ public Header getHeader(ObjectNode headerNode, String location, ParseResult resu Object example = getAnyExample("example", headerNode, location,result); if (example != null){ - header.setExample(example); + header.setExample(example instanceof NullNode ? null : example); } ObjectNode contentNode = getObject("content",headerNode,false,location,result); @@ -1753,6 +1754,8 @@ public Object getAnyExample(String nodeKey,ObjectNode node, String location, Par if (bool != null){ return bool; } + } else if (example.getNodeType().equals(JsonNodeType.NULL)){ + return example; } } return null; @@ -2415,8 +2418,8 @@ public Schema getSchema(ObjectNode node, String location, ParseResult result){ } Object example = getAnyExample("example", node, location,result); - if (example != null){ - schema.setExample(example); + if (example != null ){ + schema.setExample(example instanceof NullNode ? null : example); } bool = getBoolean("deprecated", node, false, location, result); @@ -2614,7 +2617,7 @@ public Example getExample(ObjectNode node, String location, ParseResult result) Object sample = getAnyExample("value", node, location,result); if (sample != null){ - example.setValue(sample); + example.setValue(sample instanceof NullNode ? null : sample); } diff --git a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java index 79c073da22..b4d19c8519 100644 --- a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java +++ b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java @@ -96,6 +96,31 @@ public void testIssue1367() { assertTrue(((Schema)openAPI.getComponents().getSchemas().get("TestDTO").getProperties().get("choice")).getEnum() != null); } + @Test + public void testDeserializeExampleFlag() { + OpenAPIV3Parser openApiParser = new OpenAPIV3Parser(); + ParseOptions options = new ParseOptions(); + options.setResolve(true); + options.setResolveCombinators(true); + options.setResolveFully(true); + options.setFlatten(true); + SwaggerParseResult parseResult = openApiParser.readLocation("exampleFlag.yaml", null, options); + OpenAPI openAPI = parseResult.getOpenAPI(); + assertTrue(openAPI.getComponents().getSchemas().get("TestDTO").getExampleSetFlag()); + assertNull(openAPI.getComponents().getSchemas().get("TestDTO").getExample()); + assertTrue(openAPI.getComponents().getSchemas().get("TestString").getExampleSetFlag()); + assertNull(openAPI.getComponents().getSchemas().get("TestString").getExample()); + assertTrue(openAPI.getComponents().getSchemas().get("TestNumber").getExampleSetFlag()); + assertNull(openAPI.getComponents().getSchemas().get("TestNumber").getExample()); + + assertFalse(openAPI.getComponents().getSchemas().get("TestDTOMissing").getExampleSetFlag()); + assertNull(openAPI.getComponents().getSchemas().get("TestDTOMissing").getExample()); + assertFalse(openAPI.getComponents().getSchemas().get("TestStringMissing").getExampleSetFlag()); + assertNull(openAPI.getComponents().getSchemas().get("TestStringMissing").getExample()); + assertFalse(openAPI.getComponents().getSchemas().get("TestNumberMissing").getExampleSetFlag()); + assertNull(openAPI.getComponents().getSchemas().get("TestNumberMissing").getExample()); + } + @Test public void testIssueFlattenAdditionalPropertiesSchemaInlineModelTrue() { OpenAPIV3Parser openApiParser = new OpenAPIV3Parser(); @@ -2241,7 +2266,7 @@ public void shouldParseApiWithMultipleParameterReferences() { assertThat(parameters.keySet(), equalTo(new HashSet<>(asList("IdParam", "NameParam")))); assertThat(parameters.get("IdParam").getName(), equalTo("id")); assertThat(parameters.get("NameParam").getName(), equalTo("name")); - + assertThat(result.getMessages(), equalTo(emptyList())); } @@ -2250,7 +2275,7 @@ public void shouldParseApiWithMultipleParameterReferences() { public void shouldParseApiWithParametersUsingContentvsSchema() { // Tests that the content method of specifying the format of a parameter // gets resolved. - // Test checks if an API's single parameter of array type gets fully resolved to + // Test checks if an API's single parameter of array type gets fully resolved to // referenced definitions. String location = "src/test/resources/issue-1078/api.yaml"; ParseOptions options = new ParseOptions(); @@ -2258,14 +2283,14 @@ public void shouldParseApiWithParametersUsingContentvsSchema() { // This test uses an Array in the parameters, test if it get's fully resolved. options.setResolveFully(true); OpenAPIV3Parser tested = new OpenAPIV3Parser(); - + // Parse yaml SwaggerParseResult result = tested.readLocation(location, emptyList(), options); OpenAPI api = result.getOpenAPI(); Paths paths = api.getPaths(); - // First ensure all schemas were resolved, this is important when this library + // First ensure all schemas were resolved, this is important when this library // is used to generate code Components components = api.getComponents(); assertNotNull(components); @@ -2274,13 +2299,13 @@ public void shouldParseApiWithParametersUsingContentvsSchema() { assertNotNull(components.getSchemas().get("Lat")); assertNotNull(components.getSchemas().get("Long")); assertNotNull(components.getSchemas().get("SearchResult")); - + PathItem apiEndpoint = paths.get("/api-endpoint-1"); List parameters = apiEndpoint.getGet().getParameters(); - + // Ensure there's only one parameter in this test assertThat(parameters.size(), equalTo(1)); - + // We are testing content for a parameter so make sure its there. Content content = parameters.get(0).getContent(); assertNotNull(content); diff --git a/modules/swagger-parser-v3/src/test/resources/exampleFlag.yaml b/modules/swagger-parser-v3/src/test/resources/exampleFlag.yaml new file mode 100644 index 0000000000..f4eb4670fb --- /dev/null +++ b/modules/swagger-parser-v3/src/test/resources/exampleFlag.yaml @@ -0,0 +1,25 @@ +openapi: 3.0.0 +info: + version: 1.0.1 + title: Products API definition for the 4th Platform +paths: + '/TestDTO': + get: + operationId: description +components: + schemas: + TestDTO: + type: object + example: null + TestString: + type: string + example: null + TestNumber: + type: integer + example: null + TestDTOMissing: + type: object + TestStringMissing: + type: string + TestNumberMissing: + type: integer diff --git a/pom.xml b/pom.xml index 8298c54b22..bf3ebcad06 100644 --- a/pom.xml +++ b/pom.xml @@ -288,15 +288,15 @@ 1.0.52-SNAPSHOT 2.6 1.7.30 - 2.1.2 - 1.6.1 + 2.1.4-SNAPSHOT + 1.6.2 4.13 6.14.2 1.35 2.15.0 2.22.2 3.2.1 - 2.10.2 + 2.11.1