Openapi generator: Central defintion of error response description and content #44113
-
In my project, I automatically generated the openapi.yaml from the Java classes. But to achieve this I have repeated annotations like
again and again for all endpoints. For most endpoints, the description and content depend only on the error code. Of course it should still be possible to set individual values for responseCode = "200". |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
/cc @EricWittmann (openapi), @MikeEdgar (openapi), @phillip-kruger (openapi) |
Beta Was this translation helpful? Give feedback.
-
@PhilippKemkes you can use the @OpenAPIDefinition(
components = @Components(
responses = {
@APIResponse(
name = "BadRequest",
description = "a description",
content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(implementation = ErrorMessage.class))),
@APIResponse(
name = "Unauthorized",
description = "another description")
}
)
) Then on your resource classes or individual resource methods: @APIResponse(responseCode = "400", ref = "BadRequest")
@APIResponse(responseCode = "401", ref = "Unauthorized") There is still some repetition, but note you can place them at the class level to apply to all endpoints within. |
Beta Was this translation helpful? Give feedback.
@PhilippKemkes you can use the
@OpenAPIDefinition
annotation, which has acomponents.responses
array to define your@APIResponse
s.@OpenAPIDefinition
should be placed in apackage-info.java
or on a JAX-RSApplication
class in order to be found by the scanner. Each@APIResponse
should have aname
that will be its key withincomponents
in the resulting OpenAPI.