Skip to content

Commit 0425433

Browse files
authored
Merge pull request #852 from brightcove/fix-npe-no-content-api-response
Fix NullPointerException when resolving an ApiResponse with no content
2 parents 911d018 + adea5ff commit 0425433

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/ResolverCache.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,10 @@ else if(rootPath != null) {
159159
protected <T> void updateLocalRefs(String file, T result) {
160160
if(result instanceof ApiResponse) {
161161
ApiResponse response = (ApiResponse) result;
162-
for(String mediaType : response.getContent().keySet()) {
163-
updateLocalRefs(file, response.getContent().get(mediaType).getSchema());
162+
if (response.getContent() != null) {
163+
for (String mediaType : response.getContent().keySet()) {
164+
updateLocalRefs(file, response.getContent().get(mediaType).getSchema());
165+
}
164166
}
165167
}
166168
if(result instanceof Schema && ((Schema)(result)).get$ref() != null) {

modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/ResolverCacheTest.java

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ public class ResolverCacheTest {
3636
@Mocked
3737
RefUtils refUtils;
3838

39-
@Mocked
40-
DeserializationUtils deserializationUtils;
4139

4240
@Injectable
4341
OpenAPI openAPI;
@@ -50,7 +48,7 @@ public void testMock(@Injectable final Schema expectedResult) throws Exception {
5048
final String ref = "http://my.company.com/path/to/file.json";
5149
final String contentsOfExternalFile = "really good json";
5250

53-
new Expectations() {{
51+
new Expectations(DeserializationUtils.class) {{
5452
RefUtils.readExternalUrlRef(ref, format, auths, "http://my.company.com/path/parent.json");
5553
times = 1;
5654
result = contentsOfExternalFile;
@@ -75,7 +73,7 @@ public void testLoadExternalRef_NoDefinitionPath(@Injectable final Schema expect
7573
final String ref = "http://my.company.com/path/to/file.json";
7674
final String contentsOfExternalFile = "really good json";
7775

78-
new Expectations() {{
76+
new Expectations(DeserializationUtils.class) {{
7977
RefUtils.readExternalUrlRef(ref, format, auths, "http://my.company.com/path/parent.json");
8078
times = 1;
8179
result = contentsOfExternalFile;
@@ -128,6 +126,35 @@ public void testLoadExternalRefWithEscapedCharacters() throws Exception {
128126
assertNotNull(path);
129127
}
130128

129+
@Test
130+
public void testLoadExternalRefResponseWithNoContent() throws Exception {
131+
final RefFormat format = RefFormat.URL;
132+
final String ref = "http://my.company.com/path/to/main.yaml";
133+
final String contentsOfExternalFile = "openapi: 3.0.0\n" +
134+
"\n" +
135+
"info:\n" +
136+
" version: 1.0.0\n" +
137+
" title: Response include test case child\n" +
138+
"\n" +
139+
"components:\n" +
140+
" responses:\n" +
141+
" 200:\n" +
142+
" description: Success\n";
143+
144+
new Expectations() {{
145+
RefUtils.readExternalUrlRef(ref, format, auths, "http://my.company.com/path/parent.json");
146+
times = 1;
147+
result = contentsOfExternalFile;
148+
}};
149+
150+
ResolverCache cache = new ResolverCache(openAPI, auths, "http://my.company.com/path/parent.json");
151+
152+
ApiResponse response = cache.loadRef(ref+"#/components/responses/200", RefFormat.URL, ApiResponse.class);
153+
assertNotNull(response);
154+
assertEquals(response.getDescription(), "Success");
155+
assertNull(response.getContent());
156+
}
157+
131158
@Test
132159
public void testLoadInternalParameterRef(@Injectable Parameter mockedParameter) throws Exception {
133160
OpenAPI openAPI = new OpenAPI();

0 commit comments

Comments
 (0)