2

I'm working on spring boot(v2.5.6) with dependency openapi-ui(v1.5.2) for swagger UI. I want to set description and other attribute such as required in @schema. The problem is I need to read this value from a property(resource-bundle). Also I want to read other attribute such as min/max/.. from some properties.

I'v tried below code but not worked:

@Schema( description ="{postalCode.description}" ,required ="{postalCode.required}")
String postalCode;

whereas postalCode.description is a property key in a resource bundle.

Is There any way to use some dictionary like properties for swagger?

mhds
  • 31
  • 1
  • 4
  • Looking at [this post](https://stackoverflow.com/questions/32126836/spring-pass-value-from-property-file-to-annotation/32127068) the examples they provide use a $ sign in the attribute like this: `description ="${postalCode.description}"`. Does this work? – D-FENS Dec 04 '21 at 10:59
  • Hello, in your swagger configuration have used @PropertySource to specify the location of your properties files ? can you add your swagger configuration class ? – Cheikh HAIBALA Dec 04 '21 at 11:10
  • @roccobaroccoSC Yes, but didn't work, maybe it need extra configuration – mhds Dec 04 '21 at 11:58
  • I am not sure how does Spring resolve this. It probably interpretes the `${}` in its annotation processor logic. Unfortunately I assume the @Schema annotation does not get processed by Spring, but you could theoretically implement your own annotation processing as described in [this tutorial](https://www.baeldung.com/java-annotation-processing-builder). I have not done it myself. – D-FENS Dec 04 '21 at 12:12
  • Another idea, search if you could make Spring process other annotations as well, if it is at all possible. Just a wild guess. – D-FENS Dec 04 '21 at 12:13
  • @CheikhHAIBALA not worked on model and its fields, but ok for controller – mhds Dec 04 '21 at 15:12
  • @roccobaroccoSC thanks for your reply, I'll try annotations – mhds Dec 04 '21 at 15:13

1 Answers1

0

You can add file with swagger properties and then add configuration for it in Application.java: @PropertySource(value = "classpath:swagger-messages.properties", encoding = "UTF-8").

Then you have to use description ="${postalCode.description}" (add $) and set springdoc.api-docs.resolve-schema-properties to true.

source: https://springdoc.org/faq.html

Patrice333
  • 52
  • 6