graphql-java-generator / graphql-maven-plugin-project

graphql-maven-plugin is a Maven Plugin for GraphQL, based on graphql-java. It accelerates the development for both the client and the server, by generating the Java code. It allows a quicker development when in contract-first approach, by avoiding to code the boilerplate code.
https://graphql-maven-plugin-project.graphql-java-generator.com
MIT License
115 stars 47 forks source link

Abuse the JSON scalar #207

Closed razilevin closed 6 months ago

razilevin commented 6 months ago

In regards to

https://github.com/graphql-java-generator/graphql-maven-plugin-project/issues/93

seems like many projects abuse the JSON scalar so that their services can return a graph of any shape. Calling GQL services applying the same approach I get an exception thrown in class AbstractCustomJacksonDeserializer.

switch (p.currentToken()) {
            case VALUE_FALSE:
            case VALUE_TRUE:
                value = new BooleanValue(p.getBooleanValue());
                break;
            case VALUE_NUMBER_FLOAT:
                value = new FloatValue(p.getDecimalValue());
                break;
            case VALUE_NUMBER_INT:
                value = new IntValue(p.getBigIntegerValue());
                break;
            case VALUE_STRING:
                value = new StringValue(p.getText());
                break;
            case VALUE_NULL:
                value = null;
                break;
            default:
                throw new JsonParseException(p, "Non managed JSON token: " + p.currentToken()); //$NON-NLS-1$
            }

the token in question is '{'. I am having a hard time trying to work around this issue. Say I wanted it to work I would apply the following to your code base

  1. Extend AbstractCustomJacksonDeserializer
  2. Override your deserialize function
  3. Generate a new template based on your client_jackson_deserializers.vm.java and replace your base class with mine
  4. I get stuck in the implementation of the deserialize function.

I don't mind using a JsonNode as the return type. Any suggestions?

etienne-sf commented 6 months ago

Hello, it's the same issue as #205.

I'm working on it. But the issue is not in the plugin's code. It's in the graphql-java core tool.

I'll create a demo project, before filing an issue to the graphql-java team.

Etienne

razilevin commented 6 months ago

Given this hack on the generated CustomJacksonDeserializers for just that mapping of JSON scalar using the following implementation everything works as expected.

@Override public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
            return p.readValueAsTree();
}

Can you explain to me why you think it is not in your implementation of the AbstractCustomJacksonDeserializer deserialize function?

etienne-sf commented 6 months ago

Hello,

Thank you for your comment. I came to the same conclusion (that the issue is in the plugin).

This custom serialization / deserialization has become too complex. But I need to keep it for backward compatibility. The main issue is to make it work for all possible cases.

Étienne

etienne-sf commented 6 months ago

Hello,

The 2.4 version has been released. It handles the JSON and Object custom scalars.

Etienne