Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #3134
Closed
Open
Issue created Jun 10, 2019 by Administrator@rootContributor4 of 6 checklist items completed4/6 checklist items

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

Created by: 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)

Assignee
Assign to
Time tracking