diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/mapper/ValueMapper.java b/src/main/java/com/kobylynskyi/graphql/codegen/mapper/ValueMapper.java index 9f6fad441023533ea5cd4a1e039a76385e41a121..98702f422111b543a02af792dd861d834a2609cb 100644 --- a/src/main/java/com/kobylynskyi/graphql/codegen/mapper/ValueMapper.java +++ b/src/main/java/com/kobylynskyi/graphql/codegen/mapper/ValueMapper.java @@ -39,7 +39,7 @@ public class ValueMapper { return ValueFormatter.format(mapBoolean((BooleanValue) value), formatter); } if (value instanceof IntValue) { - return ValueFormatter.format(mapInt((IntValue) value), formatter); + return ValueFormatter.format(mapInt(mappingContext, (IntValue) value, graphQLType), formatter); } if (value instanceof FloatValue) { return ValueFormatter.format(mapFloat((FloatValue) value), formatter); @@ -65,7 +65,21 @@ public class ValueMapper { return value.isValue() ? "true" : "false"; } - private static String mapInt(IntValue value) { + private static String mapInt(MappingContext mappingContext, IntValue value, Type graphQLType) { + //default java basic type is `int`. so, default value like 123 that must wrap or append suffix `L` when it be defined as `int` in graphql schema. + //`int` cannot assign to `Long`, also `double` cannot assign to `Float`, but graphql Float default mapping is Double in java, so, not modify `mapFloat`. + if (graphQLType instanceof TypeName) { + String customType = mappingContext.getCustomTypesMapping().get("Long"); + String typeName = ((TypeName) graphQLType).getName(); + if ("Long".equals(typeName) && ("java.lang.Long".equals(customType) || "Long".equals(customType))) { + return String.valueOf(value.getValue()).concat("L"); + } + } + + if (graphQLType instanceof NonNullType) { + // unwrapping NonNullType + return mapInt(mappingContext, value, ((NonNullType) graphQLType).getType()); + } return String.valueOf(value.getValue()); } diff --git a/src/test/java/com/kobylynskyi/graphql/codegen/GraphQLCodegenDefaultsTest.java b/src/test/java/com/kobylynskyi/graphql/codegen/GraphQLCodegenDefaultsTest.java index 9147d24e56364a868211f39387358b856f05a331..1aa015179caee92d85ed5be3d6d7070a3822a0e5 100644 --- a/src/test/java/com/kobylynskyi/graphql/codegen/GraphQLCodegenDefaultsTest.java +++ b/src/test/java/com/kobylynskyi/graphql/codegen/GraphQLCodegenDefaultsTest.java @@ -8,11 +8,13 @@ import org.junit.jupiter.api.Test; import java.io.File; import java.util.Arrays; +import java.util.HashMap; import java.util.List; import java.util.Objects; import static com.kobylynskyi.graphql.codegen.TestUtils.assertSameTrimmedContent; import static java.util.Collections.singletonList; +import static java.util.Collections.singletonMap; import static java.util.stream.Collectors.toList; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -66,4 +68,21 @@ class GraphQLCodegenDefaultsTest { file); } } + + @Test + void generate_CheckFiles_OnLongDefault() throws Exception { + mappingConfig.setCustomTypesMapping(new HashMap<>(singletonMap("Long", "java.lang.Long"))); + mappingConfig.setModelNameSuffix("DTO"); + new GraphQLCodegen(singletonList("src/test/resources/schemas/defaults-with-Long.graphqls"), + outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo()).generate(); + File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles()); + List generatedFileNames = Arrays.stream(files).map(File::getName).sorted().collect(toList()); + assertEquals(Arrays.asList("InputWithDefaultsDTO.java", "MyEnumDTO.java", "SomeObjectDTO.java"), generatedFileNames); + + for (File file : files) { + assertSameTrimmedContent(new File(String.format("src/test/resources/expected-classes/defaults/%s.txt", + file.getName())), + file); + } + } } diff --git a/src/test/resources/expected-classes/defaults/InputWithDefaultsDTO.java.txt b/src/test/resources/expected-classes/defaults/InputWithDefaultsDTO.java.txt new file mode 100644 index 0000000000000000000000000000000000000000..8d92b9718021b68847ceb74088d8852351754b2f --- /dev/null +++ b/src/test/resources/expected-classes/defaults/InputWithDefaultsDTO.java.txt @@ -0,0 +1,220 @@ +package com.kobylynskyi.graphql.testdefaults; + + +/** + * This input has all possible types + */ +@javax.annotation.Generated( + value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen", + date = "2020-12-31T23:59:59-0500" +) +public class InputWithDefaultsDTO implements java.io.Serializable { + + private Double floatVal = 1.23; + private Boolean booleanVal = false; + private Integer intVal = 42; + private String stringVal = "my-default"; + private MyEnumDTO enumVal = MyEnumDTO.ONE; + @javax.validation.constraints.NotNull + private MyEnumDTO nonNullEnumVal = MyEnumDTO.TWO; + private SomeObjectDTO objectWithNullDefault = null; + private SomeObjectDTO objectWithNonNullDefault; + private java.util.List intList = java.util.Arrays.asList(1, 2, 3); + private java.util.List intListEmptyDefault = java.util.Collections.emptyList(); + @javax.validation.constraints.NotNull + private java.util.List objectListEmptyDefault = java.util.Collections.emptyList(); + private java.lang.Long longValue = 123456L; + + public InputWithDefaultsDTO() { + } + + public InputWithDefaultsDTO(Double floatVal, Boolean booleanVal, Integer intVal, String stringVal, MyEnumDTO enumVal, MyEnumDTO nonNullEnumVal, SomeObjectDTO objectWithNullDefault, SomeObjectDTO objectWithNonNullDefault, java.util.List intList, java.util.List intListEmptyDefault, java.util.List objectListEmptyDefault, java.lang.Long longValue) { + this.floatVal = floatVal; + this.booleanVal = booleanVal; + this.intVal = intVal; + this.stringVal = stringVal; + this.enumVal = enumVal; + this.nonNullEnumVal = nonNullEnumVal; + this.objectWithNullDefault = objectWithNullDefault; + this.objectWithNonNullDefault = objectWithNonNullDefault; + this.intList = intList; + this.intListEmptyDefault = intListEmptyDefault; + this.objectListEmptyDefault = objectListEmptyDefault; + this.longValue = longValue; + } + + public Double getFloatVal() { + return floatVal; + } + public void setFloatVal(Double floatVal) { + this.floatVal = floatVal; + } + + public Boolean getBooleanVal() { + return booleanVal; + } + public void setBooleanVal(Boolean booleanVal) { + this.booleanVal = booleanVal; + } + + public Integer getIntVal() { + return intVal; + } + public void setIntVal(Integer intVal) { + this.intVal = intVal; + } + + public String getStringVal() { + return stringVal; + } + public void setStringVal(String stringVal) { + this.stringVal = stringVal; + } + + public MyEnumDTO getEnumVal() { + return enumVal; + } + public void setEnumVal(MyEnumDTO enumVal) { + this.enumVal = enumVal; + } + + public MyEnumDTO getNonNullEnumVal() { + return nonNullEnumVal; + } + public void setNonNullEnumVal(MyEnumDTO nonNullEnumVal) { + this.nonNullEnumVal = nonNullEnumVal; + } + + public SomeObjectDTO getObjectWithNullDefault() { + return objectWithNullDefault; + } + public void setObjectWithNullDefault(SomeObjectDTO objectWithNullDefault) { + this.objectWithNullDefault = objectWithNullDefault; + } + + public SomeObjectDTO getObjectWithNonNullDefault() { + return objectWithNonNullDefault; + } + public void setObjectWithNonNullDefault(SomeObjectDTO objectWithNonNullDefault) { + this.objectWithNonNullDefault = objectWithNonNullDefault; + } + + public java.util.List getIntList() { + return intList; + } + public void setIntList(java.util.List intList) { + this.intList = intList; + } + + public java.util.List getIntListEmptyDefault() { + return intListEmptyDefault; + } + public void setIntListEmptyDefault(java.util.List intListEmptyDefault) { + this.intListEmptyDefault = intListEmptyDefault; + } + + public java.util.List getObjectListEmptyDefault() { + return objectListEmptyDefault; + } + public void setObjectListEmptyDefault(java.util.List objectListEmptyDefault) { + this.objectListEmptyDefault = objectListEmptyDefault; + } + + public java.lang.Long getLongValue() { + return longValue; + } + public void setLongValue(java.lang.Long longValue) { + this.longValue = longValue; + } + + + + public static InputWithDefaultsDTO.Builder builder() { + return new InputWithDefaultsDTO.Builder(); + } + + public static class Builder { + + private Double floatVal = 1.23; + private Boolean booleanVal = false; + private Integer intVal = 42; + private String stringVal = "my-default"; + private MyEnumDTO enumVal = MyEnumDTO.ONE; + private MyEnumDTO nonNullEnumVal = MyEnumDTO.TWO; + private SomeObjectDTO objectWithNullDefault = null; + private SomeObjectDTO objectWithNonNullDefault; + private java.util.List intList = java.util.Arrays.asList(1, 2, 3); + private java.util.List intListEmptyDefault = java.util.Collections.emptyList(); + private java.util.List objectListEmptyDefault = java.util.Collections.emptyList(); + private java.lang.Long longValue = 123456L; + + public Builder() { + } + + public Builder setFloatVal(Double floatVal) { + this.floatVal = floatVal; + return this; + } + + public Builder setBooleanVal(Boolean booleanVal) { + this.booleanVal = booleanVal; + return this; + } + + public Builder setIntVal(Integer intVal) { + this.intVal = intVal; + return this; + } + + public Builder setStringVal(String stringVal) { + this.stringVal = stringVal; + return this; + } + + public Builder setEnumVal(MyEnumDTO enumVal) { + this.enumVal = enumVal; + return this; + } + + public Builder setNonNullEnumVal(MyEnumDTO nonNullEnumVal) { + this.nonNullEnumVal = nonNullEnumVal; + return this; + } + + public Builder setObjectWithNullDefault(SomeObjectDTO objectWithNullDefault) { + this.objectWithNullDefault = objectWithNullDefault; + return this; + } + + public Builder setObjectWithNonNullDefault(SomeObjectDTO objectWithNonNullDefault) { + this.objectWithNonNullDefault = objectWithNonNullDefault; + return this; + } + + public Builder setIntList(java.util.List intList) { + this.intList = intList; + return this; + } + + public Builder setIntListEmptyDefault(java.util.List intListEmptyDefault) { + this.intListEmptyDefault = intListEmptyDefault; + return this; + } + + public Builder setObjectListEmptyDefault(java.util.List objectListEmptyDefault) { + this.objectListEmptyDefault = objectListEmptyDefault; + return this; + } + + public Builder setLongValue(java.lang.Long longValue) { + this.longValue = longValue; + return this; + } + + + public InputWithDefaultsDTO build() { + return new InputWithDefaultsDTO(floatVal, booleanVal, intVal, stringVal, enumVal, nonNullEnumVal, objectWithNullDefault, objectWithNonNullDefault, intList, intListEmptyDefault, objectListEmptyDefault, longValue); + } + + } +} diff --git a/src/test/resources/expected-classes/defaults/MyEnumDTO.java.txt b/src/test/resources/expected-classes/defaults/MyEnumDTO.java.txt new file mode 100644 index 0000000000000000000000000000000000000000..52a4f5b10991d6681238761322885d7b0846b4ba --- /dev/null +++ b/src/test/resources/expected-classes/defaults/MyEnumDTO.java.txt @@ -0,0 +1,24 @@ +package com.kobylynskyi.graphql.testdefaults; + +@javax.annotation.Generated( + value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen", + date = "2020-12-31T23:59:59-0500" +) +public enum MyEnumDTO { + + ONE("ONE"), + TWO("TWO"), + THREE("THREE"); + + private final String graphqlName; + + private MyEnumDTO(String graphqlName) { + this.graphqlName = graphqlName; + } + + @Override + public String toString() { + return this.graphqlName; + } + +} \ No newline at end of file diff --git a/src/test/resources/expected-classes/defaults/SomeObjectDTO.java.txt b/src/test/resources/expected-classes/defaults/SomeObjectDTO.java.txt new file mode 100644 index 0000000000000000000000000000000000000000..6acb509f6be82d75c9cefb10e56857eb24d2ede3 --- /dev/null +++ b/src/test/resources/expected-classes/defaults/SomeObjectDTO.java.txt @@ -0,0 +1,51 @@ +package com.kobylynskyi.graphql.testdefaults; + + +@javax.annotation.Generated( + value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen", + date = "2020-12-31T23:59:59-0500" +) +public class SomeObjectDTO implements java.io.Serializable { + + @javax.validation.constraints.NotNull + private String name; + + public SomeObjectDTO() { + } + + public SomeObjectDTO(String name) { + this.name = name; + } + + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + + + + public static SomeObjectDTO.Builder builder() { + return new SomeObjectDTO.Builder(); + } + + public static class Builder { + + private String name; + + public Builder() { + } + + public Builder setName(String name) { + this.name = name; + return this; + } + + + public SomeObjectDTO build() { + return new SomeObjectDTO(name); + } + + } +} diff --git a/src/test/resources/schemas/defaults-with-Long.graphqls b/src/test/resources/schemas/defaults-with-Long.graphqls new file mode 100644 index 0000000000000000000000000000000000000000..806a408f7a06abb39d2b8196023131c23e897dca --- /dev/null +++ b/src/test/resources/schemas/defaults-with-Long.graphqls @@ -0,0 +1,27 @@ + +# This input has all possible types +input InputWithDefaults { + floatVal: Float = 1.23 + booleanVal: Boolean = false + intVal: Int = 42 + stringVal: String = "my-default" + enumVal: MyEnum = ONE + nonNullEnumVal: MyEnum! = TWO + objectWithNullDefault: SomeObject = null + objectWithNonNullDefault: SomeObject = { name: "Bob" } + intList: [Int] = [1, 2, 3] + intListEmptyDefault: [Int] = [] + objectListEmptyDefault: [SomeObject]! = [] + longValue: Long = 123456 +} + +input SomeObject { + name: String! +} + +enum MyEnum { + ONE, TWO, THREE +} + + +scalar Long \ No newline at end of file diff --git a/src/test/resources/schemas/defaults.graphqls b/src/test/resources/schemas/defaults.graphqls index fb5391d4efb5c0179d54327c0b15e2cbdf23b895..ef1bc5c0076678804cfa95c9fe28cbfe83fb566e 100644 --- a/src/test/resources/schemas/defaults.graphqls +++ b/src/test/resources/schemas/defaults.graphqls @@ -20,4 +20,4 @@ input SomeObject { enum MyEnum { ONE, TWO, THREE -} +} \ No newline at end of file