graphql-java-codegen icon indicating copy to clipboard operation
graphql-java-codegen copied to clipboard

Custom annotations for nullable types

Open joffrey-bion opened this issue 5 years ago • 2 comments

Is your feature request related to a problem? Please describe.

There is currently a way to generate a custom annotation on non-null types (modelValidationAnnotation), but there is no equivalent for nullable ones.

I think modelValidationAnnotation was designed for the use case of enforcing non-nullability during actual runtime validation, but I would need a more general use case: generate any set of custom annotations for non-null and for nullable types.

My particular use case is to use JetBrains annotations org.jetbrains.annotations.Nullable and org.jetbrains.annotations.NotNull, so that I get proper inspections when using the generated types (to avoid NPEs at runtime).

Describe the solution you'd like

A generalization of modelValidationAnnotation, and a counter part for nullable types.

For instance, we could have the following 2 configs:

  • annotationsForNullableFields: a list of fully qualified annotation class names to add on all nullable fields
  • annotationsForNonNullFields: a list of fully qualified annotation class names to add on non-null fields.

The existing behaviour of modelValidationAnnotation would be superseded by annotationsForNonNullFields. Also, we could both use javax.validation annotations and other nullability annotations at the same time if we allow a list here.

Note that this should apply to both input and output types, and it would be useful to generate the annotations on the corresponding getters/setters as well.

Describe alternatives you've considered

In order to achieve this currently, we would need to use customAnnotationsMapping on every single nullable field (which is definitely not practical).

joffrey-bion avatar Sep 25 '20 12:09 joffrey-bion

This is especially useful for interoperability with Kotlin, where users expect a bit more null-safety.

Without the Java non/nullable annotations, Kotlin defaults to using platform types.

(Edit): Oh! I just saw this plugin supports generating Kotlin types directly. I'll definitely give that a shot. But it would be nice if Java folks get this too. 😊

(Update: Jun 8): Unfortunately the kotlin generator doesn't add JSONB annotations to the kotlin data class constructor, which means I can't use the output with Quarkus (SmallRye) GraphQL.

NfNitLoop avatar May 06 '23 00:05 NfNitLoop