Skip to content
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

Support option for @Argument binding to fall back on direct field access #599

Closed
sataphat opened this issue Jan 18, 2023 · 1 comment
Closed
Assignees
Labels
type: enhancement A general enhancement
Milestone

Comments

@sataphat
Copy link

Sometimes, the class to use as the target for @Argument might not have a setter for all its properties. E.g.

public class PriceValuation4 {
    // ...
    protected List<ActiveOrHistoricCurrencyAndAmount> ttlNAV;
    protected List<UnitPrice15> pricDtls;
    protected List<ValuationStatistics3> valtnSttstcs;
    // ...
    public PriceValuation4 addTtlNAV(ActiveOrHistoricCurrencyAndAmount ttlNAV) {
        this.getTtlNAV().add(ttlNAV);
        return this;
    }

    public PriceValuation4 addPricDtls(UnitPrice15 pricDtls) {
        this.getPricDtls().add(pricDtls);
        return this;
    }

    public PriceValuation4 addValtnSttstcs(ValuationStatistics3 valtnSttstcs) {
        this.getValtnSttstcs().add(valtnSttstcs);
        return this;
    }
}

Especially when the class is from a third-party library, which is my case here.

Is it possible to fall back to using the direct-field-access approach when the proper Setter cannot be found?

Would the following code change make sense?

From:
https://github.com/spring-projects/spring-graphql/blob/main/spring-graphql/src/main/java/org/springframework/graphql/data/GraphQlArgumentBinder.java#L300

public class GraphQlArgumentBinder {
	// ...

	private Object bindMapToObjectViaSetters(
			Map<String, Object> rawMap, Constructor<?> constructor, ResolvableType ownerType,
			ArgumentsBindingResult bindingResult) {

		Object target = BeanUtils.instantiateClass(constructor);
		BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(target);

		for (Map.Entry<String, Object> entry : rawMap.entrySet()) {
		// ...
		}
	}        
}

To:

import org.springframework.data.util.DirectFieldAccessFallbackBeanWrapper;

public class GraphQlArgumentBinder {
	// ...

	private Object bindMapToObjectViaSetters(
			Map<String, Object> rawMap, Constructor<?> constructor, ResolvableType ownerType,
			ArgumentsBindingResult bindingResult) {

		Object target = BeanUtils.instantiateClass(constructor);
		/*========================================================================*/
		DirectFieldAccessFallbackBeanWrapper beanWrapper = new DirectFieldAccessFallbackBeanWrapper(target);
		/*========================================================================*/

		for (Map.Entry<String, Object> entry : rawMap.entrySet()) {
		// ...
		}
	}        
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 18, 2023
@rstoyanchev
Copy link
Contributor

This could be done but on an opt-in basis, i.e. enabled through a configuration option.

@rstoyanchev rstoyanchev added the type: enhancement A general enhancement label Feb 1, 2023
@rstoyanchev rstoyanchev added this to the 1.2 Backlog milestone Feb 1, 2023
@rstoyanchev rstoyanchev changed the title Support @Argument binding to a parameter class that may not have proper setters Support option for @Argument binding to fall back on direct field access Feb 1, 2023
@rstoyanchev rstoyanchev removed the status: waiting-for-triage An issue we've not yet triaged label Feb 1, 2023
@rstoyanchev rstoyanchev modified the milestones: 1.2 Backlog, 1.2.0-RC1 Mar 22, 2023
@bclozel bclozel self-assigned this Mar 23, 2023
@rstoyanchev rstoyanchev assigned rstoyanchev and unassigned bclozel Apr 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

4 participants