0
votes

I'm using Java 11.0.2, Spring Boot 2.2.6 and Spring OpenApi Core 1.1.49 to create an OpenApi documentation using annotations.

During the request for creating a merchant in the Controller I need to have a custom header property, but which needs to be optional. Based on Swagger documentation for Parameter Object, the field "required" (Determines whether this parameter is mandatory. If the parameter location is "path", this property is REQUIRED and its value MUST be true. Otherwise, the property MAY be included and its default value is false.) by default for header is false, but below you can see that for some reason is true (nevertheless that I configured this option to "false").

Java - part ot Controller method

public ResponseDto create(@Parameter(in = ParameterIn.HEADER, required = false, schema = @Schema(type = "string", format = "uuid"), name = "X-Request-Correlation-Id", @RequestHeader("X-Request-Correlation-Id") @Nullable String headerXRequestId, ... <

This result in OpenApi yaml file - autogenerated with the information from the annotations

parameters: - name: X-Request-Correlation-Id in: header required: true schema: type: string format: uuid

Can you point out the problem, because I can't find a solution in the documentation or anywhere else?!

1

1 Answers

1
votes

Found the solution - the problem wasn't in OpenApi annotation @Parameter, but in Spring binding annotation @RequestHeader, which binds the header field to the method parameter. @RequestHeader has also field "required" and by default, it's set on "true" and it overrides the one in @Parameter. So the solution was the following syntax - @RequestHeader(name = "X-Request-Correlation-Id", required = false).