apollographql / apollo-kotlin

:rocket:  A strongly-typed, caching GraphQL client for the JVM, Android, and Kotlin multiplatform.
https://www.apollographql.com/docs/kotlin
MIT License
3.74k stars 653 forks source link

Object with all null fields drops e: Cannot be cast to non-null type kotlin.String #4010

Closed djordjeh closed 2 years ago

djordjeh commented 2 years ago

Summary If I receive the object with all nullable fields as null apollo drops NullPointException, cannot be cast to non-null type kotlin.String

Version Version 3.1.0

Description For instance I have object which contains three nullable strings. If I receive object like this: "featuredTag": { "title": null, "slug": null, "color": null },

Apollo drops the exception. All fields inside are nullable. I was debugging line by line and noticed that exception occurs during the emit of flow. I know in perfect word I should receive object featuredTag as null and then everything works, but sometimes from some reason our API returns as above and our app is not able to parse the response.

Thank you for any help, Cheers

BoD commented 2 years ago

Hi! 👋

To understand what the problem could be, would it be possible for you to show the schema file for this service? It would also help a lot to see the actual stacktrace of the exception. Thanks!

djordjeh commented 2 years ago

Hi @BoD

Thank you for your response. How do you want me to send you schema file? The exception is classic NullPointException but I will prepare stacktrace of the exception.

BoD commented 2 years ago

You can attach the schema to this issue in a comment - or if that's preferable you can also send it privately to my email: benoit.lubek@apollographql.com.

Don't hesitate to also share the Query you're trying to execute and any other details that may help understand the issue.

Thank you!

martinbonnin commented 2 years ago

Hi @djordjeh any news about this? Were you able to solve the issue or can we help with anything?

djordjeh commented 2 years ago

Hi @martinbonnin I've just send email to: benoit.lubek@apollographql.com with schema and query we are using. Thank you for your help and I apologize for late response.

martinbonnin commented 2 years ago

Thanks! We'll look into that!

himanshufoodpanda commented 2 years ago

@martinbonnin I am facing same issue that in schema,we have a field as below tags:[String!]

but During parsing Json from Response we get 2 different scenario 1-tags:null working fine during parsing from Apollo 2- tags:[ ] fails on StringAdapter() file during parsing

As we know ! denotes that it will not be null elements like tags:[null,"element1",null,"element2"]

.So 2nd option should also works fine. Empty Array should be easliy parsed an emptyList().

martinbonnin commented 2 years ago

@himanshufoodpanda thanks for sending the details! I added a test there: https://github.com/apollographql/apollo-kotlin/pull/4103. Unfortunately, I could not reproduce :(. So looks like there's more to it. Can you share your schema and Gradle configuration? (edit: also the full stacktrace would help a lot)

martinbonnin commented 2 years ago

@himanshufoodpanda anything else we can do here ? Can you modify the test to make it fail by any chance ?

    val mockServer = MockServer()
    mockServer.enqueue("""
      {
        "data": {
          "list": []
        }
      }
    """.trimIndent())
    val apolloClient = ApolloClient.Builder()
        .serverUrl(mockServer.url())
        .build()

    val response = apolloClient.query(ListQuery()).execute()
BoD commented 2 years ago

Closing for now, don't hesitate to re-open if you can reproduce the issue.

tekinarslan commented 1 year ago

Hi All, We have same problem. Summary My class items are nullable but I got cannot be cast to non-null type kotlin.String. Our schema item is like that "type": { "kind": "SCALAR", "name": "String", "ofType": null },

Version Version 3.4.0

Description For instance I have object which contains nullable strings. If I receive object like this:

    [
      {
        "slug": "city-trips",
        "title": "City Trips"
      },
      {
        "slug": "no-test",
        "title": "No test"
      },
      {
        "slug": "europe",
        "title": "Europe"
      },
      {
        "slug": "february",
        "title": "February"
      },
      {
        "slug": null,
        "title": null
      }
    ]

Apollo drops the exception.

Thank you for any help

BoD commented 1 year ago

Hi @tekinarslan . In order to understand the issue, could you provide the full schema, the query, and the full payload you're receiving? If available, the full stacktrace of the exception would also help.

tekinarslan commented 1 year ago

I can attach schema, query and response json. About exception, I also attached stacktrace as json file. I you need more thing, please let me know. error.zip

BoD commented 1 year ago

@tekinarslan Thank you. From the stacktrace it appears that the exception appears inside a class in your project named ContentfulCacheModule.kt.

tekinarslan commented 1 year ago

Hello, I found that I got error from cacheKeyGenerator in my ContentfulCacheModule. When cache key is null on response payload, my cache key get error. I think it is solved. I will let you know again after testing.