Openapi-generator: [KOTLIN][SPRING] Required requestBody should not be optional

Created on 30 Dec 2018  路  2Comments  路  Source: OpenAPITools/openapi-generator

Description

In my operation definition, I have a requestBody with required: true. However, the generated code makes the parameter in the controller and subsequent service optional.

openapi-generator version

I tried 3.3.4 and current master

OpenAPI declaration file content or url

Spec:

paths:
  /sysmails/{key}:
    parameters:
      - name: key
        in: path
        required: true
        schema:
          $ref: "#/components/parameters/SysMailKey"
    put:
      operationId: updateSysMailAtKey
      description: Update SysMail template
      requestBody:
        description: Template data
        required: true
        content:
          application/json:
            schema:
              title: SysMailFormData
              description: Data to update a SysMail template.
              type: object
              required:
                - subject
                - body
              properties:
                subject:
                  $ref: "#/components/schemas/SysMailSubject"
                body:
                  $ref: "#/components/schemas/SysMailBody"

Generated code:

    @RequestMapping(
            value = ["/sysmails/{key}"],
            produces = ["application/json"], 
            consumes = ["application/json"],
            method = [RequestMethod.PUT])
    fun updateSysMailAtKey( @PathVariable("key") key: String, @Valid @RequestBody sysMailFormData: SysMailFormData?): ResponseEntity<SysMail> {
        return ResponseEntity(service.updateSysMailAtKey(key, sysMailFormData), HttpStatus.OK)
    }

Parameter SysMailFormData is optional, which it shouldn't.

Command line used for generation
java -jar openapi-generator-cli.jar \
    generate \
    --input-spec ./sysmails-1.x.yml \
    --output ../ \
    --generator-name kotlin-spring \
    --config config.kotlin.json \
    --enable-post-process-file \
    --model-name-suffix Dto

config.kotlin.json

{
  "serviceInterface": true,
  "swaggerAnnotations": false,
  "serviceImplementation": true,
  "gradleBuildFile": false
}
Steps to reproduce

Run generator with configuration above

Related issues/PRs

Here someone reported the other way round: required:false resulted in non-optional parameters.

https://github.com/OpenAPITools/openapi-generator/issues/1106

Suggest a fix/enhancement

The code template should be adjusted.

Bug Kotlin

All 2 comments

@mindhaq Thanks for reporting this! I'm working on the issue.

@mindhaq https://github.com/OpenAPITools/openapi-generator/pull/1847 has been merged into master so please give it a try with latest master when you have time. 馃槈

Was this page helpful?
0 / 5 - 0 ratings