OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Kotlin] Kotlin-client with retrofit2 doesn't generate nullable parameters

Open
#5,859 0 comments 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?
Description

The kotlin-client generator when using Retrofit2 library generates a DefaultApi.kt file that doesn't mark optional parameters as nullable.

openapi-generator version

openapi-generator-cli/4.3.0

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  version: 0.0.1
  title: Optional Parameter Demo
paths:
  /foo:
    get:
      parameters:
        - in: query
          name: optional
          required: false
          schema:
            type: integer
        - in: query
          name: mandatory
          required: true
          schema:
            type: integer
      responses:
        200:
          description: ''
          content:
            text/plain:
              schema:
                type: integer
Command line used for generation

java -jar .\openapi-generator-cli-4.3.0.jar generate -i ".\foo.yaml" --artifact-id kotlin-client -g kotlin -o generated --library jvm-retrofit2

Steps to reproduce
  1. Run the above command on the declaration file provided

Result will be:

package org.openapitools.client.apis

import org.openapitools.client.infrastructure.CollectionFormats.*
import retrofit2.http.*
import retrofit2.Call
import okhttp3.RequestBody
import okhttp3.ResponseBody
import okhttp3.MultipartBody


interface DefaultApi {
    @GET("/foo")
    fun fooGet(@Query("mandatory") mandatory: kotlin.Int, @Query("optional") optional: kotlin.Int): Call<kotlin.Int>

}
Related issues/PRs

#5198
#2012

Suggest a fix

Optional parameter should be @Query("optional") optional: kotlin.Int?

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 supplied foo.yaml and run the openapi-generator-cli command using the kotlin generator with the jvm-retrofit2 library. Inspect how DefaultApi.kt is generated for the optional and mandatory query parameters; done means the optional parameter is nullable while the mandatory parameter remains non-nullable.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.