Skip to content

Commit c973402

Browse files
committed
add OptionalInput type to allow explicit null value detection
1 parent 02b16bd commit c973402

File tree

5 files changed

+204
-4
lines changed

5 files changed

+204
-4
lines changed
Lines changed: 71 additions & 0 deletions
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+
public 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/GraphQlArgumentInstantiator.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.beans.BeanUtils;
2727
import org.springframework.beans.MutablePropertyValues;
2828
import org.springframework.core.MethodParameter;
29+
import org.springframework.graphql.data.method.OptionalInput;
2930
import org.springframework.validation.DataBinder;
3031

3132
/**
@@ -57,11 +58,13 @@ public <T> T instantiate(Class<T> targetType, Map<String, Object> arguments) {
5758
if (ctor.getParameterCount() == 0) {
5859
target = BeanUtils.instantiateClass(ctor);
5960
DataBinder dataBinder = new DataBinder(target);
61+
dataBinder.setConversionService(new OptionalInputArgumentConversionService());
6062
dataBinder.bind(propertyValues);
6163
}
6264
else {
6365
// Data class constructor
6466
DataBinder binder = new DataBinder(null);
67+
binder.setConversionService(new OptionalInputArgumentConversionService());
6568
String[] paramNames = BeanUtils.getParameterNames(ctor);
6669
Class<?>[] paramTypes = ctor.getParameterTypes();
6770
Object[] args = new Object[paramTypes.length];
@@ -70,8 +73,12 @@ public <T> T instantiate(Class<T> targetType, Map<String, Object> arguments) {
7073
Object value = propertyValues.get(paramName);
7174
value = (value instanceof List ? ((List<?>) value).toArray() : value);
7275
MethodParameter methodParam = new MethodParameter(ctor, i);
73-
if (value == null && methodParam.isOptional()) {
74-
args[i] = (methodParam.getParameterType() == Optional.class ? Optional.empty() : null);
76+
if (value == null) {
77+
if (methodParam.getParameterType() == OptionalInput.class) {
78+
args[i] = propertyValues.contains(paramName) ? OptionalInput.defined(null) : OptionalInput.undefined();
79+
} else if(methodParam.isOptional()) {
80+
args[i] = (methodParam.getParameterType() == Optional.class ? Optional.empty() : null);
81+
}
7582
}
7683
else {
7784
args[i] = binder.convertIfNecessary(value, paramTypes[i], methodParam);
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

Lines changed: 89 additions & 2 deletions
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,11 +84,40 @@ 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());
8789
}
8890

8991
@Test
90-
void shouldResolveDefaultValue() throws Exception {
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));
117+
}
118+
119+
@Test
120+
void shouldResolveDefaultValue() throws Exception {
91121
Method findWithDefault = ClassUtils.getMethod(BookController.class, "findWithDefault", Long.class);
92122
String payload = "{\"name\": \"test\" }";
93123
DataFetchingEnvironment environment = initEnvironment(payload);
@@ -96,6 +126,48 @@ void shouldResolveDefaultValue() throws Exception {
96126
assertThat(result).isNotNull().isInstanceOf(Long.class).isEqualTo(42L);
97127
}
98128

129+
@Test
130+
void shouldResolveKotlinBeanArgument() throws Exception {
131+
Method addBook = ClassUtils.getMethod(BookController.class, "ktAddBook", KotlinBookInput.class);
132+
String payload = "{\"bookInput\": { \"name\": \"test name\", \"authorId\": 42} }";
133+
DataFetchingEnvironment environment = initEnvironment(payload);
134+
MethodParameter methodParameter = getMethodParameter(addBook, 0);
135+
Object result = resolver.resolveArgument(methodParameter, environment);
136+
assertThat(result).isNotNull().isInstanceOf(KotlinBookInput.class);
137+
assertThat((KotlinBookInput) result)
138+
.hasFieldOrPropertyWithValue("name", "test name")
139+
.hasFieldOrPropertyWithValue("authorId", 42L)
140+
.hasFieldOrPropertyWithValue("notes", OptionalInput.undefined());
141+
}
142+
143+
@Test
144+
void shouldResolveKotlinBeanOptionalArgument() throws Exception {
145+
Method addBook = ClassUtils.getMethod(BookController.class, "ktAddBook", KotlinBookInput.class);
146+
String payload = "{\"bookInput\": { \"name\": \"test name\", \"authorId\": 42, \"notes\": \"Hello\"} }";
147+
DataFetchingEnvironment environment = initEnvironment(payload);
148+
MethodParameter methodParameter = getMethodParameter(addBook, 0);
149+
Object result = resolver.resolveArgument(methodParameter, environment);
150+
assertThat(result).isNotNull().isInstanceOf(KotlinBookInput.class);
151+
assertThat((KotlinBookInput) result)
152+
.hasFieldOrPropertyWithValue("name", "test name")
153+
.hasFieldOrPropertyWithValue("authorId", 42L)
154+
.hasFieldOrPropertyWithValue("notes", OptionalInput.defined("Hello"));
155+
}
156+
157+
@Test
158+
void shouldResolveKotlinBeanOptionalNullArgument() throws Exception {
159+
Method addBook = ClassUtils.getMethod(BookController.class, "ktAddBook", KotlinBookInput.class);
160+
String payload = "{\"bookInput\": { \"name\": \"test name\", \"authorId\": 42, \"notes\": null} }";
161+
DataFetchingEnvironment environment = initEnvironment(payload);
162+
MethodParameter methodParameter = getMethodParameter(addBook, 0);
163+
Object result = resolver.resolveArgument(methodParameter, environment);
164+
assertThat(result).isNotNull().isInstanceOf(KotlinBookInput.class);
165+
assertThat((KotlinBookInput) result)
166+
.hasFieldOrPropertyWithValue("name", "test name")
167+
.hasFieldOrPropertyWithValue("authorId", 42L)
168+
.hasFieldOrPropertyWithValue("notes", OptionalInput.defined(null));
169+
}
170+
99171
@Test
100172
void shouldResolveListOfJavaBeansArgument() throws Exception {
101173
Method addBooks = ClassUtils.getMethod(BookController.class, "addBooks", List.class);
@@ -142,6 +214,11 @@ public Book addBook(@Argument BookInput bookInput) {
142214
return null;
143215
}
144216

217+
@MutationMapping
218+
public Book ktAddBook(@Argument KotlinBookInput bookInput) {
219+
return null;
220+
}
221+
145222
@MutationMapping
146223
public List<Book> addBooks(@Argument List<Book> books) {
147224
return null;
@@ -155,6 +232,8 @@ static class BookInput {
155232

156233
Long authorId;
157234

235+
OptionalInput<String> notes = OptionalInput.undefined();
236+
158237
public String getName() {
159238
return this.name;
160239
}
@@ -170,6 +249,14 @@ public Long getAuthorId() {
170249
public void setAuthorId(Long authorId) {
171250
this.authorId = authorId;
172251
}
252+
253+
public OptionalInput<String> getNotes() {
254+
return this.notes;
255+
}
256+
257+
public void setNotes(OptionalInput<String> notes) {
258+
this.notes = (notes == null) ? OptionalInput.defined(null) : notes;
259+
}
173260
}
174261

175262
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.springframework.graphql.data.method.annotation.support;
2+
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)