Skip to content

Commit

Permalink
Fix a case when List NonNullType has a default value #177 (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
kobylynskyi authored Jun 18, 2020
1 parent 0159315 commit 33d25a7
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static String map(MappingContext mappingContext, Value<?> defaultValue, T
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;
Expand Down Expand Up @@ -74,17 +74,20 @@ private static String mapObject(ObjectValue defaultValue) {
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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/schemas/defaults.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ input InputWithDefaults {
objectWithNonNullDefault: SomeObject = { name: "Bob" }
intList: [Int] = [1, 2, 3]
intListEmptyDefault: [Int] = []
objectListEmptyDefault: [SomeObject] = []
objectListEmptyDefault: [SomeObject]! = []
}

input SomeObject {
Expand Down

0 comments on commit 33d25a7

Please sign in to comment.