Skip to content

Commit a4cec9b

Browse files
committed
refs #4483 - fix NullPointer for @apiresponse missing description
1 parent 6d3602c commit a4cec9b

File tree

3 files changed

+155
-2
lines changed

3 files changed

+155
-2
lines changed

modules/swagger-jaxrs2/src/main/java/io/swagger/v3/jaxrs2/Reader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,7 +1200,7 @@ protected Operation parseMethod(
12001200
Content content = resolveEmptyContent(classProduces, methodProduces);
12011201

12021202
ApiResponse apiResponseObject = new ApiResponse().description(DEFAULT_DESCRIPTION).content(content);
1203-
operation.setResponses(new ApiResponses()._default(apiResponseObject));
1203+
operation.setResponses(new ApiResponses().addApiResponse(defaultResponseKey, apiResponseObject));
12041204
}
12051205
if (returnTypeSchema != null) {
12061206
resolveResponseSchemaFromReturnType(operation, classResponses, returnTypeSchema, classProduces, methodProduces);
@@ -1296,8 +1296,8 @@ protected void resolveResponseSchemaFromReturnType(
12961296
}
12971297
opResponse.content(content);
12981298
}
1299+
opResponse.getContent().putAll(reresolvedMediaTypes);
12991300
}
1300-
opResponse.getContent().putAll(reresolvedMediaTypes);
13011301
}
13021302
}
13031303
}

modules/swagger-jaxrs2/src/test/java/io/swagger/v3/jaxrs2/ReaderTest.java

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
import io.swagger.v3.jaxrs2.resources.Ticket3731Resource;
8181
import io.swagger.v3.jaxrs2.resources.Ticket4412Resource;
8282
import io.swagger.v3.jaxrs2.resources.Ticket4446Resource;
83+
import io.swagger.v3.jaxrs2.resources.Ticket4483Resource;
8384
import io.swagger.v3.jaxrs2.resources.UploadResource;
8485
import io.swagger.v3.jaxrs2.resources.UrlEncodedResourceWithEncodings;
8586
import io.swagger.v3.jaxrs2.resources.UserAnnotationResource;
@@ -4061,4 +4062,85 @@ public void testParameterMaximumValue() {
40614062
" '*/*': {}\n";
40624063
SerializationMatchers.assertEqualsToYaml31(openAPI, yaml);
40634064
}
4065+
4066+
@Test
4067+
public void test4483Response() {
4068+
Reader reader = new Reader(new OpenAPI());
4069+
4070+
OpenAPI openAPI = reader.read(Ticket4483Resource.class);
4071+
String yaml = "openapi: 3.0.1\n" +
4072+
"tags:\n" +
4073+
"- name: Dummy\n" +
4074+
" description: Dummy resource for testing setup\n" +
4075+
"paths:\n" +
4076+
" /test:\n" +
4077+
" get:\n" +
4078+
" tags:\n" +
4079+
" - Dummy\n" +
4080+
" description: Dummy GET\n" +
4081+
" operationId: dummy\n" +
4082+
" responses:\n" +
4083+
" \"401\":\n" +
4084+
" description: Authentication is required\n" +
4085+
" content:\n" +
4086+
" application/json:\n" +
4087+
" schema:\n" +
4088+
" type: array\n" +
4089+
" items:\n" +
4090+
" $ref: '#/components/schemas/LocalizedError'\n" +
4091+
" \"200\":\n" +
4092+
" description: test\n" +
4093+
" content:\n" +
4094+
" application/json:\n" +
4095+
" schema:\n" +
4096+
" type: object\n" +
4097+
" additionalProperties:\n" +
4098+
" type: boolean\n" +
4099+
" /test/opresp:\n" +
4100+
" get:\n" +
4101+
" tags:\n" +
4102+
" - Dummy\n" +
4103+
" operationId: dummyopresp\n" +
4104+
" responses:\n" +
4105+
" \"401\":\n" +
4106+
" description: Authentication is required\n" +
4107+
" content:\n" +
4108+
" application/json:\n" +
4109+
" schema:\n" +
4110+
" type: array\n" +
4111+
" items:\n" +
4112+
" $ref: '#/components/schemas/LocalizedError'\n" +
4113+
" \"200\":\n" +
4114+
" description: Dummy GET opresp\n" +
4115+
" content:\n" +
4116+
" application/json:\n" +
4117+
" schema:\n" +
4118+
" type: object\n" +
4119+
" additionalProperties:\n" +
4120+
" type: boolean\n" +
4121+
" /test/oprespnodesc:\n" +
4122+
" get:\n" +
4123+
" tags:\n" +
4124+
" - Dummy\n" +
4125+
" operationId: oprespnodesc\n" +
4126+
" responses:\n" +
4127+
" \"401\":\n" +
4128+
" description: Authentication is required\n" +
4129+
" content:\n" +
4130+
" application/json:\n" +
4131+
" schema:\n" +
4132+
" type: array\n" +
4133+
" items:\n" +
4134+
" $ref: '#/components/schemas/LocalizedError'\n" +
4135+
"components:\n" +
4136+
" schemas:\n" +
4137+
" LocalizedError:\n" +
4138+
" type: object\n" +
4139+
" properties:\n" +
4140+
" code:\n" +
4141+
" type: string\n" +
4142+
" message:\n" +
4143+
" type: string\n";
4144+
SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
4145+
}
40644146
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package io.swagger.v3.jaxrs2.resources;
2+
3+
import io.swagger.v3.oas.annotations.Operation;
4+
import io.swagger.v3.oas.annotations.media.ArraySchema;
5+
import io.swagger.v3.oas.annotations.media.Content;
6+
import io.swagger.v3.oas.annotations.media.Schema;
7+
import io.swagger.v3.oas.annotations.responses.ApiResponse;
8+
import io.swagger.v3.oas.annotations.responses.ApiResponses;
9+
import io.swagger.v3.oas.annotations.tags.Tag;
10+
11+
import javax.ws.rs.Consumes;
12+
import javax.ws.rs.GET;
13+
import javax.ws.rs.Path;
14+
import javax.ws.rs.Produces;
15+
import javax.ws.rs.core.MediaType;
16+
import java.util.Map;
17+
18+
/*
19+
20+
*/
21+
@Tag(
22+
name = "Dummy",
23+
description = "Dummy resource for testing setup"
24+
)
25+
@Path("test")
26+
@Consumes({MediaType.APPLICATION_JSON})
27+
@Produces({MediaType.APPLICATION_JSON})
28+
@ApiResponses({
29+
@ApiResponse(responseCode = "401", description = "Authentication is required", content = @Content(array = @ArraySchema(schema = @Schema(implementation = Ticket4483Resource.LocalizedError.class))))
30+
})
31+
public class Ticket4483Resource {
32+
@GET
33+
@Operation(
34+
description = "Dummy GET"
35+
)
36+
@ApiResponse(responseCode = "200", description = "test", useReturnTypeSchema = true)
37+
public Map<String, Boolean> dummy() {
38+
Map map = new java.util.HashMap();
39+
map.put("success", Boolean.TRUE);
40+
return map;
41+
}
42+
43+
44+
45+
@Path("/opresp")
46+
@GET
47+
@Operation(
48+
responses = {
49+
@ApiResponse(responseCode = "200", description = "Dummy GET opresp", useReturnTypeSchema = true)})
50+
public Map<String, Boolean> dummyopresp() {
51+
Map map = new java.util.HashMap();
52+
map.put("success", Boolean.TRUE);
53+
return map;
54+
}
55+
56+
@Path("/oprespnodesc")
57+
@GET
58+
@Operation(
59+
responses = {
60+
@ApiResponse(responseCode = "200", useReturnTypeSchema = true)})
61+
public Map<String, Boolean> oprespnodesc() {
62+
Map map = new java.util.HashMap();
63+
map.put("success", Boolean.TRUE);
64+
return map;
65+
}
66+
67+
public static class LocalizedError {
68+
public String code;
69+
public String message;
70+
}
71+
}

0 commit comments

Comments
 (0)