Skip to content

Commit 6b41b5b

Browse files
Support custom type default values for enum (#1458)
1 parent 3ab9418 commit 6b41b5b

File tree

5 files changed

+324
-8
lines changed

5 files changed

+324
-8
lines changed

src/main/java/com/kobylynskyi/graphql/codegen/mapper/ValueMapper.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import java.util.Collections;
1919
import java.util.List;
20-
import java.util.Map;
2120
import java.util.stream.Collectors;
2221

2322
/**
@@ -123,16 +122,14 @@ public String map(MappingContext mappingContext, Value<?> value, Type<?> graphQL
123122

124123
private String mapEnum(MappingContext mappingContext, EnumValue value, Type<?> graphQLType) {
125124
if (graphQLType == null) {
126-
Map<String, String> customTypesMapping = mappingContext.getCustomTypesMapping();
127-
if (customTypesMapping.containsKey(value.getName())) {
128-
return customTypesMapping.get(value.getName());
129-
}
130-
131-
return value.getName();
125+
String typeName = value.getName();
126+
return mappingContext.getCustomTypesMapping().getOrDefault(typeName, typeName);
132127
}
133128
if (graphQLType instanceof TypeName) {
134129
String typeName = ((TypeName) graphQLType).getName();
135-
typeName = DataModelMapper.getModelClassNameWithPrefixAndSuffix(mappingContext, typeName);
130+
typeName = mappingContext.getCustomTypesMapping().getOrDefault(typeName,
131+
DataModelMapper.getModelClassNameWithPrefixAndSuffix(mappingContext, typeName)
132+
);
136133
return typeName + "." + dataModelMapper.capitalizeIfRestricted(mappingContext, value.getName());
137134
}
138135
if (graphQLType instanceof NonNullType) {

src/test/java/com/kobylynskyi/graphql/codegen/GraphQLCodegenCustomScalarMappingTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@
99

1010
import java.io.File;
1111
import java.io.IOException;
12+
import java.util.Arrays;
1213
import java.util.HashMap;
14+
import java.util.List;
1315
import java.util.Objects;
1416

1517
import static com.kobylynskyi.graphql.codegen.TestUtils.assertSameTrimmedContent;
1618
import static com.kobylynskyi.graphql.codegen.TestUtils.getFileByName;
19+
import static java.util.Arrays.asList;
1720
import static java.util.Collections.singletonList;
1821
import static java.util.Collections.singletonMap;
22+
import static java.util.stream.Collectors.toList;
23+
import static org.junit.jupiter.api.Assertions.assertEquals;
1924

2025
class GraphQLCodegenCustomScalarMappingTest {
2126

@@ -86,6 +91,28 @@ void generate_CustomTypeMapping_ForExtensionProperty() throws Exception {
8691
getFileByName(files, "ExternalResolver.java"));
8792
}
8893

94+
/**
95+
* See #1429
96+
*/
97+
@Test
98+
void generate_CustomTypeMapping_ForEnumWithDefaultValue() throws Exception {
99+
mappingConfig.setGenerateExtensionFieldsResolvers(true);
100+
mappingConfig.setCustomTypesMapping(singletonMap("MyEnum", "com.example.MyOtherEnum"));
101+
mappingConfig.setGenerateClient(false);
102+
103+
generate("src/test/resources/schemas/defaults.graphqls");
104+
105+
File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
106+
List<String> generatedFileNames = Arrays.stream(files).map(File::getName).sorted().collect(toList());
107+
assertEquals(asList("InputWithDefaults.java", "MyEnum.java", "SomeObject.java"), generatedFileNames);
108+
109+
for (File file : files) {
110+
assertSameTrimmedContent(
111+
new File(String.format("src/test/resources/expected-classes/default-custom-types/%s.txt",
112+
file.getName())), file);
113+
}
114+
}
115+
89116
private void generate(String path) throws IOException {
90117
new JavaGraphQLCodegen(singletonList(path),
91118
outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo(mappingConfig))
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
package com.kobylynskyi.graphql.test1;
2+
3+
4+
/**
5+
* This input has all possible types
6+
*/
7+
@javax.annotation.Generated(
8+
value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen",
9+
date = "2020-12-31T23:59:59-0500"
10+
)
11+
public class InputWithDefaults implements java.io.Serializable {
12+
13+
private static final long serialVersionUID = 1L;
14+
15+
private Double floatVal = 1.23;
16+
private Boolean booleanVal = false;
17+
private Integer intVal = 42;
18+
private String stringVal = "my-default";
19+
private com.example.MyOtherEnum enumVal = com.example.MyOtherEnum.ONE;
20+
@javax.validation.constraints.NotNull
21+
private com.example.MyOtherEnum nonNullEnumVal = com.example.MyOtherEnum.TWO;
22+
private SomeObject objectWithNullDefault = null;
23+
private SomeObject objectWithNonNullDefault;
24+
private java.util.List<Integer> intList = java.util.Arrays.asList(1, 2, 3);
25+
private java.util.List<Integer> intListEmptyDefault = java.util.Collections.emptyList();
26+
@javax.validation.constraints.NotNull
27+
private java.util.List<SomeObject> objectListEmptyDefault = java.util.Collections.emptyList();
28+
29+
public InputWithDefaults() {
30+
}
31+
32+
public InputWithDefaults(Double floatVal, Boolean booleanVal, Integer intVal, String stringVal, com.example.MyOtherEnum enumVal, com.example.MyOtherEnum nonNullEnumVal, SomeObject objectWithNullDefault, SomeObject objectWithNonNullDefault, java.util.List<Integer> intList, java.util.List<Integer> intListEmptyDefault, java.util.List<SomeObject> objectListEmptyDefault) {
33+
this.floatVal = floatVal;
34+
this.booleanVal = booleanVal;
35+
this.intVal = intVal;
36+
this.stringVal = stringVal;
37+
this.enumVal = enumVal;
38+
this.nonNullEnumVal = nonNullEnumVal;
39+
this.objectWithNullDefault = objectWithNullDefault;
40+
this.objectWithNonNullDefault = objectWithNonNullDefault;
41+
this.intList = intList;
42+
this.intListEmptyDefault = intListEmptyDefault;
43+
this.objectListEmptyDefault = objectListEmptyDefault;
44+
}
45+
46+
public Double getFloatVal() {
47+
return floatVal;
48+
}
49+
public void setFloatVal(Double floatVal) {
50+
this.floatVal = floatVal;
51+
}
52+
53+
public Boolean getBooleanVal() {
54+
return booleanVal;
55+
}
56+
public void setBooleanVal(Boolean booleanVal) {
57+
this.booleanVal = booleanVal;
58+
}
59+
60+
public Integer getIntVal() {
61+
return intVal;
62+
}
63+
public void setIntVal(Integer intVal) {
64+
this.intVal = intVal;
65+
}
66+
67+
public String getStringVal() {
68+
return stringVal;
69+
}
70+
public void setStringVal(String stringVal) {
71+
this.stringVal = stringVal;
72+
}
73+
74+
public com.example.MyOtherEnum getEnumVal() {
75+
return enumVal;
76+
}
77+
public void setEnumVal(com.example.MyOtherEnum enumVal) {
78+
this.enumVal = enumVal;
79+
}
80+
81+
public com.example.MyOtherEnum getNonNullEnumVal() {
82+
return nonNullEnumVal;
83+
}
84+
public void setNonNullEnumVal(com.example.MyOtherEnum nonNullEnumVal) {
85+
this.nonNullEnumVal = nonNullEnumVal;
86+
}
87+
88+
public SomeObject getObjectWithNullDefault() {
89+
return objectWithNullDefault;
90+
}
91+
public void setObjectWithNullDefault(SomeObject objectWithNullDefault) {
92+
this.objectWithNullDefault = objectWithNullDefault;
93+
}
94+
95+
public SomeObject getObjectWithNonNullDefault() {
96+
return objectWithNonNullDefault;
97+
}
98+
public void setObjectWithNonNullDefault(SomeObject objectWithNonNullDefault) {
99+
this.objectWithNonNullDefault = objectWithNonNullDefault;
100+
}
101+
102+
public java.util.List<Integer> getIntList() {
103+
return intList;
104+
}
105+
public void setIntList(java.util.List<Integer> intList) {
106+
this.intList = intList;
107+
}
108+
109+
public java.util.List<Integer> getIntListEmptyDefault() {
110+
return intListEmptyDefault;
111+
}
112+
public void setIntListEmptyDefault(java.util.List<Integer> intListEmptyDefault) {
113+
this.intListEmptyDefault = intListEmptyDefault;
114+
}
115+
116+
public java.util.List<SomeObject> getObjectListEmptyDefault() {
117+
return objectListEmptyDefault;
118+
}
119+
public void setObjectListEmptyDefault(java.util.List<SomeObject> objectListEmptyDefault) {
120+
this.objectListEmptyDefault = objectListEmptyDefault;
121+
}
122+
123+
124+
125+
public static InputWithDefaults.Builder builder() {
126+
return new InputWithDefaults.Builder();
127+
}
128+
129+
@javax.annotation.Generated(
130+
value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen",
131+
date = "2020-12-31T23:59:59-0500"
132+
)
133+
public static class Builder {
134+
135+
private Double floatVal = 1.23;
136+
private Boolean booleanVal = false;
137+
private Integer intVal = 42;
138+
private String stringVal = "my-default";
139+
private com.example.MyOtherEnum enumVal = com.example.MyOtherEnum.ONE;
140+
private com.example.MyOtherEnum nonNullEnumVal = com.example.MyOtherEnum.TWO;
141+
private SomeObject objectWithNullDefault = null;
142+
private SomeObject objectWithNonNullDefault;
143+
private java.util.List<Integer> intList = java.util.Arrays.asList(1, 2, 3);
144+
private java.util.List<Integer> intListEmptyDefault = java.util.Collections.emptyList();
145+
private java.util.List<SomeObject> objectListEmptyDefault = java.util.Collections.emptyList();
146+
147+
public Builder() {
148+
}
149+
150+
public Builder setFloatVal(Double floatVal) {
151+
this.floatVal = floatVal;
152+
return this;
153+
}
154+
155+
public Builder setBooleanVal(Boolean booleanVal) {
156+
this.booleanVal = booleanVal;
157+
return this;
158+
}
159+
160+
public Builder setIntVal(Integer intVal) {
161+
this.intVal = intVal;
162+
return this;
163+
}
164+
165+
public Builder setStringVal(String stringVal) {
166+
this.stringVal = stringVal;
167+
return this;
168+
}
169+
170+
public Builder setEnumVal(com.example.MyOtherEnum enumVal) {
171+
this.enumVal = enumVal;
172+
return this;
173+
}
174+
175+
public Builder setNonNullEnumVal(com.example.MyOtherEnum nonNullEnumVal) {
176+
this.nonNullEnumVal = nonNullEnumVal;
177+
return this;
178+
}
179+
180+
public Builder setObjectWithNullDefault(SomeObject objectWithNullDefault) {
181+
this.objectWithNullDefault = objectWithNullDefault;
182+
return this;
183+
}
184+
185+
public Builder setObjectWithNonNullDefault(SomeObject objectWithNonNullDefault) {
186+
this.objectWithNonNullDefault = objectWithNonNullDefault;
187+
return this;
188+
}
189+
190+
public Builder setIntList(java.util.List<Integer> intList) {
191+
this.intList = intList;
192+
return this;
193+
}
194+
195+
public Builder setIntListEmptyDefault(java.util.List<Integer> intListEmptyDefault) {
196+
this.intListEmptyDefault = intListEmptyDefault;
197+
return this;
198+
}
199+
200+
public Builder setObjectListEmptyDefault(java.util.List<SomeObject> objectListEmptyDefault) {
201+
this.objectListEmptyDefault = objectListEmptyDefault;
202+
return this;
203+
}
204+
205+
206+
public InputWithDefaults build() {
207+
return new InputWithDefaults(floatVal, booleanVal, intVal, stringVal, enumVal, nonNullEnumVal, objectWithNullDefault, objectWithNonNullDefault, intList, intListEmptyDefault, objectListEmptyDefault);
208+
}
209+
210+
}
211+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.kobylynskyi.graphql.test1;
2+
3+
@javax.annotation.Generated(
4+
value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen",
5+
date = "2020-12-31T23:59:59-0500"
6+
)
7+
public enum MyEnum {
8+
9+
ONE("ONE"),
10+
TWO("TWO"),
11+
THREE("THREE");
12+
13+
private final String graphqlName;
14+
15+
private MyEnum(String graphqlName) {
16+
this.graphqlName = graphqlName;
17+
}
18+
19+
@Override
20+
public String toString() {
21+
return this.graphqlName;
22+
}
23+
24+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.kobylynskyi.graphql.test1;
2+
3+
4+
@javax.annotation.Generated(
5+
value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen",
6+
date = "2020-12-31T23:59:59-0500"
7+
)
8+
public class SomeObject implements java.io.Serializable {
9+
10+
private static final long serialVersionUID = 1L;
11+
12+
@javax.validation.constraints.NotNull
13+
private String name;
14+
15+
public SomeObject() {
16+
}
17+
18+
public SomeObject(String name) {
19+
this.name = name;
20+
}
21+
22+
public String getName() {
23+
return name;
24+
}
25+
public void setName(String name) {
26+
this.name = name;
27+
}
28+
29+
30+
31+
public static SomeObject.Builder builder() {
32+
return new SomeObject.Builder();
33+
}
34+
35+
@javax.annotation.Generated(
36+
value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen",
37+
date = "2020-12-31T23:59:59-0500"
38+
)
39+
public static class Builder {
40+
41+
private String name;
42+
43+
public Builder() {
44+
}
45+
46+
public Builder setName(String name) {
47+
this.name = name;
48+
return this;
49+
}
50+
51+
52+
public SomeObject build() {
53+
return new SomeObject(name);
54+
}
55+
56+
}
57+
}

0 commit comments

Comments
 (0)