Skip to content

Commit

Permalink
refs #4483 - fix NullPointer for @ApiResponse missing description
Browse files Browse the repository at this point in the history
  • Loading branch information
frantuma committed Oct 25, 2023
1 parent 6d3602c commit a4cec9b
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1200,7 +1200,7 @@ protected Operation parseMethod(
Content content = resolveEmptyContent(classProduces, methodProduces);

ApiResponse apiResponseObject = new ApiResponse().description(DEFAULT_DESCRIPTION).content(content);
operation.setResponses(new ApiResponses()._default(apiResponseObject));
operation.setResponses(new ApiResponses().addApiResponse(defaultResponseKey, apiResponseObject));
}
if (returnTypeSchema != null) {
resolveResponseSchemaFromReturnType(operation, classResponses, returnTypeSchema, classProduces, methodProduces);
Expand Down Expand Up @@ -1296,8 +1296,8 @@ protected void resolveResponseSchemaFromReturnType(
}
opResponse.content(content);
}
opResponse.getContent().putAll(reresolvedMediaTypes);
}
opResponse.getContent().putAll(reresolvedMediaTypes);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
import io.swagger.v3.jaxrs2.resources.Ticket3731Resource;
import io.swagger.v3.jaxrs2.resources.Ticket4412Resource;
import io.swagger.v3.jaxrs2.resources.Ticket4446Resource;
import io.swagger.v3.jaxrs2.resources.Ticket4483Resource;
import io.swagger.v3.jaxrs2.resources.UploadResource;
import io.swagger.v3.jaxrs2.resources.UrlEncodedResourceWithEncodings;
import io.swagger.v3.jaxrs2.resources.UserAnnotationResource;
Expand Down Expand Up @@ -4061,4 +4062,85 @@ public void testParameterMaximumValue() {
" '*/*': {}\n";
SerializationMatchers.assertEqualsToYaml31(openAPI, yaml);
}

@Test
public void test4483Response() {
Reader reader = new Reader(new OpenAPI());

OpenAPI openAPI = reader.read(Ticket4483Resource.class);
String yaml = "openapi: 3.0.1\n" +
"tags:\n" +
"- name: Dummy\n" +
" description: Dummy resource for testing setup\n" +
"paths:\n" +
" /test:\n" +
" get:\n" +
" tags:\n" +
" - Dummy\n" +
" description: Dummy GET\n" +
" operationId: dummy\n" +
" responses:\n" +
" \"401\":\n" +
" description: Authentication is required\n" +
" content:\n" +
" application/json:\n" +
" schema:\n" +
" type: array\n" +
" items:\n" +
" $ref: '#/components/schemas/LocalizedError'\n" +
" \"200\":\n" +
" description: test\n" +
" content:\n" +
" application/json:\n" +
" schema:\n" +
" type: object\n" +
" additionalProperties:\n" +
" type: boolean\n" +
" /test/opresp:\n" +
" get:\n" +
" tags:\n" +
" - Dummy\n" +
" operationId: dummyopresp\n" +
" responses:\n" +
" \"401\":\n" +
" description: Authentication is required\n" +
" content:\n" +
" application/json:\n" +
" schema:\n" +
" type: array\n" +
" items:\n" +
" $ref: '#/components/schemas/LocalizedError'\n" +
" \"200\":\n" +
" description: Dummy GET opresp\n" +
" content:\n" +
" application/json:\n" +
" schema:\n" +
" type: object\n" +
" additionalProperties:\n" +
" type: boolean\n" +
" /test/oprespnodesc:\n" +
" get:\n" +
" tags:\n" +
" - Dummy\n" +
" operationId: oprespnodesc\n" +
" responses:\n" +
" \"401\":\n" +
" description: Authentication is required\n" +
" content:\n" +
" application/json:\n" +
" schema:\n" +
" type: array\n" +
" items:\n" +
" $ref: '#/components/schemas/LocalizedError'\n" +
"components:\n" +
" schemas:\n" +
" LocalizedError:\n" +
" type: object\n" +
" properties:\n" +
" code:\n" +
" type: string\n" +
" message:\n" +
" type: string\n";
SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package io.swagger.v3.jaxrs2.resources;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.Map;

/*
*/
@Tag(
name = "Dummy",
description = "Dummy resource for testing setup"
)
@Path("test")
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
@ApiResponses({
@ApiResponse(responseCode = "401", description = "Authentication is required", content = @Content(array = @ArraySchema(schema = @Schema(implementation = Ticket4483Resource.LocalizedError.class))))
})
public class Ticket4483Resource {
@GET
@Operation(
description = "Dummy GET"
)
@ApiResponse(responseCode = "200", description = "test", useReturnTypeSchema = true)
public Map<String, Boolean> dummy() {
Map map = new java.util.HashMap();
map.put("success", Boolean.TRUE);
return map;
}



@Path("/opresp")
@GET
@Operation(
responses = {
@ApiResponse(responseCode = "200", description = "Dummy GET opresp", useReturnTypeSchema = true)})
public Map<String, Boolean> dummyopresp() {
Map map = new java.util.HashMap();
map.put("success", Boolean.TRUE);
return map;
}

@Path("/oprespnodesc")
@GET
@Operation(
responses = {
@ApiResponse(responseCode = "200", useReturnTypeSchema = true)})
public Map<String, Boolean> oprespnodesc() {
Map map = new java.util.HashMap();
map.put("success", Boolean.TRUE);
return map;
}

public static class LocalizedError {
public String code;
public String message;
}
}

0 comments on commit a4cec9b

Please sign in to comment.