Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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]
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@
<swagger-parser-v2-version>1.0.55</swagger-parser-v2-version>
<commons-io-version>2.6</commons-io-version>
<slf4j-version>1.7.30</slf4j-version>
<swagger-core-version>2.1.7</swagger-core-version>
<swagger-core-version>2.1.10-SNAPSHOT</swagger-core-version>
<swagger-core-v2-version>1.6.2</swagger-core-v2-version>
<junit-version>4.13.1</junit-version>
<testng-version>6.14.2</testng-version>
Expand Down