0

I am working with springdoc-openapi-ui swagger. I don't want to hard code the values for swagger documentation. I want to read such values from the properties file.

When I tried that I am getting compilation error The value for annotation attribute Operation.summary must be a constant expression.

I know it is looking for constant expression, but I don't want to hard code these values in my code.

enter image description here

Please find my controller code here

@RestController
@PropertySource("classpath:test.properties")
public class TestController {

    @Autowired
    private TestService testService;

    @Autowired
    private static Environment environment;

    final String SUMMARY = environment.getProperty("operationSummary");

    @Operation(summary = SUMMARY, description = "Returns a list", tags = { "Test" })
    @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Successful operation") })
    @GetMapping(path = "/description/{id}")
    public List<Test> getDescriptionById(@PathVariable String id) {

        return testService.getDescriptionById(id);
    }
}

Is there a way to add message properties for different languages in the end point annotations?

Shababb Karim
  • 3,614
  • 1
  • 22
  • 35
SSK
  • 3,444
  • 6
  • 32
  • 59
  • 2
    You are trying to do something that's not related to swagger, its related to Java language feature. You definitely can't use runtime values in annotations in java. An easy alternative may be to keep a class named `SwaggerOperationProps` with `public final` fields – Shababb Karim Jun 10 '20 at 06:17
  • @Shababb Karim Thanks for your time. I got your point. Actually I need to use external content for `I18N` based on the client preference – SSK Jun 10 '20 at 06:44

2 Answers2

1

Happy to share the answer, so this will be useful for others
Finally, I got the answer to use external properties in swagger documentation.

We can externalize the documentations by creating properties or yaml file and use as propertysource.
Then we can use the key as ${propertyname} in the swagger annotations as shown below.

@Tag(name = "Person Data")
@PropertySource("classpath:person-data-controller.properties")
public class PersonDataController {

    @Operation(summary = "${person.summary}", description = "${person.description}")
    public getPersonData(){
    
    }
}

We can also add @PropertySource("classpath:person-data-controller.properties") on SwaggerConfiguration class to avoid repeataion on each controller

SSK
  • 3,444
  • 6
  • 32
  • 59
  • I'm facing the same problem, but in my case the solution that you proposed doesn't work (probably my fault). Could you please share an example of code? – diogenes_vz Jul 12 '21 at 15:13
  • @SSK but have you have configured it for multiple languages – John Jan 24 '22 at 10:57
0

You have the spring properties support with swagger annotations:

  • The support of spring property resolver for @Info: title - description - version - termsOfService
  • The support of spring property resolver for @Info.license: name - url
  • The support of spring property resolver for @Info.contact: name - email - url
  • The support of spring property resolver for @Operation: description - summary
  • The support of spring property resolver for @Parameter: description - name
  • The support of spring property resolver for @ApiResponse: description
  • Its also possible to declare security URLs for @OAuthFlow: openIdConnectUrl - authorizationUrl - refreshUrl - tokenUrl
  • The support of spring property resolver for @Schema: name - title - description , by setting springdoc.api-docs.resolve-schema-properties to true