Skip to content

Commit

Permalink
Merge pull request #1400 from swagger-api/null-missing
Browse files Browse the repository at this point in the history
process exampleSetFlag to flag deserialization of null example vs missing
  • Loading branch information
frantuma authored Jul 16, 2020
2 parents 991b5f8 + 5404631 commit 6a6760a
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}


Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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()));

}
Expand All @@ -2250,22 +2275,22 @@ 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();
options.setResolve(true);
// 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);
Expand All @@ -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<Parameter> 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);
Expand Down
25 changes: 25 additions & 0 deletions modules/swagger-parser-v3/src/test/resources/exampleFlag.yaml
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -288,15 +288,15 @@
<swagger-parser-v2-version>1.0.52-SNAPSHOT</swagger-parser-v2-version>
<commons-io-version>2.6</commons-io-version>
<slf4j-version>1.7.30</slf4j-version>
<swagger-core-version>2.1.2</swagger-core-version>
<swagger-core-v2-version>1.6.1</swagger-core-v2-version>
<swagger-core-version>2.1.4-SNAPSHOT</swagger-core-version>
<swagger-core-v2-version>1.6.2</swagger-core-v2-version>
<junit-version>4.13</junit-version>
<testng-version>6.14.2</testng-version>
<jmockit-version>1.35</jmockit-version>
<wiremock-version>2.15.0</wiremock-version>
<surefire-version>2.22.2</surefire-version>
<commons-lang-version>3.2.1</commons-lang-version>
<jackson-version>2.10.2</jackson-version>
<jackson-version>2.11.1</jackson-version>
</properties>

</project>

0 comments on commit 6a6760a

Please sign in to comment.