Skip to content

Commit 7a52b0c

Browse files
committed
add support for java.util.Optional
1 parent 089fc59 commit 7a52b0c

File tree

4 files changed

+27
-15
lines changed

4 files changed

+27
-15
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public <T> T instantiate(Map<String, Object> arguments, Class<T> targetType) {
8787
if (methodParam.getParameterType() == OptionalInput.class) {
8888
args[i] = arguments.containsKey(paramName) ? OptionalInput.defined(null) : OptionalInput.undefined();
8989
} else if(methodParam.isOptional()) {
90-
args[i] = (methodParam.getParameterType() == Optional.class ? Optional.empty() : null);
90+
args[i] = (methodParam.getParameterType() == Optional.class && arguments.containsKey(paramName) ? Optional.empty() : null);
9191
}
9292
}
9393
else if (CollectionFactory.isApproximableCollectionType(value.getClass())) {

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import org.springframework.core.convert.TypeDescriptor;
55
import org.springframework.graphql.data.method.OptionalInput;
66

7+
import javax.swing.text.html.Option;
8+
import java.util.Optional;
9+
710
public class OptionalInputArgumentConversionService implements ConversionService {
811
@Override
912
public boolean canConvert(Class<?> sourceType, Class<?> targetType) {
@@ -12,7 +15,7 @@ public boolean canConvert(Class<?> sourceType, Class<?> targetType) {
1215

1316
@Override
1417
public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType) {
15-
return targetType.getType() == OptionalInput.class;
18+
return targetType.getType() == OptionalInput.class || targetType.getType() == Optional.class;
1619
}
1720

1821
@Override
@@ -22,6 +25,10 @@ public <T> T convert(Object source, Class<T> targetType) {
2225

2326
@Override
2427
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
25-
return OptionalInput.defined(source);
28+
if (targetType.getType() == Optional.class) {
29+
return Optional.of(source);
30+
} else {
31+
return OptionalInput.defined(source);
32+
}
2633
}
2734
}

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.lang.reflect.Method;
2121
import java.util.List;
2222
import java.util.Map;
23+
import java.util.Optional;
2324

2425
import com.fasterxml.jackson.core.JsonProcessingException;
2526
import com.fasterxml.jackson.core.type.TypeReference;
@@ -38,6 +39,8 @@
3839
import org.springframework.stereotype.Controller;
3940
import org.springframework.util.ClassUtils;
4041

42+
import javax.annotation.Nullable;
43+
4144
import static org.assertj.core.api.Assertions.assertThat;
4245

4346
/**
@@ -85,7 +88,7 @@ void shouldResolveJavaBeanArgument() throws Exception {
8588
assertThat(result).isNotNull().isInstanceOf(BookInput.class);
8689
assertThat((BookInput) result).hasFieldOrPropertyWithValue("name", "test name")
8790
.hasFieldOrPropertyWithValue("authorId", 42L)
88-
.hasFieldOrPropertyWithValue("notes", OptionalInput.undefined());
91+
.hasFieldOrPropertyWithValue("notes", null);
8992
}
9093

9194
@Test
@@ -99,7 +102,7 @@ void shouldResolveJavaBeanOptionalArgument() throws Exception {
99102
assertThat((BookInput) result)
100103
.hasFieldOrPropertyWithValue("name", "test name")
101104
.hasFieldOrPropertyWithValue("authorId", 42L)
102-
.hasFieldOrPropertyWithValue("notes", OptionalInput.defined("Hello"));
105+
.hasFieldOrPropertyWithValue("notes", Optional.of("Hello"));
103106
}
104107

105108
@Test
@@ -113,7 +116,7 @@ void shouldResolveJavaBeanOptionalNullArgument() throws Exception {
113116
assertThat((BookInput) result)
114117
.hasFieldOrPropertyWithValue("name", "test name")
115118
.hasFieldOrPropertyWithValue("authorId", 42L)
116-
.hasFieldOrPropertyWithValue("notes", OptionalInput.defined(null));
119+
.hasFieldOrPropertyWithValue("notes", Optional.empty());
117120
}
118121

119122
@Test
@@ -146,7 +149,7 @@ void shouldResolveKotlinBeanArgument() throws Exception {
146149
assertThat((KotlinBookInput) result)
147150
.hasFieldOrPropertyWithValue("name", "test name")
148151
.hasFieldOrPropertyWithValue("authorId", 42L)
149-
.hasFieldOrPropertyWithValue("notes", OptionalInput.undefined());
152+
.hasFieldOrPropertyWithValue("notes", null);
150153
}
151154

152155
@Test
@@ -160,7 +163,7 @@ void shouldResolveKotlinBeanOptionalArgument() throws Exception {
160163
assertThat((KotlinBookInput) result)
161164
.hasFieldOrPropertyWithValue("name", "test name")
162165
.hasFieldOrPropertyWithValue("authorId", 42L)
163-
.hasFieldOrPropertyWithValue("notes", OptionalInput.defined("Hello"));
166+
.hasFieldOrPropertyWithValue("notes", Optional.of("Hello"));
164167
}
165168

166169
@Test
@@ -174,7 +177,7 @@ void shouldResolveKotlinBeanOptionalNullArgument() throws Exception {
174177
assertThat((KotlinBookInput) result)
175178
.hasFieldOrPropertyWithValue("name", "test name")
176179
.hasFieldOrPropertyWithValue("authorId", 42L)
177-
.hasFieldOrPropertyWithValue("notes", OptionalInput.defined(null));
180+
.hasFieldOrPropertyWithValue("notes", Optional.empty());
178181
}
179182

180183
@Test
@@ -246,7 +249,8 @@ static class BookInput {
246249

247250
Long authorId;
248251

249-
OptionalInput<String> notes = OptionalInput.undefined();
252+
@Nullable
253+
Optional<String> notes = null;
250254

251255
public String getName() {
252256
return this.name;
@@ -264,12 +268,13 @@ public void setAuthorId(Long authorId) {
264268
this.authorId = authorId;
265269
}
266270

267-
public OptionalInput<String> getNotes() {
271+
@Nullable
272+
public Optional<String> getNotes() {
268273
return this.notes;
269274
}
270275

271-
public void setNotes(OptionalInput<String> notes) {
272-
this.notes = (notes == null) ? OptionalInput.defined(null) : notes;
276+
public void setNotes(@Nullable Optional<String> notes) {
277+
this.notes = notes;
273278
}
274279
}
275280

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

3-
import org.springframework.graphql.data.method.OptionalInput
3+
import java.util.Optional
44

55
data class KotlinBookInput(
66
val name: String, val authorId: Long,
7-
val notes: OptionalInput<String?>
7+
val notes: Optional<String?>?
88
)

0 commit comments

Comments
 (0)