Skip to content
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

refs #371 - better null example handling using updated core and parser - also update dependencies #377

Merged
merged 1 commit into from
Jul 16, 2020
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
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 @@ -149,14 +149,15 @@ public static Example fromProperty(

Object example = property.getExample();

if (example == null) {
if (example == null && property.get$ref() == 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]