Skip to content

Commit 7f89bb9

Browse files
committed
Support argument binding to java.util.Set property
Closes gh-394
1 parent d9f815e commit 7f89bb9

File tree

1 file changed

+37
-49
lines changed

1 file changed

+37
-49
lines changed

spring-graphql/src/test/java/org/springframework/graphql/data/GraphQlArgumentBinderTests.java

+37-49
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,21 @@ void dataBindingWithNestedBeanListProperty() throws Exception {
9999
.hasSize(2).extracting("name").containsExactly("first", "second");
100100
}
101101

102+
@Test // gh-394
103+
void dataBindingWithNestedBeanSetProperty() throws Exception {
104+
105+
String items = IntStream.range(0, 5)
106+
.mapToObj(value -> "{\"name\":\"test" + value + "\"}")
107+
.collect(Collectors.joining(","));
108+
109+
Object result = this.binder.bind(
110+
environment("{\"key\":{\"items\":[" + items + "]}}"), "key",
111+
ResolvableType.forClass(ItemSetHolder.class));
112+
113+
assertThat(result).isNotNull().isInstanceOf(ItemSetHolder.class);
114+
assertThat(((ItemSetHolder) result).getItems()).hasSize(5);
115+
}
116+
102117
@Test // gh-301
103118
void dataBindingWithNestedBeanListEmpty() throws Exception {
104119

@@ -247,6 +262,28 @@ void primaryConstructorWithNestedBeanList() throws Exception {
247262
.hasSize(2).extracting("name").containsExactly("first", "second");
248263
}
249264

265+
@Test // gh-410
266+
void primaryConstructorWithNestedBeanSingletonList() throws Exception {
267+
268+
Map<String, String> itemMap = new HashMap<>();
269+
itemMap.put("name", "Joe");
270+
itemMap.put("age", "37");
271+
272+
Map<String, Object> arguments = new HashMap<>();
273+
arguments.put("key", Collections.singletonMap("items", Collections.singletonList(itemMap)));
274+
275+
Object result = this.binder.bind(
276+
DataFetchingEnvironmentImpl.newDataFetchingEnvironment().arguments(arguments).build(), "key",
277+
ResolvableType.forClass(PrimaryConstructorItemListBean.class));
278+
279+
assertThat(result).isNotNull().isInstanceOf(PrimaryConstructorItemListBean.class);
280+
List<Item> items = ((PrimaryConstructorItemListBean) result).getItems();
281+
282+
assertThat(items).hasSize(1);
283+
assertThat(items.get(0).getName()).isEqualTo("Joe");
284+
assertThat(items.get(0).getAge()).isEqualTo(37);
285+
}
286+
250287
@Test
251288
void primaryConstructorNotFound() {
252289
assertThatThrownBy(
@@ -353,51 +390,6 @@ void primaryConstructorWithGenericObject() throws Exception {
353390
Collections.singletonMap("name", "second"));
354391
}
355392

356-
@Test // gh-410
357-
@SuppressWarnings("unchecked")
358-
void coercionWithSingletonList() throws Exception {
359-
360-
Map<String, String> itemMap = new HashMap<>();
361-
itemMap.put("name", "Joe");
362-
itemMap.put("age", "37");
363-
364-
Map<String, Object> arguments = new HashMap<>();
365-
arguments.put("key", Collections.singletonList(itemMap));
366-
367-
DataFetchingEnvironment environment =
368-
DataFetchingEnvironmentImpl.newDataFetchingEnvironment().arguments(arguments).build();
369-
370-
Object result = this.binder.bind(environment, "key",
371-
ResolvableType.forClassWithGenerics(List.class, Item.class));
372-
373-
assertThat(result).isNotNull().isInstanceOf(List.class);
374-
List<Item> items = (List<Item>) result;
375-
376-
assertThat(items).hasSize(1);
377-
assertThat(items.get(0).getName()).isEqualTo("Joe");
378-
assertThat(items.get(0).getAge()).isEqualTo(37);
379-
}
380-
381-
@Test // gh-392
382-
void shouldHaveHigherDefaultAutoGrowLimit() throws Exception {
383-
String items = IntStream.range(0, 260).mapToObj(value -> "{\"name\":\"test\"}").collect(Collectors.joining(","));
384-
Object result = this.binder.bind(
385-
environment("{\"key\":{\"items\":[" + items + "]}}"), "key",
386-
ResolvableType.forClass(ItemListHolder.class));
387-
assertThat(result).isNotNull().isInstanceOf(ItemListHolder.class);
388-
assertThat(((ItemListHolder) result).getItems()).hasSize(260);
389-
}
390-
391-
@Test
392-
void shouldUseTargetCollectionType() throws Exception {
393-
String items = IntStream.range(0, 5).mapToObj(value -> "{\"name\":\"test" + value + "\"}").collect(Collectors.joining(","));
394-
Object result = this.binder.bind(
395-
environment("{\"key\":{\"items\":[" + items + "]}}"), "key",
396-
ResolvableType.forClass(ItemSetHolder.class));
397-
assertThat(result).isNotNull().isInstanceOf(ItemSetHolder.class);
398-
assertThat(((ItemSetHolder) result).getItems()).hasSize(5);
399-
}
400-
401393
@SuppressWarnings("unchecked")
402394
private DataFetchingEnvironment environment(String jsonPayload) throws JsonProcessingException {
403395
Map<String, Object> arguments = this.mapper.readValue(jsonPayload, Map.class);
@@ -574,10 +566,6 @@ static class ItemSetHolder {
574566

575567
private Set<Item> items;
576568

577-
public ItemSetHolder(Set<Item> items) {
578-
this.items = items;
579-
}
580-
581569
public Set<Item> getItems() {
582570
return items;
583571
}

0 commit comments

Comments
 (0)