OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.31k stars 6.45k forks source link

[BUG] [Kotlin] Optional parameters #2012

Open dlazerka opened 5 years ago

dlazerka commented 5 years ago

Bug Report Checklist

Description

Client code generated: It is impossible to not provide an optional param to an API.

openapi-generator version

4.0.0-SNAPSHOT

OpenAPI declaration file content or url
swagger: "2.0"
info:
  version: "2.1.4"
  title: "Test"
  description: ""
host: "example.cm"
basePath: "/2_0"
schemes:
  - "https"
paths:
  /data/foo:
    get:
      operationId: "getFoo"
      produces:
        - "application/json"
      parameters:
        - name: "t0"
          in: "query"
          required: false
          type: "string"
          format: "date-time"
          description: "Filter by start time, e.g. '2018-03-01T00-00'"
      responses:
        200:
          description: "200 response"
          schema:
            type: "string"
Command line used for generation

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -i test.yaml -g kotlin -o tmp/kotlin/

Steps to reproduce
  1. Generate the code.
  2. Open vi tmp/kotlin/src/main/kotlin/org/openapitools/client/apis/DefaultApi.kt
  3. See getFoo argument type.
Suggest a fix

Arguments for which spec says "required: false", should be optional (followed by ? in Kotlin).

auto-labeler[bot] commented 5 years ago

👍 Thanks for opening this issue! 🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

wing328 commented 5 years ago

I think ? is for nulalble type instead: https://kotlinlang.org/docs/reference/null-safety.html

karismann commented 5 years ago

@wing328 this issue can be closed as it has been fixed by the related PR with 4.0.0

isycat commented 4 years ago

Heads up, the issue has not been fixed:

  1. Omitting the "= null" from "paramName: TypeName? = null" in method signatures forces the consumer to specify null as the value, which makes the "required: false" parameter effectively required, albeit nullable.

  2. The implementation does not handle nullable header values val localVariableHeaders: MutableMap<String, String> = mutableMapOf("headerName" to headerName.toString(),... see petstore This passes the string "null" downstream.

no. 2 is pretty major, as it renders the implementation unusable for APIs that don't handle the "null" header value, e.g. A pretty standard implementation of kotlin-spring, where the value of 'optionalParam' in @RequestHeader(value="optionalHeader", required=false) optionalHeader: String?, comes through as the string "null"

Edit: I see the multiplatform template has null-friendly code, but the okhttp template has the issue in no. 2 above

isycat commented 4 years ago

I have submitted a small PR re. the above: https://github.com/OpenAPITools/openapi-generator/pull/7341

Who should I @? Someone/everyone from the kotlin technical committee? Or is posting here enough?

grEvenX commented 1 year ago

@wing328 This issue can probably be closed?

ablil commented 3 months ago

Im using version 7.1.0 and this issues is still there, generated params are not optional (nullable)

A workaround that works for me: declare the params schema, then reference it afterward on the query param