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
118 stars 47 forks source link

Problem with primitive types #179

Closed gjvoosten closed 1 year ago

gjvoosten commented 1 year ago

Environment:

If I add a configuration like this:

generateClientCodeConf {
  // …
  customScalars = [
    [
      graphQLTypeName: "Base64String",
      javaType: "byte[]",
      graphQLScalarTypeStaticField: "com.graphql_java_generator.customscalars.GraphQLScalarTypeBase64String.GraphQLBase64String"
    ]
  ]
}

I get an error when generating the client code, that boils down to:

Caused by: java.lang.StringIndexOutOfBoundsException: begin 0, end -1, length 6
    at com.graphql_java_generator.util.GraphqlUtils.addImport(GraphqlUtils.java:803)
    at com.graphql_java_generator.plugin.language.impl.AbstractType.addImport(AbstractType.java:156)
    at com.graphql_java_generator.plugin.generate_code.GenerateCodeDocumentParser.addFieldAnnotationForClientMode(GenerateCodeDocumentParser.java:473)
    at com.graphql_java_generator.plugin.generate_code.GenerateCodeDocumentParser.lambda$addAnnotations$2(GenerateCodeDocumentParser.java:312)

A test such as this (on current master):

diff --git a/graphql-java-client-runtime/src/test/java/com/graphql_java_generator/util/GraphqlUtilsTest.java b/graphql-java-client-runtime/src/test/java/com/graphql_java_generator/util/GraphqlUtilsTest.java
index 3933e7f58..d1c29401d 100644
--- a/graphql-java-client-runtime/src/test/java/com/graphql_java_generator/util/GraphqlUtilsTest.java
+++ b/graphql-java-client-runtime/src/test/java/com/graphql_java_generator/util/GraphqlUtilsTest.java
@@ -160,6 +160,10 @@ class GraphqlUtilsTest {
                graphqlUtils.addImport(imports, getClass().getPackage().getName(), getClass().getName());
                assertEquals(0, imports.size(), "Same package: import not added");

+               // primitive type
+               graphqlUtils.addImport(imports, getClass().getPackage().getName(), "byte[]");
+               assertEquals(0, imports.size(), "primitive type: import not added");
+
                // java.lang
                graphqlUtils.addImport(imports, getClass().getPackage().getName(), java.lang.String.class.getName());
                assertEquals(0, imports.size(), "java.lang: import not added");

reproduces the error.

I can fix it with:

diff --git a/graphql-java-common-runtime/src/main/java/com/graphql_java_generator/util/GraphqlUtils.java b/graphql-java-common-runtime/src/main/java/com/graphql_java_generator/util/GraphqlUtils.java
index 17896fc7d..1fef3d9f0 100644
--- a/graphql-java-common-runtime/src/main/java/com/graphql_java_generator/util/GraphqlUtils.java
+++ b/graphql-java-common-runtime/src/main/java/com/graphql_java_generator/util/GraphqlUtils.java
@@ -800,12 +800,12 @@ public class GraphqlUtils {
                String fullClassname = classname.replace('$', '.');

                int lastDotPos = fullClassname.lastIndexOf('.');
-               String packageName = fullClassname.substring(0, lastDotPos);
+               String packageName = lastDotPos < 0 ? "" : fullClassname.substring(0, lastDotPos);
                String simpleClassName = fullClassname.substring(lastDotPos + 1);

-               // No import for java.lang
+               // No import for primitive types and java.lang
                // And no import if the class is in the same package.
-               if (!packageName.equals("java.lang") && !targetPackageName.equals(packageName)) {
+               if (!packageName.isEmpty() && !packageName.equals("java.lang") && !targetPackageName.equals(packageName)) {
                        imports.add(packageName + "." + simpleClassName);
                }
        }
etienne-sf commented 1 year ago

Thank you for the precise report. There is still something I don't understand here: the gradle conf you've pasted here above is the same that is used (and works) in the 1.18.10 version of the plugin.

I know I must do additional test with Java 17. But I don't understand why Java 17 would change the behavior of the part of the code you changed...

Etienne

gjvoosten commented 1 year ago

@etienne-sf For me, the issue appears to be fixed in release 1.18.11. :fireworks:

etienne-sf commented 1 year ago

:)