Skip to content

Commit

Permalink
refs #371 - better null example handling using updated core and parser
Browse files Browse the repository at this point in the history
  • Loading branch information
frantuma committed Jul 16, 2020
1 parent 0a13c19 commit c649ea1
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 4 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,8 @@
</dependencies>
<properties>
<maven-javadoc-version>3.0.1</maven-javadoc-version>
<swagger-core-version>2.1.3</swagger-core-version>
<swagger-parser-version>2.0.20</swagger-parser-version>
<swagger-core-version>2.1.4-SNAPSHOT</swagger-core-version>
<swagger-parser-version>2.0.21-SNAPSHOT</swagger-parser-version>
<jackson.version>2.11.1</jackson.version>
<jetty-version>9.4.11.v20180605</jetty-version>
<jersey2-version>2.26</jersey2-version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,12 @@ public static Example fromProperty(
if (example == null) {
if (nullExample) {
return new NullExample();
}
if (processNullExampleExtension) {
} else if (processNullExampleExtension) {
if (property.getExtensions() != null && property.getExtensions().get(Constants.X_INFLECTOR_NULL_EXAMPLE) != null) {
return new NullExample();
}
} else if (property.getExampleSetFlag()) {
return new NullExample();
}
}

Expand Down
57 changes: 57 additions & 0 deletions src/test/java/io/swagger/oas/test/examples/ExampleBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1001,4 +1001,61 @@ public void testNullExampleSupportOAS3() throws Exception{
output = Json.pretty(example);
assertEquals(output, "[ \"foo\", " + null + " ]");
}

@Test
public void testNullExampleSupportOAS3NoFlag() throws Exception{

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

OpenAPI openAPI = new OpenAPIV3Parser().read("src/test/swagger/null-examples-oas3-no-flag.yaml", null, options);

ApiResponse response;
Example example;
String output;

response = openAPI.getPaths().get("/object-with-null-example").getGet().getResponses().get("200");
// returns instance of NullExample
example = ExampleBuilder.fromSchema(response.getContent().get("application/json").getSchema(), null, ExampleBuilder.RequestType.READ);
assertNull(example.asString());

response = openAPI.getPaths().get("/object-with-null-in-schema-example").getGet().getResponses().get("200");
example = ExampleBuilder.fromSchema(response.getContent().get("application/json").getSchema(), null, ExampleBuilder.RequestType.READ);
output = Json.pretty(example);
assertEquals(output, "{\n" +
" \"a\" : 5,\n" +
" \"b\" : \"test\",\n" +
" \"c\" : true,\n" +
" \"d\" : " + null + "\n" +
"}");

response = openAPI.getPaths().get("/object-with-null-property-example").getGet().getResponses().get("200");
example = ExampleBuilder.fromSchema(response.getContent().get("application/json").getSchema(), null, ExampleBuilder.RequestType.READ);
output = Json.pretty(example);
assertEquals(output, "{\n" +
" \"a\" : 5,\n" +
" \"b\" : " + null + "\n" +
"}");

response = openAPI.getPaths().get("/string-with-null-example").getGet().getResponses().get("200");
// returns instance of NullExample
example = ExampleBuilder.fromSchema(response.getContent().get("application/json").getSchema(), null, ExampleBuilder.RequestType.READ);
assertNull(example.asString());

response = openAPI.getPaths().get("/array-with-null-array-example").getGet().getResponses().get("200");
// returns instance of NullExample
example = ExampleBuilder.fromSchema(response.getContent().get("application/json").getSchema(), null, ExampleBuilder.RequestType.READ);
assertNull(example.asString());

response = openAPI.getPaths().get("/array-with-null-item-example").getGet().getResponses().get("200");
example = ExampleBuilder.fromSchema(response.getContent().get("application/json").getSchema(), null, ExampleBuilder.RequestType.READ);
output = Json.pretty(example);
assertEquals(output, "[ " + null + " ]");

response = openAPI.getPaths().get("/array-with-null-in-array-example").getGet().getResponses().get("200");
example = ExampleBuilder.fromSchema(response.getContent().get("application/json").getSchema(), null, ExampleBuilder.RequestType.READ);
output = Json.pretty(example);
assertEquals(output, "[ \"foo\", " + null + " ]");
}
}
138 changes: 138 additions & 0 deletions src/test/swagger/null-examples-oas3-no-flag.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
openapi: 3.0.2
info:
title: null examples
version: 1.0.0
paths:

/object-with-null-example:
get:
description: Response should be `null`
responses:
'200':
description: Should be `null`
content:
application/json:
schema:
$ref: '#/components/schemas/ObjectWithNullExample'

/object-with-null-in-schema-example:
get:
description: 'Response should be `{..., "d": null}`'
responses:
'200':
description: 'Should be `{..., "d": null}`'
content:
application/json:
schema:
$ref: '#/components/schemas/ObjectWithNullInSchemaExample'

/object-with-null-property-example:
get:
description: 'Response should be `{"a": 5, "b": null}`'
responses:
'200':
description: 'Should be `{"a": 5, "b": null}`'
content:
application/json:
schema:
$ref: '#/components/schemas/ObjectWithNullPropertyExample'

/string-with-null-example:
get:
description: Response should be `null`
responses:
'200':
description: Should be `null`
content:
application/json:
schema:
$ref: '#/components/schemas/StringWithNullExample'

/array-with-null-array-example:
get:
description: Response should be `null`
responses:
'200':
description: Should be `null`
content:
application/json:
schema:
$ref: '#/components/schemas/ArrayWithNullArrayExample'

/array-with-null-item-example:
get:
description: Response should be `[null]`
responses:
'200':
description: Should be `[null]`
content:
application/json:
schema:
$ref: '#/components/schemas/ArrayWithNullItemExample'

/array-with-null-in-array-example:
get:
description: Response should be `["foo", null]`
responses:
'200':
description: Should be `["foo", null]`
content:
application/json:
schema:
$ref: '#/components/schemas/ArrayWithNullInArrayExample'

components:
schemas:

ObjectWithNullExample:
type: object
properties:
foo:
type: string
nullable: true
example: null

ObjectWithNullInSchemaExample:
type: object
example:
a: 5
b: test
c: true
d: null

ObjectWithNullPropertyExample:
type: object
properties:
a:
type: integer
example: 5
b:
type: string
nullable: true
example: null

StringWithNullExample:
type: string
nullable: true
example: null

ArrayWithNullArrayExample:
type: array
items:
type: string
nullable: true
example: null

ArrayWithNullItemExample:
type: array
items:
type: string
nullable: true
example: null

ArrayWithNullInArrayExample:
type: array
items:
type: string
nullable: true
example: [foo, null]

0 comments on commit c649ea1

Please sign in to comment.