Skip to content

Incorrect schema addition on header if used in Spring mapping #869

Description

@Huo12345

Describe the bug
When headers are used for mapping a request to a controller method, springdoc-openapi adds an enum with just an empty string to the swagger schema definition.

To Reproduce
Steps to reproduce the behavior:
I use spring- boot 2.1.7 with springdoc-openapi 1.4.6. The code below works as expected:

    @GetMapping
    public String getMessageFromHeader(
            @Parameter(name = "myHeader", description = "A header", schema = @Schema(type = "int32"))
            @RequestHeader("myHeader") Integer header
    ) {
        return "bar " + header;
    }

and produces the following output:

parameters: [
  {
    name: "myHeader",
    in: "header",
    description: "A header",
    required: true,
    schema: {
      type: "int32"
    }
  }
]

But when adding the headers as a requirement for the mapping in spring, the schema definition changes in an undesirable way:

    @GetMapping(headers = {"myHeader"})
    public String getMessageFromHeader(
            @Parameter(name = "myHeader", description = "A header", schema = @Schema(type = "int32"))
            @RequestHeader("myHeader") Integer header
    ) {
        return "bar " + header;
    }

The schema now contains an enum with an empty string as single entry.

parameters: [
  {
    name: "myHeader",
    in: "header",
    description: "A header",
    required: true,
    schema: {
      type: "int32",
      enum: [
        ""
      ]
    }
  }
]

When defining a list of allowed values in the schema, the generation fails completly:

    @GetMapping(headers = {"myHeader"})
    public String getMessageFromHeader(
            @Parameter(name = "myHeader", description = "A header", schema = @Schema(allowableValues = {"foo", "bar"}))
            @RequestHeader("myHeader") String header
    ) {
        return "bar " + header;
    }

Response of call to /v3/api-docs/:

{
  status: "500",
  code: "INTERNAL_SERVER_ERROR",
  message: "My custom error message for 500 errors.",
  detail: null
}

Expected behavior
Adding a header to the routing condition in spring should not impact the defined schema for the header parameter in any other way than marking it as required.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions