Skip to content

[BUG][KOTLIN] No JsonAdapter for LocalDateTime in Moshi serializer #1271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
toXel opened this issue Oct 19, 2018 · 5 comments · Fixed by #2981
Closed

[BUG][KOTLIN] No JsonAdapter for LocalDateTime in Moshi serializer #1271

toXel opened this issue Oct 19, 2018 · 5 comments · Fixed by #2981

Comments

@toXel
Copy link

toXel commented Oct 19, 2018

Description

date-time fields can't be parsed from a generated Kotlin client with default configuration. It fails with this exception:
java.lang.IllegalArgumentException: Platform class java.time.LocalDateTime annotated [] requires explicit JsonAdapter to be registered

The reason for it is that in Serializer.kt.mustache Moshi only gets a JsonAdapter for the java.util.Date type but not for java.time.LocalDateTime (or org.threeten.bp.LocalDateTime when ThreeTen is used).

openapi-generator version

v3.3.1

Command line used for generation

generate -g kotlin -i https://petstore.swagger.io/v2/swagger.yaml -o swaggerClientTest

Steps to reproduce

It can be reproduced with the petstore swagger spec.

  1. Generate Kotlin client with the command above.
  2. Write a simple test like this:
@Test
fun getOrderById() {
    val order = StoreApi().getOrderById(1)
    assertNotNull(order)
}
  1. It will fail with the respective exception.
Suggest a fix/enhancement

A workaround is to implement a JsonAdapter for the respective type and add it to Moshi in the generated Serializer class. Maybe this can be integrated in the generator.

@toXel toXel changed the title [KOTLIN-CLIENT] No JsonAdapter for LocalDateTime in Moshi Serializer [KOTLIN-CLIENT] No JsonAdapter for LocalDateTime in Moshi serializer Oct 19, 2018
@toXel toXel changed the title [KOTLIN-CLIENT] No JsonAdapter for LocalDateTime in Moshi serializer [BUG][KOTLIN] No JsonAdapter for LocalDateTime in Moshi serializer Dec 10, 2018
@krevelen
Copy link

same for java.util.UUID

@jpmeijers
Copy link

I'm hitting this same issue with the OffsetDateTime type.

Applicable line in generated code looks like this:

    /* Absolute time when... */
    @Json(name = "absolute_time")
    val absoluteTime: java.time.OffsetDateTime? = null

Applicable lines in api.swagger.json:

        "absolute_time": {
          "type": "string",
          "format": "date-time",
          "description": "Absolute time when ..."
        }

And the exception that moshi is throwing:

    java.lang.IllegalArgumentException: Platform class java.time.OffsetDateTime requires explicit JsonAdapter to be registered
    for class java.time.OffsetDateTime absoluteTime

@ghost
Copy link

ghost commented Jun 30, 2021

@jpmeijers did you find out how to use OffsetDateTime with Moshi? I have been trying to write a custom adapter but still getting errors. Thanks

@jpmeijers
Copy link

I'm not 100% sure as it's a while back, but looks like I created a custom adapter:
https://github.com/ttnmapper/ttnmapper-android-v3/blob/master/app/src/main/java/org/ttnmapper/phonesurveyor/model/ttnV3/OffsetDateTimeAdapter.kt

And then added that to the Moshi instance:

        val moshi = Moshi.Builder()
//                .add(KotlinJsonAdapterFactory())
                .add(OffsetDateTimeAdapter())
                .build()

@ghost
Copy link

ghost commented Jul 1, 2021

Thanks for the response! I was able to get it working :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants