Skip to content

Commit 95bd55d

Browse files
authored
Merge pull request #1580 from swagger-api/example-flags
bump parser version up and added test to verify exampleFlags checks.
2 parents 67d2aa7 + 9d1685c commit 95bd55d

File tree

3 files changed

+272
-1
lines changed

3 files changed

+272
-1
lines changed

modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,65 @@ public void testDeserializeExampleFlag() {
231231
assertNull(openAPI.getComponents().getSchemas().get("TestNumberMissing").getExample());
232232
}
233233

234+
@Test
235+
public void testExampleFlag() {
236+
OpenAPIV3Parser openApiParser = new OpenAPIV3Parser();
237+
ParseOptions options = new ParseOptions();
238+
options.setResolve(true);
239+
options.setResolveCombinators(true);
240+
options.setResolveFully(true);
241+
options.setFlatten(true);
242+
SwaggerParseResult parseResult = openApiParser.readLocation("media-type-null-example.yaml", null, options);
243+
244+
OpenAPI openAPI = parseResult.getOpenAPI();
245+
246+
assertNull(openAPI.getPaths().get("/pets/{petId}").getGet().getResponses().get("200").getContent().get("application/json").getExample());
247+
assertTrue(openAPI.getPaths().get("/pets/{petId}").getGet().getResponses().get("200").getContent().get("application/json").getExampleSetFlag());
248+
249+
assertNull(openAPI.getPaths().get("/pet").getPost().getResponses().get("200").getContent().get("application/json").getExample());
250+
assertFalse(openAPI.getPaths().get("/pet").getPost().getResponses().get("200").getContent().get("application/json").getExampleSetFlag());
251+
252+
assertNotNull(openAPI.getPaths().get("/pet").getPost().getRequestBody().getContent().get("application/json").getExample());
253+
assertNotNull(openAPI.getPaths().get("/pet").getPost().getRequestBody().getContent().get("application/json").getExample());
254+
255+
assertTrue(openAPI.getPaths().get("/pet").getPost().getRequestBody().getContent().get("application/json").getExampleSetFlag());
256+
257+
assertNotNull(openAPI.getPaths().get("/object-with-null-example").getGet().getResponses().get("200").getContent().get("application/json").getExamples().get("foo").getValue());
258+
assertTrue(openAPI.getPaths().get("/object-with-null-example").getGet().getResponses().get("200").getContent().get("application/json").getExamples().get("foo").getValueSetFlag());
259+
assertNull(openAPI.getPaths().get("/object-with-null-example").getGet().getResponses().get("200").getContent().get("application/json").getExamples().get("bar").getValue());
260+
assertTrue(openAPI.getPaths().get("/object-with-null-example").getGet().getResponses().get("200").getContent().get("application/json").getExamples().get("bar").getValueSetFlag());
261+
262+
assertNotNull(openAPI.getPaths().get("/object-with-null-in-schema-example").getGet().getResponses().get("200").getContent().get("application/json").getExamples().get("a").getValue());
263+
assertTrue(openAPI.getPaths().get("/object-with-null-in-schema-example").getGet().getResponses().get("200").getContent().get("application/json").getExamples().get("a").getValueSetFlag());
264+
assertNotNull(openAPI.getPaths().get("/object-with-null-in-schema-example").getGet().getResponses().get("200").getContent().get("application/json").getExamples().get("b").getValue());
265+
assertTrue(openAPI.getPaths().get("/object-with-null-in-schema-example").getGet().getResponses().get("200").getContent().get("application/json").getExamples().get("b").getValueSetFlag());
266+
assertNotNull(openAPI.getPaths().get("/object-with-null-in-schema-example").getGet().getResponses().get("200").getContent().get("application/json").getExamples().get("c").getValue());
267+
assertTrue(openAPI.getPaths().get("/object-with-null-in-schema-example").getGet().getResponses().get("200").getContent().get("application/json").getExamples().get("c").getValueSetFlag());
268+
assertNull(openAPI.getPaths().get("/object-with-null-in-schema-example").getGet().getResponses().get("200").getContent().get("application/json").getExamples().get("d").getValue());
269+
assertTrue(openAPI.getPaths().get("/object-with-null-in-schema-example").getGet().getResponses().get("200").getContent().get("application/json").getExamples().get("d").getValueSetFlag());
270+
271+
272+
assertNull(openAPI.getComponents().getSchemas().get("ObjectWithNullExample").getExample());
273+
assertTrue(openAPI.getComponents().getSchemas().get("ObjectWithNullExample").getExampleSetFlag());
274+
275+
assertNotNull(openAPI.getComponents().getSchemas().get("ObjectWithNullInSchemaExample").getExample());
276+
assertTrue(openAPI.getComponents().getSchemas().get("ObjectWithNullInSchemaExample").getExampleSetFlag());
277+
278+
assertNotNull(((Schema)openAPI.getComponents().getSchemas().get("ObjectWithNullPropertyExample").getProperties().get("a")).getExample());
279+
assertTrue(((Schema)openAPI.getComponents().getSchemas().get("ObjectWithNullPropertyExample").getProperties().get("a")).getExampleSetFlag());
280+
assertNull(((Schema)openAPI.getComponents().getSchemas().get("ObjectWithNullPropertyExample").getProperties().get("b")).getExample());
281+
assertTrue(((Schema)openAPI.getComponents().getSchemas().get("ObjectWithNullPropertyExample").getProperties().get("b")).getExampleSetFlag());
282+
283+
assertNull(openAPI.getComponents().getSchemas().get("StringWithNullExample").getExample());
284+
assertTrue(openAPI.getComponents().getSchemas().get("StringWithNullExample").getExampleSetFlag());
285+
286+
assertNull(openAPI.getComponents().getSchemas().get("ArrayWithNullArrayExample").getExample());
287+
assertTrue(openAPI.getComponents().getSchemas().get("ArrayWithNullArrayExample").getExampleSetFlag());
288+
289+
assertNull(((ArraySchema)openAPI.getComponents().getSchemas().get("ArrayWithNullItemExample")).getItems().getExample());
290+
assertTrue(((ArraySchema)openAPI.getComponents().getSchemas().get("ArrayWithNullItemExample")).getItems().getExampleSetFlag());
291+
}
292+
234293
@Test
235294
public void testIssueFlattenAdditionalPropertiesSchemaInlineModelTrue() {
236295
OpenAPIV3Parser openApiParser = new OpenAPIV3Parser();
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
openapi: "3.0.1"
2+
info:
3+
version: 1.0.0
4+
title: Swagger Petstore
5+
license:
6+
name: MIT
7+
servers:
8+
- url: https://petstore3.swagger.io/api/v3
9+
paths:
10+
/pet:
11+
post:
12+
tags:
13+
- pet
14+
summary: Add a new pet to the store
15+
operationId: addPet
16+
requestBody:
17+
description: Pet object that needs to be added to the store
18+
content:
19+
application/json:
20+
schema:
21+
$ref: '#/components/schemas/Pet'
22+
example:
23+
id: 10
24+
name: kitty
25+
tag: something
26+
required: true
27+
responses:
28+
200:
29+
description: Expected response to a valid request
30+
content:
31+
application/json:
32+
schema:
33+
$ref: "#/components/schemas/Pet"
34+
/pets/{petId}:
35+
get:
36+
summary: Info for a specific pet
37+
operationId: showPetById
38+
tags:
39+
- pets
40+
parameters:
41+
- name: petId
42+
in: path
43+
required: true
44+
description: The id of the pet to retrieve
45+
schema:
46+
type: string
47+
responses:
48+
200:
49+
description: Expected response to a valid request
50+
content:
51+
application/json:
52+
schema:
53+
$ref: "#/components/schemas/Pet"
54+
example: null
55+
/object-with-null-example:
56+
get:
57+
description: ''
58+
responses:
59+
'200':
60+
description: ''
61+
content:
62+
application/json:
63+
schema:
64+
$ref: '#/components/schemas/ObjectWithNullExample'
65+
examples:
66+
foo:
67+
value: '{"foo": "bar"}'
68+
bar:
69+
value: null
70+
71+
/object-with-null-in-schema-example:
72+
get:
73+
description: ''
74+
responses:
75+
'200':
76+
description: ''
77+
content:
78+
application/json:
79+
schema:
80+
$ref: '#/components/schemas/ObjectWithNullInSchemaExample'
81+
examples:
82+
a:
83+
value: 5
84+
b:
85+
value: 'test'
86+
c:
87+
value: true
88+
d:
89+
value: null
90+
91+
92+
/object-with-null-property-example:
93+
get:
94+
description: ''
95+
responses:
96+
'200':
97+
description: ''
98+
content:
99+
application/json:
100+
schema:
101+
$ref: '#/components/schemas/ObjectWithNullPropertyExample'
102+
103+
/string-with-null-example:
104+
get:
105+
description: ''
106+
responses:
107+
'200':
108+
description: ''
109+
content:
110+
application/json:
111+
schema:
112+
$ref: '#/components/schemas/StringWithNullExample'
113+
114+
/array-with-null-array-example:
115+
get:
116+
description: ''
117+
responses:
118+
'200':
119+
description: ''
120+
content:
121+
application/json:
122+
schema:
123+
$ref: '#/components/schemas/ArrayWithNullArrayExample'
124+
125+
/array-with-null-item-example:
126+
get:
127+
description: ''
128+
responses:
129+
'200':
130+
description: ''
131+
content:
132+
application/json:
133+
schema:
134+
$ref: '#/components/schemas/ArrayWithNullItemExample'
135+
136+
/arrey-with-null-in-array-example:
137+
get:
138+
description: ''
139+
responses:
140+
'200':
141+
description: ''
142+
content:
143+
application/json:
144+
schema:
145+
$ref: '#/components/schemas/ArrayWithNullInArrayExample'
146+
components:
147+
schemas:
148+
Pet:
149+
required:
150+
- id
151+
- name
152+
properties:
153+
id:
154+
type: integer
155+
format: int64
156+
name:
157+
type: string
158+
tag:
159+
type: string
160+
161+
ObjectWithNullExample:
162+
type: object
163+
properties:
164+
foo:
165+
type: string
166+
nullable: true
167+
example: null
168+
169+
ObjectWithNullInSchemaExample:
170+
type: object
171+
example:
172+
a: 5
173+
b: test
174+
c: true
175+
d: null
176+
177+
ObjectWithNullPropertyExample:
178+
type: object
179+
properties:
180+
a:
181+
type: integer
182+
example: 5
183+
b:
184+
type: string
185+
nullable: true
186+
example: null
187+
188+
StringWithNullExample:
189+
type: string
190+
nullable: true
191+
example: null
192+
193+
ArrayWithNullArrayExample:
194+
type: array
195+
items:
196+
type: string
197+
nullable: true
198+
example: null
199+
200+
ArrayWithNullItemExample:
201+
type: array
202+
items:
203+
type: string
204+
nullable: true
205+
example: null
206+
207+
ArrayWithNullInArrayExample:
208+
type: array
209+
items:
210+
type: string
211+
nullable: true
212+
example: [foo, null]

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@
369369
<swagger-parser-v2-version>1.0.55</swagger-parser-v2-version>
370370
<commons-io-version>2.6</commons-io-version>
371371
<slf4j-version>1.7.30</slf4j-version>
372-
<swagger-core-version>2.1.7</swagger-core-version>
372+
<swagger-core-version>2.1.10-SNAPSHOT</swagger-core-version>
373373
<swagger-core-v2-version>1.6.2</swagger-core-v2-version>
374374
<junit-version>4.13.1</junit-version>
375375
<testng-version>6.14.2</testng-version>

0 commit comments

Comments
 (0)