diff --git a/spring-graphql/src/main/java/org/springframework/graphql/data/method/annotation/support/GraphQlArgumentInstantiator.java b/spring-graphql/src/main/java/org/springframework/graphql/data/method/annotation/support/GraphQlArgumentInstantiator.java index 75fef4881..eb51f4878 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/data/method/annotation/support/GraphQlArgumentInstantiator.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/data/method/annotation/support/GraphQlArgumentInstantiator.java @@ -89,7 +89,9 @@ else if (value != null && CollectionFactory.isApproximableCollectionType(value.g Class elementType = typeDescriptor.getElementTypeDescriptor().getType(); args[i] = instantiateCollection(elementType, (Collection) value); } - else { + else if (value instanceof Map) { + args[i] = this.instantiate((Map) value, methodParam.getParameterType()); + } else { args[i] = this.converter.convertIfNecessary(value, paramTypes[i], methodParam); } } diff --git a/spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/GraphQlArgumentInstantiatorTests.java b/spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/GraphQlArgumentInstantiatorTests.java index 3f01cd15f..bdd2e99b7 100644 --- a/spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/GraphQlArgumentInstantiatorTests.java +++ b/spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/GraphQlArgumentInstantiatorTests.java @@ -103,6 +103,17 @@ void shouldInstantiatePrimaryConstructorNestedBeanLists() throws Exception { assertThat(result.getItems()).hasSize(2).extracting("name").containsExactly("first", "second"); } + @Test + void shouldInstantiateComplexNestedBean() throws Exception { + String payload = "{\"complex\": { \"item\": {\"name\": \"Item name\"}, \"name\": \"Hello\" } }"; + DataFetchingEnvironment environment = initEnvironment(payload); + PrimaryConstructorComplexInput result = instantiator.instantiate(environment.getArgument("complex"), PrimaryConstructorComplexInput.class); + + assertThat(result).isNotNull().isInstanceOf(PrimaryConstructorComplexInput.class); + assertThat(result.item.name).isEqualTo("Item name"); + assertThat(result.name).isEqualTo("Hello"); + } + private DataFetchingEnvironment initEnvironment(String jsonPayload) throws JsonProcessingException { Map arguments = this.mapper.readValue(jsonPayload, new TypeReference>() { }); @@ -182,5 +193,24 @@ public void setName(String name) { this.name = name; } } + + static class PrimaryConstructorComplexInput { + final String name; + + final Item item; + + public PrimaryConstructorComplexInput(String name, Item item) { + this.name = name; + this.item = item; + } + + public String getName() { + return this.name; + } + + public Item getItem() { + return item; + } + } } \ No newline at end of file