OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA] Optional(default) requestBody produces Spring REST parameter annotated with RequestBody(required = true)

Open
#3,134 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

If I am not mistaken, request bodies should be optional by default (https://swagger.io/docs/specification/describing-request-body/). Spring / Java code generator creates endpoint that has plain @RequestBody annotation, which means by default required = true and behaves accordingly.

With the generated endpoint, attempting to not provide request body causes HttpMessageNotReadableException:

org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public org.springframework.http.ResponseEntity<java.lang.Void> org.my.restapi.openapi.FooApiController.fooIdBarPost(java.lang.String,org.my.restapi.openapi.model.InlineObject11)

Also posted at https://stackoverflow.com/questions/56524143/should-i-be-able-to-define-a-post-resource-with-optional-message-body

openapi-generator version

Tested with 3.3.4 and 4.0.1 versions of https://mvnrepository.com/artifact/org.openapitools/openapi-generator-gradle-plugin

OpenAPI declaration file content or url

(excerpt)

  /foo/{id}/bar:
      post: # ok
          parameters:
              - in: path
                name: id
                schema:
                    type: string
          requestBody:
              required: false
              content:
                  application/json:
                      schema:
                          type: object
                          properties:
                              bazuu:
                                  type: string
          responses:
              '200':
                  description: ok
Command line used for generation

used gradle plugin

Steps to reproduce

created classes with build.gradle

openApiGenerate {
    generatorName = "spring"
    inputSpec = "$projectDir/src/main/resources/openapi-definition.yaml".toString()
    outputDir = "$buildDir/generated-sources".toString()
    apiPackage = "org.my.restapi.openapi"
    modelPackage = "org.my.restapi.openapi.model"
    systemProperties = [
        modelDocs: "false",
        apis: "", 
        models: ""
    ]
    configOptions = [
        interfaceOnly: "true"
    ]
}
Related issues/PRs
Suggest a fix

Optional requestBody should produce parameter annotated with RequestBody(required = false)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Spring generator configuration in build.gradle and reproduce the issue with the provided OpenAPI YAML. Inspect the generated FooApiController endpoint and the Spring request-body generation path. Done means an optional requestBody generates the corresponding optional Spring annotation and accepts a request without a body.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, spring
Domain
api, backend, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.