未验证 提交 33d25a76 编写于 作者: B Bogdan Kobylynskyi 提交者: GitHub

Fix a case when List NonNullType has a default value #177 (#179)

上级 0159315b
......@@ -31,7 +31,7 @@ public class DefaultValueMapper {
return mapObject((ObjectValue) defaultValue);
}
if (defaultValue instanceof ArrayValue) {
return mapArray(mappingContext, (ArrayValue) defaultValue, graphQLType);
return mapArray(mappingContext, graphQLType, (ArrayValue) defaultValue);
}
// no default value, or not a known type
return null;
......@@ -74,17 +74,20 @@ public class DefaultValueMapper {
return null;
}
private static String mapArray(MappingContext mappingContext, ArrayValue defaultValue, Type<?> graphQLType) {
if (!(graphQLType instanceof ListType)) {
throw new IllegalArgumentException("Unexpected array default value for non-list type");
private static String mapArray(MappingContext mappingContext, Type<?> graphQLType, ArrayValue defaultValue) {
if (graphQLType instanceof NonNullType) {
return mapArray(mappingContext, ((NonNullType) graphQLType).getType(), defaultValue);
}
List<Value> values = defaultValue.getValues();
if (values.isEmpty()) {
return "java.util.Collections.emptyList()";
if (graphQLType instanceof ListType) {
List<Value> values = defaultValue.getValues();
if (values.isEmpty()) {
return "java.util.Collections.emptyList()";
}
Type<?> elementType = ((ListType) graphQLType).getType();
return values.stream()
.map(v -> map(mappingContext, v, elementType))
.collect(Collectors.joining(", ", "java.util.Arrays.asList(", ")"));
}
Type<?> elementType = ((ListType) graphQLType).getType();
return values.stream()
.map(v -> map(mappingContext, v, elementType))
.collect(Collectors.joining(", ", "java.util.Arrays.asList(", ")"));
throw new IllegalArgumentException("Unexpected array default value for non-list type");
}
}
......@@ -17,6 +17,7 @@ public class InputWithDefaults implements java.io.Serializable {
private SomeObject objectWithNonNullDefault;
private java.util.List<Integer> intList = java.util.Arrays.asList(1, 2, 3);
private java.util.List<Integer> intListEmptyDefault = java.util.Collections.emptyList();
@javax.validation.constraints.NotNull
private java.util.List<SomeObject> objectListEmptyDefault = java.util.Collections.emptyList();
public InputWithDefaults() {
......
......@@ -17,6 +17,7 @@ public class InputWithDefaultsTO implements java.io.Serializable {
private SomeObjectTO objectWithNonNullDefault;
private java.util.List<Integer> intList = java.util.Arrays.asList(1, 2, 3);
private java.util.List<Integer> intListEmptyDefault = java.util.Collections.emptyList();
@javax.validation.constraints.NotNull
private java.util.List<SomeObjectTO> objectListEmptyDefault = java.util.Collections.emptyList();
public InputWithDefaultsTO() {
......
......@@ -11,7 +11,7 @@ input InputWithDefaults {
objectWithNonNullDefault: SomeObject = { name: "Bob" }
intList: [Int] = [1, 2, 3]
intListEmptyDefault: [Int] = []
objectListEmptyDefault: [SomeObject] = []
objectListEmptyDefault: [SomeObject]! = []
}
input SomeObject {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册