Home > Back-end >  Is it possible to group OpenAPI ApiResponse in error status?
Is it possible to group OpenAPI ApiResponse in error status?

Time:06-05

I want to document my Spring Boot Rest APIs with OpenAPI and swagger. The problem I have is that I have to add ApiResponse annotation for each possible response code and repeat that for every single API:

@Operation(summary = "Create new Address")
@ApiResponses(value = {
        @ApiResponse(responseCode = "201", description = "New Address created", content = {
                @Content(mediaType = "application/json", schema = @Schema(implementation = AddressResponse.class))
        }),
        @ApiResponse(responseCode = "400", description = "Invalid input supplied", content = {
                @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class))
        }),
        @ApiResponse(responseCode = "401", description = "Unauthorized", content = {
                @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class)),
        }),
        @ApiResponse(responseCode = "404", description = "Not Found", content = {
                @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class)),
        }),
        @ApiResponse(responseCode = "409", description = "Conflict", content = {
                @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class)),
        }),
        @ApiResponse(responseCode = "422", description = "Unprocessable Entity", content = {
                @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class)),
        }),
        @ApiResponse(responseCode = "500", description = "Internal Server Error", content = {
            @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class)),
    }),            
})

Is there a way to combine all ApiResponses for "400, 401, 404, 409, 422, 500 and default" together? Something like this:

@Operation(summary = "Create new Address")
@ApiResponses(value = {
        @ApiResponse(responseCode = "201", description = "New Address created", content = {
                @Content(mediaType = "application/json", schema = @Schema(implementation = AddressResponse.class))
        }),
        @ApiResponse(responseCode = "400,401,404,409,422,500,default", description = "Error Happened", content = {
                @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class))
        }),           
})

CodePudding user response:

short answer, No. As stated in the openapi spec direct children of responses object are https statuses for each response. However you can use tags for grouping your operations (and not responses)

  • Related