Skip to content

Commit 371361e

Browse files
committed
Fix null pointer exception when security scheme doesn't exist
1 parent a38208d commit 371361e

File tree

3 files changed

+254
-1
lines changed

3 files changed

+254
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ public boolean same(SecurityRequirement left, SecurityRequirement right) {
5656

5757
private List<Pair<SecurityScheme.Type, SecurityScheme.In>> getListOfSecuritySchemes(Components components, SecurityRequirement securityRequirement) {
5858
return securityRequirement.keySet().stream()
59-
.map(x -> components.getSecuritySchemes().get(x))
59+
.map(x -> {
60+
SecurityScheme result = components.getSecuritySchemes().get(x);
61+
if (result == null) {
62+
throw new IllegalArgumentException("Impossible to find security scheme: " + x);
63+
}
64+
return result;
65+
})
6066
.map(this::getPair)
6167
.distinct()
6268
.collect(Collectors.toList());

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
public class SecurityDiffTest {
1414
private final String OPENAPI_DOC1 = "security_diff_1.yaml";
1515
private final String OPENAPI_DOC2 = "security_diff_2.yaml";
16+
private final String OPENAPI_DOC3 = "security_diff_3.yaml";
1617

1718

1819
@Test
@@ -59,4 +60,9 @@ public void testDiffDifferent() {
5960
assertTrue(securityRequirement3.size() == 1);
6061
assertTrue(securityRequirement3.get("petstore_auth").size() == 2);
6162
}
63+
64+
@Test(expected = IllegalArgumentException.class)
65+
public void testWithUnknownSecurityScheme() {
66+
OpenApiCompare.fromLocations(OPENAPI_DOC3, OPENAPI_DOC3);
67+
}
6268
}
Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
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+
tags:
19+
- name: pet
20+
description: Everything about your Pets
21+
externalDocs:
22+
description: Find out more
23+
url: 'http://swagger.io'
24+
- name: store
25+
description: Access to Petstore orders
26+
- name: user
27+
description: Operations about user
28+
externalDocs:
29+
description: Find out more about our store
30+
url: 'http://swagger.io'
31+
security:
32+
- petstore_auth:
33+
- 'write:pets'
34+
- 'read:pets'
35+
- unknown: []
36+
paths:
37+
'/pet/{petId}':
38+
parameters:
39+
- name: newHeaderParam
40+
in: header
41+
required: false
42+
schema:
43+
type: integer
44+
delete:
45+
tags:
46+
- pet
47+
summary: Deletes a pet
48+
description: ''
49+
operationId: deletePet
50+
parameters:
51+
- name: api_key
52+
in: header
53+
required: false
54+
schema:
55+
type: string
56+
- name: newHeaderParam
57+
in: header
58+
required: false
59+
schema:
60+
type: string
61+
- name: petId
62+
in: path
63+
description: Pet id to delete
64+
required: true
65+
schema:
66+
type: integer
67+
format: int64
68+
responses:
69+
'400':
70+
description: Invalid ID supplied
71+
'404':
72+
description: Pet not found
73+
security:
74+
- petstore_auth:
75+
- 'write:pets'
76+
/pet:
77+
post:
78+
tags:
79+
- pet
80+
summary: Add a new pet to the store
81+
description: ''
82+
operationId: addPet
83+
responses:
84+
'405':
85+
description: Invalid input
86+
requestBody:
87+
$ref: '#/components/requestBodies/Pet'
88+
/pet2:
89+
post:
90+
tags:
91+
- pet
92+
summary: Add a new pet to the store
93+
description: ''
94+
operationId: addPet
95+
responses:
96+
'405':
97+
description: Invalid input
98+
requestBody:
99+
$ref: '#/components/requestBodies/Pet'
100+
/pet3:
101+
post:
102+
tags:
103+
- pet
104+
summary: Add a new pet to the store
105+
description: ''
106+
operationId: addPet
107+
responses:
108+
'405':
109+
description: Invalid input
110+
requestBody:
111+
$ref: '#/components/requestBodies/Pet'
112+
security:
113+
- petstore_auth:
114+
- 'write:pets'
115+
- 'read:pets'
116+
/pet/findByStatus2:
117+
get:
118+
tags:
119+
- pet
120+
summary: Finds Pets by status
121+
description: Multiple status values can be provided with comma separated strings
122+
operationId: findPetsByStatus
123+
parameters:
124+
- name: status
125+
in: query
126+
deprecated: true
127+
description: Status values that need to be considered for filter
128+
required: true
129+
explode: true
130+
schema:
131+
type: array
132+
items:
133+
type: string
134+
enum:
135+
- available
136+
- pending
137+
- sold
138+
default: available
139+
security:
140+
- tenant: []
141+
user: []
142+
responses:
143+
'200':
144+
description: successful operation
145+
content:
146+
application/xml:
147+
schema:
148+
type: array
149+
items:
150+
$ref: '#/components/schemas/Pet'
151+
application/json:
152+
schema:
153+
type: array
154+
items:
155+
$ref: '#/components/schemas/Pet'
156+
'400':
157+
description: Invalid status value
158+
externalDocs:
159+
description: Find out more about Swagger
160+
url: 'http://swagger.io'
161+
components:
162+
requestBodies:
163+
Pet:
164+
content:
165+
application/json:
166+
schema:
167+
$ref: '#/components/schemas/Pet'
168+
application/xml:
169+
schema:
170+
$ref: '#/components/schemas/Pet'
171+
description: Pet object that needs to be added to the store
172+
required: true
173+
securitySchemes:
174+
petstore_auth:
175+
type: oauth2
176+
flows:
177+
implicit:
178+
authorizationUrl: 'http://petstore.swagger.io/oauth/dialog'
179+
scopes:
180+
'write:pets': modify pets in your account
181+
'read:pets': read your pets
182+
tenant:
183+
type: apiKey
184+
name: tenant
185+
in: header
186+
user:
187+
type: apiKey
188+
name: user
189+
in: header
190+
schemas:
191+
Tag:
192+
type: object
193+
properties:
194+
id:
195+
type: integer
196+
format: int64
197+
name:
198+
type: string
199+
xml:
200+
name: Tag
201+
Pet:
202+
type: object
203+
required:
204+
- name
205+
- photoUrls
206+
properties:
207+
id:
208+
type: integer
209+
format: int64
210+
category:
211+
type: string
212+
name:
213+
type: string
214+
example: doggie
215+
newField:
216+
type: string
217+
example: a field demo
218+
description: a field demo
219+
photoUrls:
220+
type: array
221+
xml:
222+
name: photoUrl
223+
wrapped: true
224+
items:
225+
type: string
226+
tags:
227+
type: array
228+
xml:
229+
name: tag
230+
wrapped: true
231+
items:
232+
$ref: '#/components/schemas/Tag'
233+
status:
234+
type: string
235+
description: pet status in the store
236+
enum:
237+
- available
238+
- pending
239+
- sold
240+
xml:
241+
name: Pet

0 commit comments

Comments
 (0)