swagger-core icon indicating copy to clipboard operation
swagger-core copied to clipboard

Not possible to DRY @Schema annotation

Open jbraga opened this issue 6 years ago • 1 comments

Hi guys,

I'm able to DRY @ApiResponses annotation like so:

@ApiResponses(value = {
    @ApiResponse(
        responseCode = "4XX", description = "Client Error",
        content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(implementation = com.merchant.model.root.response.Error.class)),
        headers = {
            @Header(name = ApiConstants.X_REQUEST_ID,
                description = ApiConstants.Docs.X_REQUEST_ID_DESCRIPTION,
                schema = @Schema(type = ApiConstants.Docs.STRING_TYPE, format = ApiConstants.Docs.UUID_FORMAT))
        }),
    @ApiResponse(
        responseCode = "5XX", description = "Server Error",
        content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(implementation = com.merchant.model.root.response.Error.class)),
        headers = {
            @Header(name = ApiConstants.X_REQUEST_ID,
                description = ApiConstants.Docs.X_REQUEST_ID_DESCRIPTION,
                schema = @Schema(type = ApiConstants.Docs.STRING_TYPE, format = ApiConstants.Docs.UUID_FORMAT))
        })
})
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiErrorResponses { }

That makes it possible to reduce repetition by just adding @ApiErrorResponses annotation to all my endpoints.

However, this approach doesn't currently work with the @Schema annotation. Does anybody know why? And is there a good way of reusing swagger annotations?

Thanks!

jbraga avatar Oct 23 '19 08:10 jbraga

It would be good to use @Schema and @ArraySchema from swagger-annotations in meta-annotation. The alternative to use a schema reference is more complicated to define and makes problem with array after code generation from the generated open-api.

florianhof avatar Feb 16 '22 12:02 florianhof