Skip to content

Request Body cannot be configured as optional #603

Closed
@sobafuchs

Description

@sobafuchs

Describe the bug

  • If you are reporting a bug, please help to speed up problem diagnosis by providing as much information as possible:
  • A clear and concise description of what the bug is: the title of an issue is not enough
    The current behavior seems to parse whether or not a request body is optional from one of several values, one of which is the spring boot @RequestBody annotation. The problem is, even if you set required to false, springdoc-openapi still registers it as required. I've looked at the code and I suspect the reason is that it simply checks if the @RequestBody annotation exists but doesn't check the required value.

To Reproduce
Steps to reproduce the behavior:

  • What version of spring-boot you are using?

2.2.6

  • What modules and versions of springdoc-openapi are you using?

org.springdoc:springdoc-openapi-ui:1.3.4
org.springdoc:springdoc-openapi-kotlin:1.3.4

  • What is the actual and the expected result using OpenAPI Description (yml or json)?

Sorry I don't understand the question

  • Provide with a sample code (HelloController) or Test that reproduces the problem

You can configure any POST endpoint and set the @RequestBody(required = false) and it will still display the request body as required.

@RestController
class TestController {
    @PostMapping("/lol")
    public void test(@RequestBody(required = false) Body body) { }

    public class Body {
        public String field;
    }
}

In the screenshots section, I've attached how the UI still displays the body as required, even though its set to false

Expected behavior
The UI should display the request body as not required.

  • What is the expected result using OpenAPI Description (yml or json)?
openapi: 3.0.1
paths:
  /lol:
    post:
      tags:
      - External Order Number API
      operationId: test
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Body'
        required: false

Screenshots
If applicable, add screenshots to help explain your problem.
Screenshot from 2020-04-23 13-40-05

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions