Skip to content

Commit 38e096a

Browse files
authored
Fix NPE with security schemes without components element (OpenAPITools#260)
1 parent 882c5c1 commit 38e096a

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ private List<Pair<SecurityScheme.Type, SecurityScheme.In>> getListOfSecuritySche
5656
return securityRequirement.keySet().stream()
5757
.map(
5858
x -> {
59+
if (components == null) {
60+
throw new IllegalArgumentException("Missing securitySchemes component definition.");
61+
}
5962
Map<String, SecurityScheme> securitySchemes = components.getSecuritySchemes();
6063
if (securitySchemes == null) {
6164
throw new IllegalArgumentException("Missing securitySchemes component definition.");

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

+8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class SecurityDiffTest {
1313
private final String OPENAPI_DOC2 = "security_diff_2.yaml";
1414
private final String OPENAPI_DOC3 = "security_diff_3.yaml";
1515
private final String OPENAPI_DOC4 = "security_diff_4.yaml";
16+
private final String OPENAPI_DOC5 = "security_diff_5.yaml";
1617

1718
@Test
1819
public void testDiffDifferent() {
@@ -94,4 +95,11 @@ public void testWithUnknownSecurityScheme() {
9495
IllegalArgumentException.class,
9596
() -> OpenApiCompare.fromLocations(OPENAPI_DOC4, OPENAPI_DOC4));
9697
}
98+
99+
@Test
100+
public void testMissingSecurityDefinition() {
101+
assertThrows(
102+
IllegalArgumentException.class,
103+
() -> OpenApiCompare.fromLocations(OPENAPI_DOC5, OPENAPI_DOC5));
104+
}
97105
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
19+
paths:
20+
'/pet':
21+
get:
22+
summary: Deletes a pet
23+
description: ''
24+
operationId: qqq
25+
security:
26+
- test: []
27+
responses:
28+
'200':
29+
description: Invalid ID supplied
30+
31+
# security components missing
32+
components: {}

0 commit comments

Comments
 (0)