Unable to set header parameter to optional

Originally posted by Blago on May 13, 2020

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. The issue:

During the request for creating a client in the Controller I need to have a custom x-request header, 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

create(@Parameter(in = ParameterIn.HEADER, required = false,        schema = @Schema(type = "string", format = "uuid"), name = "X-Request-Correlation-Id", description = "Request ID, random string for debugging purposes", example = "77e1c83b-7bb0-437b-bc50-a7a58e5660ac")                          @RequestHeader("X-Request-Correlation-Id") @Nullable String headerXRequestId,

OpenApi yaml file - autogenerated by the info from the annotations

parameters:   - name: X-Request-Correlation-Idin: headerdescription: Request ID, random string for debugging purposesrequired: trueschema:type: stringformat: uuidexample: 77e1c83b-7bb0-437b-bc50-a7a58e56Can you please point out the problem, because I can't find the solution in the documentation or anywhere else?!

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

Originally posted by Blago on May 14, 2020

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).