Skip to content

Commit 4858045

Browse files
committed
NPE fix for undefined value
1 parent d1b30e9 commit 4858045

File tree

4 files changed

+35
-33
lines changed

4 files changed

+35
-33
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ org.gradle.daemon=true
55
org.gradle.parallel=true
66
org.gradle.jvmargs=-Dfile.encoding=UTF-8
77

8-
version = 8.3-nosto
8+
version = 8.3-nosto1

src/main/java/graphql/annotations/annotationTypes/GraphQLUndefinable.java

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/main/java/graphql/annotations/dataFetchers/MethodDataFetcher.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.lang.reflect.Type;
2929
import java.util.ArrayList;
3030
import java.util.Arrays;
31+
import java.util.Collections;
3132
import java.util.List;
3233
import java.util.Map;
3334
import java.util.Optional;
@@ -145,14 +146,22 @@ private Object buildArg(Type p, GraphQLType graphQLType, Optional<Object> arg) {
145146
Constructor<?> constructor = getBuildArgConstructor(constructors);
146147
Parameter[] parameters = constructor.getParameters();
147148

148-
if (parameters.length == 1 && parameters[0].getType().isAssignableFrom(arg.get().getClass())) {
149-
return constructNewInstance(constructor, arg.get());
149+
if (parameters.length == 1 && parameters[0].getType().isAssignableFrom((Class<?>) p)) {
150+
if (parameters[0].getType().isAssignableFrom(Optional.class)) {
151+
return constructNewInstance(constructor, arg);
152+
} else {
153+
return constructNewInstance(constructor, arg.orElse(null));
154+
}
150155
} else {
151156
List<Object> objects = new ArrayList<>();
152-
Map map = (Map) arg.get();
157+
Map map = (Map) arg.orElseGet(Collections::emptyMap);
153158
for (Parameter parameter : parameters) {
154159
String name = toGraphqlName(parameter.getAnnotation(GraphQLName.class) != null ? parameter.getAnnotation(GraphQLName.class).value() : parameter.getName());
155-
objects.add(buildArg(parameter.getParameterizedType(), ((GraphQLInputObjectType) graphQLType).getField(name).getType(), map.containsKey(name) ? Optional.ofNullable(map.get(name)) : null));
160+
if (!map.containsKey(name)) {
161+
objects.add(null);
162+
} else {
163+
objects.add(buildArg(parameter.getParameterizedType(), ((GraphQLInputObjectType) graphQLType).getField(name).getType(), Optional.ofNullable(map.get(name))));
164+
}
156165
}
157166
return constructNewInstance(constructor, objects.toArray(new Object[objects.size()]));
158167
}
@@ -169,7 +178,7 @@ private Object buildArg(Type p, GraphQLType graphQLType, Optional<Object> arg) {
169178
Type subType = ((ParameterizedType) p).getActualTypeArguments()[0];
170179
GraphQLType wrappedType = ((GraphQLList) graphQLType).getWrappedType();
171180

172-
for (Object item : ((List) arg.get())) {
181+
for (Object item : ((List) arg.orElseGet(Collections::emptyList))) {
173182
list.add(buildArg(subType, wrappedType, Optional.ofNullable(item)));
174183
}
175184
return list;
@@ -182,7 +191,7 @@ private Object buildArg(Type p, GraphQLType graphQLType, Optional<Object> arg) {
182191
return Optional.ofNullable(buildArg(subType, new GraphQLUndefined(), arg));
183192
}
184193
} else {
185-
return arg.get();
194+
return arg.orElse(null);
186195
}
187196
}
188197

src/test/java/graphql/annotations/GraphQLInputTest.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,17 @@ public AnotherCode(@GraphQLName("firstField") Optional<String> one, @GraphQLName
144144
private final String secondField;
145145
}
146146

147+
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
148+
public static class AnotherCodeWithSingleField {
149+
150+
public AnotherCodeWithSingleField(@GraphQLName("one") Optional<String> one) {
151+
this.field = one;
152+
}
153+
154+
@GraphQLField
155+
public Optional<String> field;
156+
}
157+
147158
public static class Code {
148159
public Code(@GraphQLName("map") HashMap map) {
149160
this.firstField = (String) map.get("firstField");
@@ -164,6 +175,12 @@ public String something(@GraphQLName("code") AnotherCode code) {
164175
return (code.firstField != null ? code.firstField.orElse("") : "") + code.secondField;
165176
}
166177

178+
@SuppressWarnings({"unused", "OptionalAssignedToNull"})
179+
@GraphQLField
180+
public String somethingWithOneField(@GraphQLName("code") AnotherCodeWithSingleField code) {
181+
return code.field != null ? code.field.orElse("") : "was undefined";
182+
}
183+
167184
@SuppressWarnings({"unused", "OptionalAssignedToNull"})
168185
@GraphQLField
169186
public String otherthing(@GraphQLName("code") AnotherCode code) {
@@ -238,11 +255,12 @@ public void queryWithUndefinableParameters() {
238255
GraphQLSchema schema = newAnnotationsSchema().query(QueryUndefinedParameter.class).build();
239256

240257
GraphQL graphQL = GraphQL.newGraphQL(schema).build();
241-
ExecutionResult result = graphQL.execute("{ something(code: {firstField:\"a\",secondField:\"b\"}) otherthing(code: {secondField:\"c\"}) listthings }", new QueryUndefinedParameter());
258+
ExecutionResult result = graphQL.execute("{ something(code: {firstField:\"a\",secondField:\"b\"}) otherthing(code: {secondField:\"c\"}) listthings somethingWithOneField(code: {}) }", new QueryUndefinedParameter());
242259
assertTrue(result.getErrors().isEmpty());
243260
assertEquals(((Map<String, String>) result.getData()).get("something"), "ab");
244261
assertEquals(((Map<String, String>) result.getData()).get("otherthing"), "c");
245262
assertEquals(((Map<String, String>) result.getData()).get("listthings"), "was null");
263+
assertEquals(((Map<String, String>) result.getData()).get("somethingWithOneField"), "was undefined");
246264
}
247265

248266
@Test

0 commit comments

Comments
 (0)