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 4868ce9ef2..28e82cd905 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 @@ -231,6 +231,65 @@ public void testDeserializeExampleFlag() { assertNull(openAPI.getComponents().getSchemas().get("TestNumberMissing").getExample()); } + @Test + public void testExampleFlag() { + OpenAPIV3Parser openApiParser = new OpenAPIV3Parser(); + ParseOptions options = new ParseOptions(); + options.setResolve(true); + options.setResolveCombinators(true); + options.setResolveFully(true); + options.setFlatten(true); + SwaggerParseResult parseResult = openApiParser.readLocation("media-type-null-example.yaml", null, options); + + OpenAPI openAPI = parseResult.getOpenAPI(); + + assertNull(openAPI.getPaths().get("/pets/{petId}").getGet().getResponses().get("200").getContent().get("application/json").getExample()); + assertTrue(openAPI.getPaths().get("/pets/{petId}").getGet().getResponses().get("200").getContent().get("application/json").getExampleSetFlag()); + + assertNull(openAPI.getPaths().get("/pet").getPost().getResponses().get("200").getContent().get("application/json").getExample()); + assertFalse(openAPI.getPaths().get("/pet").getPost().getResponses().get("200").getContent().get("application/json").getExampleSetFlag()); + + assertNotNull(openAPI.getPaths().get("/pet").getPost().getRequestBody().getContent().get("application/json").getExample()); + assertNotNull(openAPI.getPaths().get("/pet").getPost().getRequestBody().getContent().get("application/json").getExample()); + + assertTrue(openAPI.getPaths().get("/pet").getPost().getRequestBody().getContent().get("application/json").getExampleSetFlag()); + + assertNotNull(openAPI.getPaths().get("/object-with-null-example").getGet().getResponses().get("200").getContent().get("application/json").getExamples().get("foo").getValue()); + assertTrue(openAPI.getPaths().get("/object-with-null-example").getGet().getResponses().get("200").getContent().get("application/json").getExamples().get("foo").getValueSetFlag()); + assertNull(openAPI.getPaths().get("/object-with-null-example").getGet().getResponses().get("200").getContent().get("application/json").getExamples().get("bar").getValue()); + assertTrue(openAPI.getPaths().get("/object-with-null-example").getGet().getResponses().get("200").getContent().get("application/json").getExamples().get("bar").getValueSetFlag()); + + assertNotNull(openAPI.getPaths().get("/object-with-null-in-schema-example").getGet().getResponses().get("200").getContent().get("application/json").getExamples().get("a").getValue()); + assertTrue(openAPI.getPaths().get("/object-with-null-in-schema-example").getGet().getResponses().get("200").getContent().get("application/json").getExamples().get("a").getValueSetFlag()); + assertNotNull(openAPI.getPaths().get("/object-with-null-in-schema-example").getGet().getResponses().get("200").getContent().get("application/json").getExamples().get("b").getValue()); + assertTrue(openAPI.getPaths().get("/object-with-null-in-schema-example").getGet().getResponses().get("200").getContent().get("application/json").getExamples().get("b").getValueSetFlag()); + assertNotNull(openAPI.getPaths().get("/object-with-null-in-schema-example").getGet().getResponses().get("200").getContent().get("application/json").getExamples().get("c").getValue()); + assertTrue(openAPI.getPaths().get("/object-with-null-in-schema-example").getGet().getResponses().get("200").getContent().get("application/json").getExamples().get("c").getValueSetFlag()); + assertNull(openAPI.getPaths().get("/object-with-null-in-schema-example").getGet().getResponses().get("200").getContent().get("application/json").getExamples().get("d").getValue()); + assertTrue(openAPI.getPaths().get("/object-with-null-in-schema-example").getGet().getResponses().get("200").getContent().get("application/json").getExamples().get("d").getValueSetFlag()); + + + assertNull(openAPI.getComponents().getSchemas().get("ObjectWithNullExample").getExample()); + assertTrue(openAPI.getComponents().getSchemas().get("ObjectWithNullExample").getExampleSetFlag()); + + assertNotNull(openAPI.getComponents().getSchemas().get("ObjectWithNullInSchemaExample").getExample()); + assertTrue(openAPI.getComponents().getSchemas().get("ObjectWithNullInSchemaExample").getExampleSetFlag()); + + assertNotNull(((Schema)openAPI.getComponents().getSchemas().get("ObjectWithNullPropertyExample").getProperties().get("a")).getExample()); + assertTrue(((Schema)openAPI.getComponents().getSchemas().get("ObjectWithNullPropertyExample").getProperties().get("a")).getExampleSetFlag()); + assertNull(((Schema)openAPI.getComponents().getSchemas().get("ObjectWithNullPropertyExample").getProperties().get("b")).getExample()); + assertTrue(((Schema)openAPI.getComponents().getSchemas().get("ObjectWithNullPropertyExample").getProperties().get("b")).getExampleSetFlag()); + + assertNull(openAPI.getComponents().getSchemas().get("StringWithNullExample").getExample()); + assertTrue(openAPI.getComponents().getSchemas().get("StringWithNullExample").getExampleSetFlag()); + + assertNull(openAPI.getComponents().getSchemas().get("ArrayWithNullArrayExample").getExample()); + assertTrue(openAPI.getComponents().getSchemas().get("ArrayWithNullArrayExample").getExampleSetFlag()); + + assertNull(((ArraySchema)openAPI.getComponents().getSchemas().get("ArrayWithNullItemExample")).getItems().getExample()); + assertTrue(((ArraySchema)openAPI.getComponents().getSchemas().get("ArrayWithNullItemExample")).getItems().getExampleSetFlag()); + } + @Test public void testIssueFlattenAdditionalPropertiesSchemaInlineModelTrue() { OpenAPIV3Parser openApiParser = new OpenAPIV3Parser(); diff --git a/modules/swagger-parser-v3/src/test/resources/media-type-null-example.yaml b/modules/swagger-parser-v3/src/test/resources/media-type-null-example.yaml new file mode 100644 index 0000000000..1bf3f9dc8a --- /dev/null +++ b/modules/swagger-parser-v3/src/test/resources/media-type-null-example.yaml @@ -0,0 +1,212 @@ +openapi: "3.0.1" +info: + version: 1.0.0 + title: Swagger Petstore + license: + name: MIT +servers: + - url: https://petstore3.swagger.io/api/v3 +paths: + /pet: + post: + tags: + - pet + summary: Add a new pet to the store + operationId: addPet + requestBody: + description: Pet object that needs to be added to the store + content: + application/json: + schema: + $ref: '#/components/schemas/Pet' + example: + id: 10 + name: kitty + tag: something + required: true + responses: + 200: + description: Expected response to a valid request + content: + application/json: + schema: + $ref: "#/components/schemas/Pet" + /pets/{petId}: + get: + summary: Info for a specific pet + operationId: showPetById + tags: + - pets + parameters: + - name: petId + in: path + required: true + description: The id of the pet to retrieve + schema: + type: string + responses: + 200: + description: Expected response to a valid request + content: + application/json: + schema: + $ref: "#/components/schemas/Pet" + example: null + /object-with-null-example: + get: + description: '' + responses: + '200': + description: '' + content: + application/json: + schema: + $ref: '#/components/schemas/ObjectWithNullExample' + examples: + foo: + value: '{"foo": "bar"}' + bar: + value: null + + /object-with-null-in-schema-example: + get: + description: '' + responses: + '200': + description: '' + content: + application/json: + schema: + $ref: '#/components/schemas/ObjectWithNullInSchemaExample' + examples: + a: + value: 5 + b: + value: 'test' + c: + value: true + d: + value: null + + + /object-with-null-property-example: + get: + description: '' + responses: + '200': + description: '' + content: + application/json: + schema: + $ref: '#/components/schemas/ObjectWithNullPropertyExample' + + /string-with-null-example: + get: + description: '' + responses: + '200': + description: '' + content: + application/json: + schema: + $ref: '#/components/schemas/StringWithNullExample' + + /array-with-null-array-example: + get: + description: '' + responses: + '200': + description: '' + content: + application/json: + schema: + $ref: '#/components/schemas/ArrayWithNullArrayExample' + + /array-with-null-item-example: + get: + description: '' + responses: + '200': + description: '' + content: + application/json: + schema: + $ref: '#/components/schemas/ArrayWithNullItemExample' + + /arrey-with-null-in-array-example: + get: + description: '' + responses: + '200': + description: '' + content: + application/json: + schema: + $ref: '#/components/schemas/ArrayWithNullInArrayExample' +components: + schemas: + Pet: + required: + - id + - name + properties: + id: + type: integer + format: int64 + name: + type: string + tag: + type: string + + 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] diff --git a/pom.xml b/pom.xml index 7edf038d42..f89801ca8a 100644 --- a/pom.xml +++ b/pom.xml @@ -369,7 +369,7 @@ 1.0.55 2.6 1.7.30 - 2.1.7 + 2.1.10-SNAPSHOT 1.6.2 4.13.1 6.14.2