Skip to content

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

Open
@jansu76

Description

@jansu76

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)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions