Skip to content

Commit a38208d

Browse files
committed
Fix null pointer exception when multiple paths have the same signature
1 parent d10c118 commit a38208d

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

src/main/java/com/qdesrame/openapi/diff/compare/PathsDiff.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ public Optional<ChangedPaths> diff(final Map<String, PathItem> left, final Map<S
3838
String template = normalizePath(url);
3939
Optional<String> result = right.keySet().stream().filter(s -> normalizePath(s).equals(template)).findFirst();
4040
if (result.isPresent()) {
41+
if (!changedPaths.getIncreased().containsKey(result.get())) {
42+
throw new IllegalArgumentException("Two path items have the same signature: " + template);
43+
}
4144
PathItem rightPath = changedPaths.getIncreased().remove(result.get());
4245
Map<String, String> params = new HashMap<>();
4346
if (!url.equals(result.get())) {

src/test/java/com/qdesrame/openapi/test/PathDiffTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@ public class PathDiffTest {
88

99
private final String OPENAPI_PATH1 = "path_1.yaml";
1010
private final String OPENAPI_PATH2 = "path_2.yaml";
11+
private final String OPENAPI_PATH3 = "path_3.yaml";
1112

1213
@Test
1314
public void testEqual() {
1415
assertOpenApiAreEquals(OPENAPI_PATH1, OPENAPI_PATH2);
1516
}
1617

18+
@Test(expected = IllegalArgumentException.class)
19+
public void testMultiplePathWithSameSignature() {
20+
assertOpenApiAreEquals(OPENAPI_PATH3, OPENAPI_PATH3);
21+
}
22+
1723
}

src/test/resources/path_3.yaml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
openapi: 3.0.0
2+
servers:
3+
- url: 'http://petstore.swagger.io/v2'
4+
info:
5+
description: >-
6+
This is a sample server Petstore server. You can find out more about
7+
Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net,
8+
#swagger](http://swagger.io/irc/). For this sample, you can use the api key
9+
`special-key` to test the authorization filters.
10+
version: 1.0.0
11+
title: Swagger Petstore
12+
termsOfService: 'http://swagger.io/terms/'
13+
contact:
14+
email: apiteam@swagger.io
15+
license:
16+
name: Apache 2.0
17+
url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
18+
paths:
19+
/pet/{petId}:
20+
get:
21+
tags:
22+
- pet
23+
summary: gets a pet by id
24+
description: ''
25+
operationId: updatePetWithForm
26+
parameters:
27+
- name: petId
28+
in: path
29+
description: ID of pet that needs to be updated
30+
required: true
31+
schema:
32+
type: integer
33+
responses:
34+
'405':
35+
description: Invalid input
36+
/pet/{petId2}:
37+
get:
38+
tags:
39+
- pet
40+
summary: gets a pet by id
41+
description: ''
42+
operationId: updatePetWithForm
43+
parameters:
44+
- name: petId2
45+
in: path
46+
description: ID of pet that needs to be updated
47+
required: true
48+
schema:
49+
type: integer
50+
responses:
51+
'405':
52+
description: Invalid input

0 commit comments

Comments
 (0)