Allow binding raw map to Map<String,?>
Closes gh-554
This commit is contained in:
@@ -245,10 +245,9 @@ public class GraphQlArgumentBinder {
|
||||
ArgumentsBindingResult bindingResult) {
|
||||
|
||||
ResolvableType valueType = targetType.asMap().getGeneric(1);
|
||||
Class<?> valueClass = valueType.resolve();
|
||||
if (valueClass == null) {
|
||||
bindingResult.rejectArgumentValue(name, null, "unknownType", "Unknown Map value type");
|
||||
return Collections.emptyMap(); // Keep going, to record more errors
|
||||
Class<?> valueClass = valueType.resolve(Object.class);
|
||||
if (valueClass == Object.class) {
|
||||
return rawMap;
|
||||
}
|
||||
|
||||
Map<String, Object> map = CollectionFactory.createMap(targetClass, rawMap.size());
|
||||
|
||||
@@ -318,7 +318,7 @@ class GraphQlArgumentBinderTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void primaryConstructorWithMapArgument() throws Exception {
|
||||
void primaryConstructorWithItemMapArgument() throws Exception {
|
||||
|
||||
Object result = bind(
|
||||
"{\"map\":{\"item1\":{\"name\":\"Jason\",\"age\":\"21\"},\"item2\":{\"name\":\"James\",\"age\":\"22\"}}}",
|
||||
@@ -336,6 +336,25 @@ class GraphQlArgumentBinderTests {
|
||||
assertThat(item2.getAge()).isEqualTo(22);
|
||||
}
|
||||
|
||||
@Test // gh-554
|
||||
void primaryConstructorWithRawMapArgument() throws Exception {
|
||||
|
||||
Object result = bind(
|
||||
"{\"map\":{\"item1\":{\"name\":\"Jason\",\"age\":\"21\"},\"item2\":{\"name\":\"James\",\"age\":\"22\"}}}",
|
||||
ResolvableType.forClass(PrimaryConstructorRawMapBean.class));
|
||||
|
||||
assertThat(result).isNotNull().isInstanceOf(PrimaryConstructorRawMapBean.class);
|
||||
Map<String, ?> map = ((PrimaryConstructorRawMapBean) result).getMap();
|
||||
|
||||
Map<String, Object> item1 = (Map<String, Object>) map.get("item1");
|
||||
assertThat(item1).containsEntry("name", "Jason");
|
||||
assertThat(item1).containsEntry("age", "21");
|
||||
|
||||
Map<String, Object> item2 = (Map<String, Object>) map.get("item2");
|
||||
assertThat(item2).containsEntry("name", "James");
|
||||
assertThat(item2).containsEntry("age", "22");
|
||||
}
|
||||
|
||||
@Test // gh-447
|
||||
@SuppressWarnings("unchecked")
|
||||
void primaryConstructorWithGenericObject() throws Exception {
|
||||
@@ -476,6 +495,20 @@ class GraphQlArgumentBinderTests {
|
||||
}
|
||||
|
||||
|
||||
static class PrimaryConstructorRawMapBean {
|
||||
|
||||
private final Map<String, ?> map;
|
||||
|
||||
public PrimaryConstructorRawMapBean(Map<String, ?> map) {
|
||||
this.map = map;
|
||||
}
|
||||
|
||||
public Map<String, ?> getMap() {
|
||||
return this.map;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
|
||||
static class PrimaryConstructorOptionalItemBean {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user