Skip to content

Commit 33d25a7

Browse files
authored
Fix a case when List NonNullType has a default value #177 (#179)
1 parent 0159315 commit 33d25a7

File tree

4 files changed

+17
-12
lines changed

4 files changed

+17
-12
lines changed

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static String map(MappingContext mappingContext, Value<?> defaultValue, T
3131
return mapObject((ObjectValue) defaultValue);
3232
}
3333
if (defaultValue instanceof ArrayValue) {
34-
return mapArray(mappingContext, (ArrayValue) defaultValue, graphQLType);
34+
return mapArray(mappingContext, graphQLType, (ArrayValue) defaultValue);
3535
}
3636
// no default value, or not a known type
3737
return null;
@@ -74,17 +74,20 @@ private static String mapObject(ObjectValue defaultValue) {
7474
return null;
7575
}
7676

77-
private static String mapArray(MappingContext mappingContext, ArrayValue defaultValue, Type<?> graphQLType) {
78-
if (!(graphQLType instanceof ListType)) {
79-
throw new IllegalArgumentException("Unexpected array default value for non-list type");
77+
private static String mapArray(MappingContext mappingContext, Type<?> graphQLType, ArrayValue defaultValue) {
78+
if (graphQLType instanceof NonNullType) {
79+
return mapArray(mappingContext, ((NonNullType) graphQLType).getType(), defaultValue);
8080
}
81-
List<Value> values = defaultValue.getValues();
82-
if (values.isEmpty()) {
83-
return "java.util.Collections.emptyList()";
81+
if (graphQLType instanceof ListType) {
82+
List<Value> values = defaultValue.getValues();
83+
if (values.isEmpty()) {
84+
return "java.util.Collections.emptyList()";
85+
}
86+
Type<?> elementType = ((ListType) graphQLType).getType();
87+
return values.stream()
88+
.map(v -> map(mappingContext, v, elementType))
89+
.collect(Collectors.joining(", ", "java.util.Arrays.asList(", ")"));
8490
}
85-
Type<?> elementType = ((ListType) graphQLType).getType();
86-
return values.stream()
87-
.map(v -> map(mappingContext, v, elementType))
88-
.collect(Collectors.joining(", ", "java.util.Arrays.asList(", ")"));
91+
throw new IllegalArgumentException("Unexpected array default value for non-list type");
8992
}
9093
}

src/test/resources/expected-classes/defaults/InputWithDefaults.java.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class InputWithDefaults implements java.io.Serializable {
1717
private SomeObject objectWithNonNullDefault;
1818
private java.util.List<Integer> intList = java.util.Arrays.asList(1, 2, 3);
1919
private java.util.List<Integer> intListEmptyDefault = java.util.Collections.emptyList();
20+
@javax.validation.constraints.NotNull
2021
private java.util.List<SomeObject> objectListEmptyDefault = java.util.Collections.emptyList();
2122

2223
public InputWithDefaults() {

src/test/resources/expected-classes/defaults/InputWithDefaultsTO.java.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class InputWithDefaultsTO implements java.io.Serializable {
1717
private SomeObjectTO objectWithNonNullDefault;
1818
private java.util.List<Integer> intList = java.util.Arrays.asList(1, 2, 3);
1919
private java.util.List<Integer> intListEmptyDefault = java.util.Collections.emptyList();
20+
@javax.validation.constraints.NotNull
2021
private java.util.List<SomeObjectTO> objectListEmptyDefault = java.util.Collections.emptyList();
2122

2223
public InputWithDefaultsTO() {

src/test/resources/schemas/defaults.graphqls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ input InputWithDefaults {
1111
objectWithNonNullDefault: SomeObject = { name: "Bob" }
1212
intList: [Int] = [1, 2, 3]
1313
intListEmptyDefault: [Int] = []
14-
objectListEmptyDefault: [SomeObject] = []
14+
objectListEmptyDefault: [SomeObject]! = []
1515
}
1616

1717
input SomeObject {

0 commit comments

Comments
 (0)