Skip to content

Commit d630dea

Browse files
committed
enable strict YAML parsing
1 parent 5def896 commit d630dea

File tree

5 files changed

+48
-6
lines changed

5 files changed

+48
-6
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: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2256,7 +2256,7 @@ public void testSampleParser() {
22562256
}
22572257

22582258
@Test
2259-
public void testDuplicateHttpStatusCodes() {
2259+
public void testDuplicateHttpStatusCodesJson() {
22602260
final String location = "src/test/resources/duplicateHttpStatusCodes.json";
22612261

22622262
final ParseOptions options = new ParseOptions();
@@ -2271,6 +2271,22 @@ public void testDuplicateHttpStatusCodes() {
22712271

22722272
}
22732273

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+
22742290
private static int getDynamicPort() {
22752291
return new Random().ints(10000, 20000).findFirst().getAsInt();
22762292
}
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)