diff --git a/pom.xml b/pom.xml index dbf39aa2..b0309af1 100644 --- a/pom.xml +++ b/pom.xml @@ -460,8 +460,8 @@ 3.0.1 - 2.1.3 - 2.0.20 + 2.1.4-SNAPSHOT + 2.0.21-SNAPSHOT 2.11.1 9.4.11.v20180605 2.26 diff --git a/src/main/java/io/swagger/oas/inflector/examples/ExampleBuilder.java b/src/main/java/io/swagger/oas/inflector/examples/ExampleBuilder.java index 4fc1b738..24120934 100644 --- a/src/main/java/io/swagger/oas/inflector/examples/ExampleBuilder.java +++ b/src/main/java/io/swagger/oas/inflector/examples/ExampleBuilder.java @@ -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(); } } diff --git a/src/test/java/io/swagger/oas/test/examples/ExampleBuilderTest.java b/src/test/java/io/swagger/oas/test/examples/ExampleBuilderTest.java index 15699c84..1ca75264 100644 --- a/src/test/java/io/swagger/oas/test/examples/ExampleBuilderTest.java +++ b/src/test/java/io/swagger/oas/test/examples/ExampleBuilderTest.java @@ -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 + " ]"); + } } diff --git a/src/test/swagger/null-examples-oas3-no-flag.yaml b/src/test/swagger/null-examples-oas3-no-flag.yaml new file mode 100644 index 00000000..8574ec73 --- /dev/null +++ b/src/test/swagger/null-examples-oas3-no-flag.yaml @@ -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]