Skip to content

Commit bd508e5

Browse files
committed
add support for java.util.Optional
1 parent 90d92de commit bd508e5

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public <T> T instantiate(Class<T> targetType, Map<String, Object> arguments) {
7777
if (methodParam.getParameterType() == OptionalInput.class) {
7878
args[i] = propertyValues.contains(paramName) ? OptionalInput.defined(null) : OptionalInput.undefined();
7979
} else if(methodParam.isOptional()) {
80-
args[i] = (methodParam.getParameterType() == Optional.class ? Optional.empty() : null);
80+
args[i] = (methodParam.getParameterType() == Optional.class && propertyValues.contains(paramName) ? Optional.empty() : null);
8181
}
8282
}
8383
else {

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

+9-2
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

+13-7
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
@@ -232,7 +235,8 @@ static class BookInput {
232235

233236
Long authorId;
234237

235-
OptionalInput<String> notes = OptionalInput.undefined();
238+
@Nullable
239+
Optional<String> notes = null;
236240

237241
public String getName() {
238242
return this.name;
@@ -250,12 +254,14 @@ public void setAuthorId(Long authorId) {
250254
this.authorId = authorId;
251255
}
252256

253-
public OptionalInput<String> getNotes() {
257+
@Nullable
258+
public Optional<String> getNotes() {
254259
return this.notes;
255260
}
256261

257-
public void setNotes(OptionalInput<String> notes) {
258-
this.notes = (notes == null) ? OptionalInput.defined(null) : notes;
262+
263+
public void setNotes(@Nullable Optional<String> notes) {
264+
this.notes = notes;
259265
}
260266
}
261267

0 commit comments

Comments
 (0)