Skip to content

Cannot bind input argument with nested beans and primary constructors #155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ else if (value != null && CollectionFactory.isApproximableCollectionType(value.g
Class<?> elementType = typeDescriptor.getElementTypeDescriptor().getType();
args[i] = instantiateCollection(elementType, (Collection<Object>) value);
}
else {
else if (value instanceof Map) {
args[i] = this.instantiate((Map<String, Object>) value, methodParam.getParameterType());
} else {
args[i] = this.converter.convertIfNecessary(value, paramTypes[i], methodParam);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> arguments = this.mapper.readValue(jsonPayload, new TypeReference<Map<String, Object>>() {
});
Expand Down Expand Up @@ -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;
}
}

}