smallrye-graphql icon indicating copy to clipboard operation
smallrye-graphql copied to clipboard

List of @NonNull is not respected in kotlin data classes.

Open almyy opened this issue 4 years ago • 5 comments

When using kotlin data classes, the @NonNull annotation is not respected for type parameters. For example, this:

data class Something(
    @NonNull
    private val somethingElse: List<@NonNull String>
)

produces the following type:

type Something {
  somethingElse: [String]!
}

Meanwhile, the following java class:

class Something {
    @NonNull
    List<@NonNull String> somethingElse;

    // constructors and getters/setters
}

produces:

type Something {
  somethingElse: [String!]!
}

almyy avatar Nov 13 '20 15:11 almyy

I think that the problem here is that the compiler of Kotlin 1.3 does not store TYPE_USE annotations into regular metadata where Java does it, see https://stackoverflow.com/questions/63093833/how-do-i-access-type-use-annotations-in-kotlin-through-reflection , so SmallRye does not see the annotation where it would be expected.

Kotlin 1.4 should fix it, but Quarkus doesn't support that yet, AFAIK (I assume you're using Quarkus..)

Theoretically we should be able to see the annotation using Jandex even with Kotlin 1.3, because it seems to be stored within the kotlin.Metadata annotation that is synthetically added to the class (that contains the annotated field), but the contents of that annotation look quite wild and I think it may be depending too much on Kotlin internals. To illustrate, this is where I found the annotation - inside a messy array of Strings named d2 (which is a field of the kotlin.Metadata annotation):

d2 = ["Lorg/example/graphql/model/Person;","","()V","somethingElse","","","Lorg/eclipse/microprofile/graphql/NonNull;","somethingElse$annotations","getSomethingElse","()Ljava/util/List;","quarkus-example-graphql"]

jmartisk avatar Nov 18 '20 09:11 jmartisk

Given that Quarkus support for Kotlin 1.4 is in progress (see https://github.com/quarkusio/quarkus/pull/11453) I'd suggest waiting for that, instead of trying to hack it with 1.3

jmartisk avatar Nov 18 '20 09:11 jmartisk

Now that Kotlin 1.4 is well supported in Quarkus, can we have another look at this?

Correct Nullability support is something that currently prevents me from using this library and instead opting for graphql-kotlin, which unfortunately breaks Quarkus' Hot Reload, so I would really like to update to a better supported library.

pothos-dev avatar Mar 25 '21 09:03 pothos-dev

Is there an update to this issue?

robp94 avatar Oct 18 '21 10:10 robp94

https://youtrack.jetbrains.com/issue/KT-13228 If you add the compiler option -Xemit-jvm-type-annotations or for maven do it like this

        <configuration>
          <args>
            <arg>-Xemit-jvm-type-annotations</arg>
          </args>
         </configuration>

The annotation is present in the java byte code and gets picked up. A bit strange is that if you place the annotation inside the list, the list is always non-null for me. But for most use cases this should be ok.

This seems to be not possible.

type Something {
  somethingElse: [String!]
}

code-with-quarkus-graphql-list-kotlin.zip

Maybe you can close the ticket. @jmartisk

robp94 avatar Oct 18 '21 15:10 robp94