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

allow Coercing.parseLiteral to return null (remove @NonNull annotation)

Open sgerke-1L opened this issue 3 years ago • 2 comments

Coercing.parseLiteral is declared to return a @NonNull value.

However, a coercing may want to return null for the NullValue. See for example the ObjectScalar implementation in https://github.com/graphql-java/graphql-java-extended-scalars

sgerke-1L avatar Jul 29 '22 08:07 sgerke-1L

Hmm this is an intersting one - on the one hand the code is like that to say

  • always return a value
  • Most of the time there should be a value
  • throw CoercingParseLiteralException if there is a problem in the conversion

I guess that the AST NullValue is mostly like going to result in a scalr saying its raw value for that is null

bbakerman avatar Aug 03 '22 11:08 bbakerman

I found this previous discussion: https://github.com/graphql-java/graphql-java/issues/1362 Somehow the @NonNull was removed from serialize but not from parseLiteral.

sgerke-1L avatar Aug 05 '22 10:08 sgerke-1L

This has been fixed

bbakerman avatar Aug 24 '22 23:08 bbakerman

Thanks for addressing this with #2912

sgerke-1L avatar Aug 25 '22 07:08 sgerke-1L

sorry to open it again, but the implementation of ObjectScalar is actually wrong: we never pass in NullValue (it is automatically converted to Java null).

andimarek avatar Aug 26 '22 00:08 andimarek

@sgerke-1L can you explain why you would want to return null for not null literal value? thanks

andimarek avatar Aug 26 '22 00:08 andimarek

Motivation was the handling of NullValue in ObjectScalar. The null return was causing problems with spotbugs validation.

sgerke-1L avatar Aug 26 '22 09:08 sgerke-1L

Is it easy to see that NullValue is never passed?

sgerke-1L avatar Aug 26 '22 09:08 sgerke-1L

Not sure what you mean with easy: if you look into the GraphQL Java source code you can find it.

It should be clearly documented I would say.

andimarek avatar Aug 26 '22 21:08 andimarek

On the follow up discussion - the NullValue case in ObjectScalar's parseLiteral implementation has been removed, as a NullValue literal input will coerced as null & returned before the scalar's parseLiteral method is called. Added more documentation to make this clearer inside Coercing.

dondonz avatar Aug 30 '22 23:08 dondonz

Thanks, this solves our problem: The inconsistency now resolved.

sgerke-1L avatar Sep 07 '22 20:09 sgerke-1L