Skip to content

Commit ac1f526

Browse files
committed
fix for issue unexpectedNullExamples when flatten set to true
1 parent 56370b2 commit ac1f526

File tree

3 files changed

+207
-10
lines changed

3 files changed

+207
-10
lines changed

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/InlineModelResolver.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,9 @@ private void fixStringModel(Schema m) {
387387
String example = m.getExample().toString();
388388
if (example.substring(0, 1).equals("\"") &&
389389
example.substring(example.length() - 1).equals("\"")) {
390-
m.setExample(example.substring(1, example.length() - 1));
390+
if(example != null || (example == null && m.getExampleSetFlag())) {
391+
m.setExample(example.substring(1, example.length() - 1));
392+
}
391393
}
392394
}
393395
}
@@ -599,7 +601,9 @@ public Schema modelFromProperty(ArraySchema object, @SuppressWarnings("unused")
599601
if (inner instanceof ObjectSchema) {
600602
ArraySchema model = new ArraySchema();
601603
model.setDescription(description);
602-
model.setExample(example);
604+
if(example != null || (example == null && object.getExampleSetFlag())) {
605+
model.setExample(example);
606+
}
603607
model.setItems(object.getItems());
604608
return model;
605609
}
@@ -626,7 +630,9 @@ public Schema createModelFromProperty(Schema schema, String path) {
626630
ComposedSchema composedModel = (ComposedSchema) schema;
627631

628632
composedModel.setDescription(description);
629-
composedModel.setExample(example);
633+
if(example != null || (example == null && schema.getExampleSetFlag())) {
634+
composedModel.setExample(example);
635+
}
630636
composedModel.setName(name);
631637
composedModel.setXml(xml);
632638
composedModel.setType(schema.getType());
@@ -642,7 +648,9 @@ public Schema createModelFromProperty(Schema schema, String path) {
642648
model.setDeprecated(schema.getDeprecated());
643649
model.setDiscriminator(schema.getDiscriminator());
644650
model.setEnum(schema.getEnum());
645-
model.setExample(example);
651+
if(example != null || (example == null && schema.getExampleSetFlag())) {
652+
model.setExample(example);
653+
}
646654
model.setExclusiveMaximum(schema.getExclusiveMaximum());
647655
model.setExclusiveMinimum(schema.getExclusiveMinimum());
648656
model.setFormat(schema.getFormat());
@@ -688,7 +696,9 @@ public Schema modelFromProperty(Schema object, @SuppressWarnings("unused") Strin
688696
}
689697
ArraySchema model = new ArraySchema();
690698
model.setDescription(description);
691-
model.setExample(example);
699+
if(example != null || (example == null && object.getExampleSetFlag())) {
700+
model.setExample(example);
701+
}
692702
if (object.getAdditionalProperties() != null && !(object.getAdditionalProperties() instanceof Boolean)) {
693703
model.setItems((Schema) object.getAdditionalProperties());
694704
}

modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/InlineModelResolverTest.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22

33

44

5-
import static org.testng.AssertJUnit.assertEquals;
6-
import static org.testng.AssertJUnit.assertNotNull;
7-
import static org.testng.AssertJUnit.assertNull;
8-
import static org.testng.AssertJUnit.assertTrue;
9-
105
import java.math.BigDecimal;
116
import java.util.ArrayList;
127
import java.util.HashMap;
@@ -34,9 +29,25 @@
3429
import io.swagger.v3.parser.OpenAPIV3Parser;
3530
import io.swagger.v3.parser.core.models.ParseOptions;
3631

32+
import static org.testng.AssertJUnit.*;
33+
3734
@SuppressWarnings({"static-method", "rawtypes"})
3835
public class InlineModelResolverTest {
3936

37+
@Test
38+
public void testIssueUnexpectedNullValues() throws Exception {
39+
ParseOptions options = new ParseOptions();
40+
options.setResolve(true);
41+
options.setFlatten(true);
42+
OpenAPI openAPI = new OpenAPIV3Parser().read("unexpectedNullValues.yaml", null, options);
43+
assertNotNull(openAPI);
44+
assertNull(openAPI.getComponents().getSchemas().get("DatasetDetail_schema").getExample());
45+
assertFalse(openAPI.getComponents().getSchemas().get("DatasetDetail_schema").getExampleSetFlag());
46+
//example set to null explicitly in the definition
47+
assertNull(openAPI.getComponents().getSchemas().get("verify_datasets").getExample());
48+
assertTrue(openAPI.getComponents().getSchemas().get("verify_datasets").getExampleSetFlag());
49+
}
50+
4051
@Test
4152
public void testIssue1018() throws Exception {
4253
ParseOptions options = new ParseOptions();
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
openapi: 3.0.3
2+
3+
info:
4+
title: Unexpected null value at schema
5+
version: 0.0.16
6+
description: |
7+
- Unexpected null value at schema when the flatten option is set and an inline object property is flatten.
8+
9+
servers:
10+
- url: https://api.abc.com/v1
11+
description: null values
12+
13+
paths:
14+
/dataset:
15+
get:
16+
summary: |
17+
sample for exposing issue
18+
tags:
19+
- dataset
20+
responses:
21+
200:
22+
description: OK - Returns metadata information for the specified dataset
23+
content:
24+
application/json:
25+
schema:
26+
$ref: '#/components/schemas/DatasetDetail'
27+
examples:
28+
123:
29+
value:
30+
id: 123
31+
version: 1
32+
country: CHN
33+
provider: DataZoo
34+
description: DataZoo China National ID
35+
36+
default:
37+
description: Returns an array of errors
38+
content:
39+
application/json:
40+
schema:
41+
$ref: '#/components/schemas/Errors'
42+
/verify:
43+
post:
44+
summary: |
45+
Performs a verification. Depending on the success or failure of the datasets you will receive an HTTP response indicating if all succeeded (200), if some had problems (200) or all had problems
46+
tags:
47+
- verify
48+
requestBody:
49+
description: The verification details
50+
required: true
51+
content:
52+
application/json:
53+
schema:
54+
type: object
55+
properties:
56+
datasets:
57+
type: array
58+
uniqueItems: true
59+
items:
60+
type: object
61+
properties:
62+
id:
63+
description: The dataset id
64+
type: integer
65+
format: int32
66+
example: 123
67+
version:
68+
description: The dataset version number
69+
type: integer
70+
format: int32
71+
example: 1
72+
required:
73+
- id
74+
- version
75+
example: null
76+
minItems: 1
77+
required:
78+
- datasets
79+
- subject
80+
examples:
81+
123:
82+
value:
83+
datasets:
84+
- id: 123
85+
version: 1
86+
credentials:
87+
userName: username
88+
password: password
89+
subject:
90+
person:
91+
firstName: Joe
92+
lastNames:
93+
- Bloggs2
94+
identity:
95+
documents:
96+
- documentType: countryId
97+
documentNumber: '37020319611025031X'
98+
responses:
99+
200:
100+
description: OK - Returns metadata information for the specified dataset
101+
content:
102+
application/json:
103+
schema:
104+
$ref: '#/components/schemas/DatasetDetail'
105+
examples:
106+
123:
107+
value:
108+
id: 123
109+
version: 1
110+
country: CHN
111+
provider: DataZoo
112+
description: DataZoo China National ID
113+
114+
default:
115+
description: Returns an array of errors
116+
content:
117+
application/json:
118+
schema:
119+
$ref: '#/components/schemas/Errors'
120+
121+
components:
122+
schemas:
123+
Dataset:
124+
description: The high level details of a Dataset that is available within a country
125+
type: object
126+
properties:
127+
id:
128+
description: The dataset id
129+
type: integer
130+
format: int32
131+
example: 123
132+
version:
133+
description: The dataset version number
134+
type: integer
135+
format: int32
136+
example: 1
137+
country:
138+
description: The 3 digit ISO country code
139+
type: string
140+
maxLength: 3
141+
example: CHN
142+
provider:
143+
description: The dataset provider name
144+
type: string
145+
maxLength: 999
146+
example: DataZoo
147+
description:
148+
description: The dataset description
149+
type: string
150+
maxLength: 999
151+
example: DataZoo China National ID
152+
required:
153+
- id
154+
- version
155+
156+
Errors:
157+
description: An array of errors
158+
type: object
159+
required:
160+
- errors
161+
162+
DatasetDetail:
163+
description: The high level details of a Dataset that is available within a country
164+
type: object
165+
allOf:
166+
- $ref: '#/components/schemas/Dataset'
167+
properties:
168+
schema:
169+
type: object
170+
properties:
171+
request:
172+
type: string
173+
required:
174+
- request
175+
required:
176+
- schema

0 commit comments

Comments
 (0)