Skip to content

Commit 2b07ea9

Browse files
authored
Merge pull request #1313 from jmini/strict-yaml
enable STRICT_DUPLICATE_DETECTION in Jackson parser for Yaml
2 parents de4dbcf + d630dea commit 2b07ea9

File tree

5 files changed

+54
-17
lines changed

5 files changed

+54
-17
lines changed

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/ObjectMapperFactory.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ public static ObjectMapper createJson() {
1515
}
1616

1717
protected static ObjectMapper createJson(boolean includePathDeserializer, boolean includeResponseDeserializer) {
18-
return create(null, includePathDeserializer, includeResponseDeserializer);
18+
return create(createJsonFactory(), includePathDeserializer, includeResponseDeserializer);
1919
}
2020

2121
public static ObjectMapper createYaml() {
2222
return createYaml(true, true);
2323
}
2424

2525
protected static ObjectMapper createYaml(boolean includePathDeserializer, boolean includeResponseDeserializer) {
26-
return create(new YAMLFactory(), includePathDeserializer, includeResponseDeserializer);
26+
return create(createYamlFactory(), includePathDeserializer, includeResponseDeserializer);
2727
}
2828

2929
private static ObjectMapper create(JsonFactory jsonFactory, boolean includePathDeserializer, boolean includeResponseDeserializer) {
30-
ObjectMapper mapper = jsonFactory == null ? new ObjectMapper(createJsonFactory()) : new ObjectMapper(jsonFactory);
30+
ObjectMapper mapper = new ObjectMapper(jsonFactory);
3131

3232
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
3333
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
@@ -42,4 +42,10 @@ private static JsonFactory createJsonFactory() {
4242
.enable(StreamReadFeature.STRICT_DUPLICATE_DETECTION)
4343
.build();
4444
}
45+
46+
private static JsonFactory createYamlFactory() {
47+
return YAMLFactory.builder()
48+
.enable(StreamReadFeature.STRICT_DUPLICATE_DETECTION)
49+
.build();
50+
}
4551
}

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

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ public void testIssue1169() {
180180
assertNotNull(apispec);
181181
}
182182

183-
184183
@Test
185184
public void testIssue339() throws Exception {
186185
OpenAPIV3Parser openAPIV3Parser = new OpenAPIV3Parser();
@@ -522,7 +521,6 @@ public void testPattern() {
522521
Assert.assertEquals(s.getPattern(),"^[A-Z]+$"); //ERROR: got null
523522
}
524523

525-
526524
@BeforeClass
527525
private void setUpWireMockServer() throws IOException {
528526
this.wireMockServer = new WireMockServer(WireMockConfiguration.wireMockConfig().dynamicPort());
@@ -867,7 +865,6 @@ public void int64ExampleWithoutOverflow() throws Exception {
867865
Assert.assertEquals("1516042231144", date.getExample().toString());
868866
}
869867

870-
871868
@Test
872869
public void testRefPaths() throws Exception {
873870
String yaml = "openapi: '3.0.0'\n" +
@@ -1234,10 +1231,8 @@ public void testIssue() {
12341231

12351232
assertEquals(((Map) openAPI.getExtensions().get("x-some-vendor")).get("sometesting"), "bye!");
12361233
assertEquals(openAPI.getPaths().get("/foo").getExtensions().get("x-something"), "yes, it is supported");
1237-
12381234
}
12391235

1240-
12411236
@Test
12421237
public void testIssue292WithCSVCollectionFormat() {
12431238
String yaml =
@@ -1898,7 +1893,7 @@ public void shouldParseParameters() {
18981893
assertEquals(parameter.getIn(), "path");
18991894
assertEquals(parameter.getName(), "playerId");
19001895
}
1901-
1896+
19021897
@Test
19031898
public void testIssue884() {
19041899
ParseOptions parseOptions = new ParseOptions();
@@ -1910,7 +1905,7 @@ public void testIssue884() {
19101905
assertEquals(operationId, "getRepository");
19111906
assertNotNull(userRepository.getHeaders());
19121907
}
1913-
1908+
19141909
@Test
19151910
public void testLinkIssue() {
19161911
ParseOptions parseOptions = new ParseOptions();
@@ -2067,10 +2062,10 @@ public void shouldParseApiWithMultipleParameterReferences() {
20672062

20682063
@Test
20692064
public void shouldParseApiWithParametersUsingContentvsSchema() {
2070-
// Tests that the content method of specifying the format of a parameter
2071-
// gets resolved.
2072-
// Test checks if an API's single parameter of array type gets fully resolved to
2073-
// referenced definitions.
2065+
// Tests that the content method of specifying the format of a parameter
2066+
// gets resolved.
2067+
// Test checks if an API's single parameter of array type gets fully resolved to
2068+
// referenced definitions.
20742069
String location = "src/test/resources/issue-1078/api.yaml";
20752070
ParseOptions options = new ParseOptions();
20762071
options.setResolve(true);
@@ -2261,7 +2256,7 @@ public void testSampleParser() {
22612256
}
22622257

22632258
@Test
2264-
public void testDuplicateHttpStatusCodes() {
2259+
public void testDuplicateHttpStatusCodesJson() {
22652260
final String location = "src/test/resources/duplicateHttpStatusCodes.json";
22662261

22672262
final ParseOptions options = new ParseOptions();
@@ -2276,6 +2271,22 @@ public void testDuplicateHttpStatusCodes() {
22762271

22772272
}
22782273

2274+
@Test
2275+
public void testDuplicateHttpStatusCodesYaml() {
2276+
final String location = "src/test/resources/duplicateHttpStatusCodes.yaml";
2277+
2278+
final ParseOptions options = new ParseOptions();
2279+
options.setResolve(true);
2280+
2281+
final OpenAPIV3Parser parser = new OpenAPIV3Parser();
2282+
final SwaggerParseResult result = parser.readLocation(location, null, options);
2283+
assertNull(result.getOpenAPI());
2284+
List<String> messages = result.getMessages();
2285+
assertEquals(1, messages.size());
2286+
assertEquals(messages.get(0), "Duplicate field '200' in `src/test/resources/duplicateHttpStatusCodes.yaml`");
2287+
2288+
}
2289+
22792290
private static int getDynamicPort() {
22802291
return new Random().ints(10000, 20000).findFirst().getAsInt();
22812292
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
openapi: 3.0.1
2+
info:
3+
title: title
4+
description: This is a sample server
5+
license:
6+
name: Apache-2.0
7+
url: http://www.apache.org/licenses/LICENSE-2.0.html
8+
version: 1.0.0
9+
servers:
10+
- url: https://api.absolute.org/v2
11+
description: An absolute path
12+
paths:
13+
/whatever:
14+
get:
15+
summary: Some operation
16+
description: Some operation
17+
operationId: doWhatever
18+
responses:
19+
"200":
20+
description: OK
21+
"200":
22+
description: duplicate HTTP status code

modules/swagger-parser-v3/src/test/resources/oas3.yaml.template

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ security:
3737
- tokenAuth: []
3838
info:
3939
description: 'This is a sample server Petstore'
40-
version: 1.0.0
4140
title: Sample Pet Store App
4241
termsOfService: http://swagger.io/terms/
4342
x-info: info extension

modules/swagger-parser-v3/src/test/resources/petstore.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,6 @@ paths:
450450
summary: Logs user into the system
451451
description: ''
452452
operationId: loginUser
453-
security: []
454453
parameters:
455454
- name: username
456455
in: query

0 commit comments

Comments
 (0)