How to reuse @ArraySchema description in SpringDoc

Viewed 793

I've got several rest-dtos that have identical field - list of currencies represented by CurrencyUnit (java-money)

import javax.money.CurrencyUnit;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Schema;

...

@ArraySchema(schema = @Schema(type = "string"),
            arraySchema = @Schema(
                    example = "[\"USD\",\"CAD\"]",
                    description = "Currencies supported by store according to ISO_4217."
            ))
private List<CurrencyUnit> currencies;

What's the correct way to extract all this metadata into single @CurrencyCodesList annotation to reuse across multiples models?

1 Answers

This does not seem to be supported in swagger-core annotations. If you want to reuse your annotations, this is the straight forward solution using springdoc-openapi.

  1. Declare your custom schema in the level of the OpenAPI bean
@Bean
public OpenAPI customOpenAPI() {
        String currenciesSample[] = { "USD", "ILS" };
        return new OpenAPI().components(new Components()
                        .addSchemas("CurrencyList", new ArraySchema().type("string")
                                        .example(currenciesSample).description("Currencies supported by store according to ISO_4217.")
                        ));
}
  1. Reference this schema in your classes:
@Schema(ref = "CurrencyList")
private List<CurrencyUnit> currencies;
Related