Skip to content

Commit 8cc8a46

Browse files
committed
add OptionalInput type to allow explicit null value detection
1 parent f2866db commit 8cc8a46

File tree

5 files changed

+191
-6
lines changed

5 files changed

+191
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package org.springframework.graphql.data.method;
2+
3+
import javax.annotation.Nullable;
4+
import java.util.Objects;
5+
6+
/**
7+
* Wrapper used to represent optionally defined input arguments that allows us to distinguish between undefined value, explicit NULL value
8+
* and specified value.
9+
*/
10+
public class OptionalInput<T> {
11+
12+
public static<T> OptionalInput<T> defined(@Nullable T value) {
13+
return new OptionalInput.Defined<>(value);
14+
}
15+
16+
@SuppressWarnings("unchecked")
17+
public static<T> OptionalInput<T> undefined() {
18+
return (OptionalInput<T>)new OptionalInput.Undefined();
19+
}
20+
21+
/**
22+
* Represents missing/undefined value.
23+
*/
24+
static class Undefined extends OptionalInput<Void> {
25+
@Override
26+
public boolean equals(Object obj) {
27+
return obj.getClass() == this.getClass();
28+
}
29+
30+
@Override
31+
public int hashCode() {
32+
return super.hashCode();
33+
}
34+
}
35+
36+
/**
37+
* Wrapper holding explicitly specified value including NULL.
38+
*/
39+
public static class Defined<T> extends OptionalInput<T> {
40+
@Nullable
41+
private final T value;
42+
43+
public Defined(@Nullable T value) {
44+
this.value = value;
45+
}
46+
47+
@Nullable
48+
public T getValue() {
49+
return value;
50+
}
51+
52+
public Boolean isEmpty() {
53+
return value == null;
54+
}
55+
56+
@Override
57+
public boolean equals(Object o) {
58+
if (this == o) return true;
59+
if (o == null || getClass() != o.getClass()) return false;
60+
Defined<?> defined = (Defined<?>) o;
61+
if (isEmpty() == defined.isEmpty()) return true;
62+
if (value == null) return false;
63+
return value.equals(defined.value);
64+
}
65+
66+
@Override
67+
public int hashCode() {
68+
return Objects.hash(value);
69+
}
70+
}
71+
}

spring-graphql/src/main/java/org/springframework/graphql/data/method/annotation/support/ArgumentMethodArgumentResolver.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.springframework.core.MethodParameter;
3232
import org.springframework.core.convert.TypeDescriptor;
3333
import org.springframework.graphql.data.method.HandlerMethodArgumentResolver;
34+
import org.springframework.graphql.data.method.OptionalInput;
3435
import org.springframework.graphql.data.method.annotation.Argument;
3536
import org.springframework.graphql.data.method.annotation.ValueConstants;
3637
import org.springframework.util.Assert;
@@ -113,10 +114,12 @@ private Object convert(Object rawValue, Class<?> targetType) {
113114
if (ctor.getParameterCount() == 0) {
114115
target = BeanUtils.instantiateClass(ctor);
115116
DataBinder dataBinder = new DataBinder(target);
117+
dataBinder.setConversionService(new OptionalInputArgumentConversionService());
116118
dataBinder.bind(propertyValues);
117119
} else {
118120
// Data class constructor
119121
DataBinder binder = new DataBinder(null);
122+
binder.setConversionService(new OptionalInputArgumentConversionService());
120123
String[] paramNames = BeanUtils.getParameterNames(ctor);
121124
Class<?>[] paramTypes = ctor.getParameterTypes();
122125
Object[] args = new Object[paramTypes.length];
@@ -125,8 +128,13 @@ private Object convert(Object rawValue, Class<?> targetType) {
125128
Object value = propertyValues.get(paramName);
126129
value = (value instanceof List ? ((List<?>) value).toArray() : value);
127130
MethodParameter methodParam = new MethodParameter(ctor, i);
128-
if (value == null && methodParam.isOptional()) {
129-
args[i] = (methodParam.getParameterType() == Optional.class ? Optional.empty() : null);
131+
132+
if (value == null) {
133+
if (methodParam.getParameterType() == OptionalInput.class) {
134+
args[i] = propertyValues.contains(paramName) ? OptionalInput.defined(null) : OptionalInput.undefined();
135+
} else if(methodParam.isOptional()) {
136+
args[i] = (methodParam.getParameterType() == Optional.class ? Optional.empty() : null);
137+
}
130138
}
131139
else {
132140
args[i] = binder.convertIfNecessary(value, paramTypes[i], methodParam);
@@ -148,6 +156,10 @@ else if (targetType.isAssignableFrom(rawValue.getClass())) {
148156
return target;
149157
}
150158

159+
private Boolean isOptionalInput(MethodParameter parameter) {
160+
return parameter.getParameterType() == OptionalInput.class;
161+
}
162+
151163
private MutablePropertyValues extractPropertyValues(Map<String, Object> arguments) {
152164
MutablePropertyValues mpvs = new MutablePropertyValues();
153165
Stack<String> path = new Stack<>();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.springframework.graphql.data.method.annotation.support;
2+
3+
import org.springframework.core.convert.ConversionService;
4+
import org.springframework.core.convert.TypeDescriptor;
5+
import org.springframework.graphql.data.method.OptionalInput;
6+
7+
public class OptionalInputArgumentConversionService implements ConversionService {
8+
@Override
9+
public boolean canConvert(Class<?> sourceType, Class<?> targetType) {
10+
return false;
11+
}
12+
13+
@Override
14+
public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType) {
15+
return targetType.getType() == OptionalInput.class;
16+
}
17+
18+
@Override
19+
public <T> T convert(Object source, Class<T> targetType) {
20+
return null;
21+
}
22+
23+
@Override
24+
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
25+
return OptionalInput.defined(source);
26+
}
27+
}

spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/ArgumentMethodArgumentResolverTests.java

+73-3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.springframework.core.DefaultParameterNameDiscoverer;
3232
import org.springframework.core.MethodParameter;
3333
import org.springframework.graphql.Book;
34+
import org.springframework.graphql.data.method.OptionalInput;
3435
import org.springframework.graphql.data.method.annotation.Argument;
3536
import org.springframework.graphql.data.method.annotation.MutationMapping;
3637
import org.springframework.graphql.data.method.annotation.QueryMapping;
@@ -83,7 +84,36 @@ void shouldResolveJavaBeanArgument() throws Exception {
8384
Object result = resolver.resolveArgument(methodParameter, environment);
8485
assertThat(result).isNotNull().isInstanceOf(BookInput.class);
8586
assertThat((BookInput) result).hasFieldOrPropertyWithValue("name", "test name")
86-
.hasFieldOrPropertyWithValue("authorId", 42L);
87+
.hasFieldOrPropertyWithValue("authorId", 42L)
88+
.hasFieldOrPropertyWithValue("notes", OptionalInput.undefined());
89+
}
90+
91+
@Test
92+
void shouldResolveJavaBeanOptionalArgument() throws Exception {
93+
Method addBook = ClassUtils.getMethod(BookController.class, "addBook", BookInput.class);
94+
String payload = "{\"bookInput\": { \"name\": \"test name\", \"authorId\": 42, \"notes\": \"Hello\"} }";
95+
DataFetchingEnvironment environment = initEnvironment(payload);
96+
MethodParameter methodParameter = getMethodParameter(addBook, 0);
97+
Object result = resolver.resolveArgument(methodParameter, environment);
98+
assertThat(result).isNotNull().isInstanceOf(BookInput.class);
99+
assertThat((BookInput) result)
100+
.hasFieldOrPropertyWithValue("name", "test name")
101+
.hasFieldOrPropertyWithValue("authorId", 42L)
102+
.hasFieldOrPropertyWithValue("notes", OptionalInput.defined("Hello"));
103+
}
104+
105+
@Test
106+
void shouldResolveJavaBeanOptionalNullArgument() throws Exception {
107+
Method addBook = ClassUtils.getMethod(BookController.class, "addBook", BookInput.class);
108+
String payload = "{\"bookInput\": { \"name\": \"test name\", \"authorId\": 42, \"notes\": null} }";
109+
DataFetchingEnvironment environment = initEnvironment(payload);
110+
MethodParameter methodParameter = getMethodParameter(addBook, 0);
111+
Object result = resolver.resolveArgument(methodParameter, environment);
112+
assertThat(result).isNotNull().isInstanceOf(BookInput.class);
113+
assertThat((BookInput) result)
114+
.hasFieldOrPropertyWithValue("name", "test name")
115+
.hasFieldOrPropertyWithValue("authorId", 42L)
116+
.hasFieldOrPropertyWithValue("notes", OptionalInput.defined(null));
87117
}
88118

89119
@Test
@@ -94,8 +124,38 @@ void shouldResolveKotlinBeanArgument() throws Exception {
94124
MethodParameter methodParameter = getMethodParameter(addBook, 0);
95125
Object result = resolver.resolveArgument(methodParameter, environment);
96126
assertThat(result).isNotNull().isInstanceOf(KotlinBookInput.class);
97-
assertThat((KotlinBookInput) result).hasFieldOrPropertyWithValue("name", "test name")
98-
.hasFieldOrPropertyWithValue("authorId", 42L);
127+
assertThat((KotlinBookInput) result)
128+
.hasFieldOrPropertyWithValue("name", "test name")
129+
.hasFieldOrPropertyWithValue("authorId", 42L)
130+
.hasFieldOrPropertyWithValue("notes", OptionalInput.undefined());
131+
}
132+
133+
@Test
134+
void shouldResolveKotlinBeanOptionalArgument() throws Exception {
135+
Method addBook = ClassUtils.getMethod(BookController.class, "ktAddBook", KotlinBookInput.class);
136+
String payload = "{\"bookInput\": { \"name\": \"test name\", \"authorId\": 42, \"notes\": \"Hello\"} }";
137+
DataFetchingEnvironment environment = initEnvironment(payload);
138+
MethodParameter methodParameter = getMethodParameter(addBook, 0);
139+
Object result = resolver.resolveArgument(methodParameter, environment);
140+
assertThat(result).isNotNull().isInstanceOf(KotlinBookInput.class);
141+
assertThat((KotlinBookInput) result)
142+
.hasFieldOrPropertyWithValue("name", "test name")
143+
.hasFieldOrPropertyWithValue("authorId", 42L)
144+
.hasFieldOrPropertyWithValue("notes", OptionalInput.defined("Hello"));
145+
}
146+
147+
@Test
148+
void shouldResolveKotlinBeanOptionalNullArgument() throws Exception {
149+
Method addBook = ClassUtils.getMethod(BookController.class, "ktAddBook", KotlinBookInput.class);
150+
String payload = "{\"bookInput\": { \"name\": \"test name\", \"authorId\": 42, \"notes\": null} }";
151+
DataFetchingEnvironment environment = initEnvironment(payload);
152+
MethodParameter methodParameter = getMethodParameter(addBook, 0);
153+
Object result = resolver.resolveArgument(methodParameter, environment);
154+
assertThat(result).isNotNull().isInstanceOf(KotlinBookInput.class);
155+
assertThat((KotlinBookInput) result)
156+
.hasFieldOrPropertyWithValue("name", "test name")
157+
.hasFieldOrPropertyWithValue("authorId", 42L)
158+
.hasFieldOrPropertyWithValue("notes", OptionalInput.defined(null));
99159
}
100160

101161
@Test
@@ -181,6 +241,8 @@ static class BookInput {
181241

182242
Long authorId;
183243

244+
OptionalInput<String> notes = OptionalInput.undefined();
245+
184246
public String getName() {
185247
return this.name;
186248
}
@@ -196,6 +258,14 @@ public Long getAuthorId() {
196258
public void setAuthorId(Long authorId) {
197259
this.authorId = authorId;
198260
}
261+
262+
public OptionalInput<String> getNotes() {
263+
return this.notes;
264+
}
265+
266+
public void setNotes(OptionalInput<String> notes) {
267+
this.notes = (notes == null) ? OptionalInput.defined(null) : notes;
268+
}
199269
}
200270

201271
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
package org.springframework.graphql.data.method.annotation.support;
22

3-
data class KotlinBookInput(val name: String, val authorId: Long)
3+
import org.springframework.graphql.data.method.OptionalInput
4+
5+
data class KotlinBookInput(
6+
val name: String, val authorId: Long,
7+
val notes: OptionalInput<String?>
8+
)

0 commit comments

Comments
 (0)