question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[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

See original GitHub issue

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"

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:13
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
mikand13commented, Aug 11, 2021

👍

2reactions
patiramyadavcommented, Aug 11, 2021

When I add requestBody it works and I see generated code as follows:

      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/MockRequest'
        required: false

Then

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

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

Changed to

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

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

The problem is Unit as request type when no explicit requestBody is added in specification of openAPI.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Platform class kotlin.Unit requires explicit JsonAdapter to be ...
In a retrofit interface I have defined a method return type as ApiModel < Unit >. but when I call the method I...
Read more >
[Solved]-Generics with moshi and retrofit-kotlin
I'm not sure what the best way to do this is, but the way I've done it is with KotlinJsonAdapterFactory. Create a data...
Read more >
How to create a generic Moshi JsonAdapter that returns ...
It makes it easy to parse JSON into Java and Kotlin classes. The real-life problem. Recently I faced an issue where one of...
Read more >
react router catch all 404 Code Example
Error : Node Sass version 5.0.0 is incompatible with ^4.0.0. ... Changing columns for table "users" requires Doctrine DBAL. Please install the ...
Read more >
Retrieving reified generic arguments - hrach.dev
Kotlin has a great feature that will allow you to preserve generic type T for further work, not only type resolution.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found