OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Kotlin] 5.2.0 generic request type cause issue Platform class kotlin.Unit requires explicit JsonAdapter to be registered if requestBody is not provided

Open
#10,126 5 comments 13 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

When using latest version 5.2.0. I have tested with 5.2.1-SNAPSHOT and has still this issue. This works with 5.1.1 version.

java.lang.IllegalArgumentException: Platform class kotlin.Unit requires explicit JsonAdapter to be registered
	at com.squareup.moshi.ClassJsonAdapter$1.create(ClassJsonAdapter.java:75)
	at com.squareup.moshi.Moshi.adapter(Moshi.java:145)
	at com.squareup.moshi.Moshi.adapter(Moshi.java:105)
	at com.squareup.moshi.Moshi.adapter(Moshi.java:79)

Spec:

openapi: 3.0.1
paths:
  /v1/abcs/{id}/cancel:
    patch:
      tags:
        - Abcs
      operationId: cancelAbc
      parameters:
        - name: id
          in: path
          required: true
          schema:
            type: string
            format: uuid
      responses:
        "200":
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/MockResponse'
    MockResponse:
      required:
        - response
      type: object
      properties:
        response:
          $ref: '#/components/schemas/Response'
 components:
   schemas:     
      Response:
            required:
              - confirmationId
            type: object
            properties:
              id:
                type: string
                format: uuid
              confirmationId:
                type: string

Note: no requestBody for patch.

Generated code:

    suspend fun cancelAbc(id: java.util.UUID) : MockResponse{
        val localVariableConfig = cancelAbcRequestConfig(id = id)

        val localVarResponse = request<Unit, MockResponse>(
            localVariableConfig
        )
       .....
}

data class MockResponse (
    @Json(name = "response")
    val response: Response
)

Here, the request type is Unit.
while 5.1.1 generates only request<MockResponse> no request type.

Extra related dependencies:

    implementation("com.squareup.moshi:moshi-kotlin:1.12.0")
    implementation "com.squareup.okhttp3:okhttp:4.9.0"

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

Compare the generated Kotlin client for the supplied OpenAPI operation between versions 5.1.1 and 5.2.0, focusing on cancelAbc and its request<Unit, MockResponse> call. Trace the generator entry point that chooses the request type for operations without a requestBody, then add or update a generation test showing that the no-body operation does not require a Kotlin Unit adapter.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.