diff --git a/spring-graphql/src/main/java/org/springframework/graphql/data/GraphQlArgumentBinder.java b/spring-graphql/src/main/java/org/springframework/graphql/data/GraphQlArgumentBinder.java index 984874e0..b2abf202 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/data/GraphQlArgumentBinder.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/data/GraphQlArgumentBinder.java @@ -292,7 +292,12 @@ public class GraphQlArgumentBinder { } try { - return BeanUtils.instantiateClass(constructor, constructorArguments); + Object target = BeanUtils.instantiateClass(constructor, constructorArguments); + // only attempt further properties binding if there were no errors + if (!bindingResult.hasErrors()) { + bindProperties(rawMap, ownerType, bindingResult, target); + } + return target; } catch (BeanInstantiationException ex) { // Ignore, if we had binding errors to begin with @@ -308,6 +313,11 @@ public class GraphQlArgumentBinder { ArgumentsBindingResult bindingResult) { Object target = BeanUtils.instantiateClass(constructor); + bindProperties(rawMap, ownerType, bindingResult, target); + return target; + } + + private void bindProperties(Map rawMap, ResolvableType ownerType, ArgumentsBindingResult bindingResult, Object target) { BeanWrapper beanWrapper = (this.fallBackOnDirectFieldAccess ? new DirectFieldAccessFallbackBeanWrapper(target) : PropertyAccessorFactory.forBeanPropertyAccess(target)); @@ -343,10 +353,9 @@ public class GraphQlArgumentBinder { bindingResult.rejectArgumentValue(key, value, "invalidPropertyValue", "Failed to set property value"); } } - - return target; } + @SuppressWarnings("unchecked") @Nullable private T convertValue( diff --git a/spring-graphql/src/test/java/org/springframework/graphql/data/GraphQlArgumentBinderTests.java b/spring-graphql/src/test/java/org/springframework/graphql/data/GraphQlArgumentBinderTests.java index 680802e6..0d2983ca 100644 --- a/spring-graphql/src/test/java/org/springframework/graphql/data/GraphQlArgumentBinderTests.java +++ b/spring-graphql/src/test/java/org/springframework/graphql/data/GraphQlArgumentBinderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2020-2023 the original author or authors. + * Copyright 2020-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -195,6 +195,16 @@ class GraphQlArgumentBinderTests { assertThat(result).hasFieldOrPropertyWithValue("name", "test"); } + @Test + void mixedConstructorProperties() throws Exception { + + Object result = bind("{\"name\":\"test\", \"age\":30}", ResolvableType.forClass(MixedConstructorPropertiesBean.class)); + + assertThat(result).isNotNull().isInstanceOf(MixedConstructorPropertiesBean.class); + assertThat(result).hasFieldOrPropertyWithValue("name", "test") + .hasFieldOrPropertyWithValue("age", 30); + } + @Test void primaryConstructorWithBeanArgument() throws Exception { @@ -457,6 +467,29 @@ class GraphQlArgumentBinderTests { } } + static class MixedConstructorPropertiesBean { + + private final String name; + + private int age; + + public MixedConstructorPropertiesBean(String name) { + this.name = name; + } + + public String getName() { + return this.name; + } + + public int getAge() { + return this.age; + } + + public void setAge(int age) { + this.age = age; + } + } + static class PrimaryConstructorItemBean {