Skip to content

Commit 2f90a44

Browse files
authored
Handle missing security schemes component (#211)
Throw an explicit `IllegalArgumentException` if `components.securitySchemes` is missing.
1 parent 028c40b commit 2f90a44

File tree

3 files changed

+236
-1
lines changed

3 files changed

+236
-1
lines changed

core/src/main/java/org/openapitools/openapidiff/core/compare/SecurityRequirementsDiff.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.swagger.v3.oas.models.security.SecurityScheme;
88
import java.util.ArrayList;
99
import java.util.List;
10+
import java.util.Map;
1011
import java.util.Optional;
1112
import java.util.stream.Collectors;
1213
import org.apache.commons.collections4.CollectionUtils;
@@ -55,10 +56,16 @@ private List<Pair<SecurityScheme.Type, SecurityScheme.In>> getListOfSecuritySche
5556
return securityRequirement.keySet().stream()
5657
.map(
5758
x -> {
58-
SecurityScheme result = components.getSecuritySchemes().get(x);
59+
Map<String, SecurityScheme> securitySchemes = components.getSecuritySchemes();
60+
if (securitySchemes == null) {
61+
throw new IllegalArgumentException("Missing securitySchemes component definition.");
62+
}
63+
64+
SecurityScheme result = securitySchemes.get(x);
5965
if (result == null) {
6066
throw new IllegalArgumentException("Impossible to find security scheme: " + x);
6167
}
68+
6269
return result;
6370
})
6471
.map(this::getPair)

core/src/test/java/org/openapitools/openapidiff/core/SecurityDiffTest.java

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class SecurityDiffTest {
1212
private final String OPENAPI_DOC1 = "security_diff_1.yaml";
1313
private final String OPENAPI_DOC2 = "security_diff_2.yaml";
1414
private final String OPENAPI_DOC3 = "security_diff_3.yaml";
15+
private final String OPENAPI_DOC4 = "security_diff_4.yaml";
1516

1617
@Test
1718
public void testDiffDifferent() {
@@ -89,5 +90,8 @@ public void testWithUnknownSecurityScheme() {
8990
assertThrows(
9091
IllegalArgumentException.class,
9192
() -> OpenApiCompare.fromLocations(OPENAPI_DOC3, OPENAPI_DOC3));
93+
assertThrows(
94+
IllegalArgumentException.class,
95+
() -> OpenApiCompare.fromLocations(OPENAPI_DOC4, OPENAPI_DOC4));
9296
}
9397
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
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+
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+
schemas:
174+
Tag:
175+
type: object
176+
properties:
177+
id:
178+
type: integer
179+
format: int64
180+
name:
181+
type: string
182+
xml:
183+
name: Tag
184+
Pet:
185+
type: object
186+
required:
187+
- name
188+
- photoUrls
189+
properties:
190+
id:
191+
type: integer
192+
format: int64
193+
category:
194+
type: string
195+
name:
196+
type: string
197+
example: doggie
198+
newField:
199+
type: string
200+
example: a field demo
201+
description: a field demo
202+
photoUrls:
203+
type: array
204+
xml:
205+
name: photoUrl
206+
wrapped: true
207+
items:
208+
type: string
209+
tags:
210+
type: array
211+
xml:
212+
name: tag
213+
wrapped: true
214+
items:
215+
$ref: '#/components/schemas/Tag'
216+
status:
217+
type: string
218+
description: pet status in the store
219+
enum:
220+
- available
221+
- pending
222+
- sold
223+
xml:
224+
name: Pet

0 commit comments

Comments
 (0)