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

Modifying the getGetter method to accept reserved keywords #177

Closed kewinremy closed 1 year ago

kewinremy commented 1 year ago

At runtime, the getGetter method does not support reserved keywords like default, etc. Currently it sends the "NoSuchMethodException" at runtime when properly fields are about to be serialized.

For complete information, please have a look at my comment on a previous PR: https://github.com/graphql-java-generator/graphql-maven-plugin-project/issues/166

Version affected: 1.18.9

pom.xml

        <dependency>
            <groupId>com.graphql-java-generator</groupId>
            <artifactId>graphql-maven-plugin</artifactId>
            <version>1.18.9</version>
        </dependency>
etienne-sf commented 1 year ago

Thanks for this PR.

You're right, there is an issue here : I created IT test to check that, to check that your PR actually solve the issue I've identifier from your PR. I'll probably update the code, then, as the field generated by the plugin contain a @GraphQLScalar or @GraphQLNonScalar annotation, that allows to get the real name for the GraphQL field, as defined in the GraphQL schema.

Etienne

etienne-sf commented 1 year ago

BTW, I confirm we're talking about the same issue: once I checked out your PR, my IT test stops complaining about the lack of the getter method.

etienne-sf commented 1 year ago

Hello Remy,

I have a question on the change you did:

            // For the boolean fields, the getter may be named isProperty. Let's try that:
            if (field.getType().equals(boolean.class) || field.getType().equals(Boolean.class) && field.getName().startsWith("is")) {
                getterMethodName = "is" + getPascalCase(field.getName());
                method = clazz.getMethod(getterMethodName);
            }
            // For the fields conflicting with reserved keywords and generated with an underscore (_), the getter may be named _Property. Let's try that:
            else if (field.getName().startsWith("_")) {
                String sanitizedField = field.getName().substring(1);
                getterMethodName = "get" + getPascalCase(sanitizedField);
                method = clazz.getMethod(getterMethodName);
            } else {
                throw e;
            }

In the first if, there is this command: field.getName().startsWith("is")

I don't understand the reason which why you check if the field starts by is.

Can you precise that?

Etienne