Skip to content

Commit bc8003e

Browse files
committed
add support for java.util.Optional
1 parent 6ed4517 commit bc8003e

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
@@ -127,7 +130,7 @@ void shouldResolveKotlinBeanArgument() throws Exception {
127130
assertThat((KotlinBookInput) result)
128131
.hasFieldOrPropertyWithValue("name", "test name")
129132
.hasFieldOrPropertyWithValue("authorId", 42L)
130-
.hasFieldOrPropertyWithValue("notes", OptionalInput.undefined());
133+
.hasFieldOrPropertyWithValue("notes", null);
131134
}
132135

133136
@Test
@@ -141,7 +144,7 @@ void shouldResolveKotlinBeanOptionalArgument() throws Exception {
141144
assertThat((KotlinBookInput) result)
142145
.hasFieldOrPropertyWithValue("name", "test name")
143146
.hasFieldOrPropertyWithValue("authorId", 42L)
144-
.hasFieldOrPropertyWithValue("notes", OptionalInput.defined("Hello"));
147+
.hasFieldOrPropertyWithValue("notes", Optional.of("Hello"));
145148
}
146149

147150
@Test
@@ -155,7 +158,7 @@ void shouldResolveKotlinBeanOptionalNullArgument() throws Exception {
155158
assertThat((KotlinBookInput) result)
156159
.hasFieldOrPropertyWithValue("name", "test name")
157160
.hasFieldOrPropertyWithValue("authorId", 42L)
158-
.hasFieldOrPropertyWithValue("notes", OptionalInput.defined(null));
161+
.hasFieldOrPropertyWithValue("notes", Optional.empty());
159162
}
160163

161164
@Test
@@ -217,7 +220,8 @@ static class BookInput {
217220

218221
Long authorId;
219222

220-
OptionalInput<String> notes = OptionalInput.undefined();
223+
@Nullable
224+
Optional<String> notes = null;
221225

222226
public String getName() {
223227
return this.name;
@@ -235,12 +239,13 @@ public void setAuthorId(Long authorId) {
235239
this.authorId = authorId;
236240
}
237241

238-
public OptionalInput<String> getNotes() {
242+
@Nullable
243+
public Optional<String> getNotes() {
239244
return this.notes;
240245
}
241246

242-
public void setNotes(OptionalInput<String> notes) {
243-
this.notes = (notes == null) ? OptionalInput.defined(null) : notes;
247+
public void setNotes(@Nullable Optional<String> notes) {
248+
this.notes = notes;
244249
}
245250
}
246251

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)