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

Clarify behavior for generics support in BeanUtils.copyProperties #27259

Closed
afattahi54 opened this issue Aug 11, 2021 · 5 comments
Closed

Clarify behavior for generics support in BeanUtils.copyProperties #27259

afattahi54 opened this issue Aug 11, 2021 · 5 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: documentation A documentation task
Milestone

Comments

@afattahi54
Copy link

The below code works in 5.2.9.RELEASE

BeanUtils.copyProperties(transferVO , transferRequest);

The classes where as follow. As you can see the source and destination beans has a List with same name but different types. But it was working

public class transferVO {
	private List<Signer> signers;
}

public class transferRequest{	
	private List<TransferSigners > signers;

}


public class Signer{
    private string name;

}

public class TransferSigners {    
    private string name;
}

After upgrade to 5.3.7 the signers property is not copied.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Aug 11, 2021
@sbrannen sbrannen added the in: core Issues in core modules (aop, beans, core, context, expression) label Aug 12, 2021
@sbrannen
Copy link
Member

sbrannen commented Aug 12, 2021

This is a potential regression introduced in conjunction with #24187.


@afattahi54, can you please also provide the setter and getter methods of those two classes so that we can inspect the actual method signatures?

Also, if Signer is not a subtype of TransferSigners, why would you expect BeanUtils to copy from List<Signer> to List<TransferSigners>?

Doing so would actually store items in the List<TransferSigners> that are not an instance of TransferSigners, and that would be a broken state.

@sbrannen sbrannen added the status: waiting-for-feedback We need additional information before we can continue label Aug 12, 2021
@afattahi54
Copy link
Author

The Signer and TransferSigners do not have same types. You are right, but as it was working before upgrade to 5.3.7 I expected it to keep working

The setters and getters are as below:

public class transferVO {
        private List<Signer> signers;
	public List<Signer> getSigners() {
		return signers;
	}

	public void setSigners(List<Signer> signers) {
		this.signers = signers;
	}

}

public class transferRequest {

       private List<TransferSigners > signers;
	public List<TransferSigners> getSigners() {
		return signers;
	}
	public void setSigners(List<TransferSigners> signers) {
		this.signers = signers;
	}

}

Thanks

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Aug 14, 2021
@bigdata-page
Copy link

bigdata-page commented Nov 24, 2021

This is a potential regression introduced in conjunction with #24187.

@afattahi54, can you please also provide the setter and getter methods of those two classes so that we can inspect the actual method signatures?

Also, if Signer is not a subtype of TransferSigners, why would you expect BeanUtils to copy from List<Signer> to List<TransferSigners>?

Doing so would actually store items in the List<TransferSigners> that are not an instance of TransferSigners, and that would be a broken state.

@sbrannen spring-beans version is 5.3.9, when Signer is a subtype of TransferSigners,use the BeanUtils.copyProperties(transferVO , transferRequest). the transferVO.signers is null. but when spring-beans version is 5.1.16.RELEASE is normal. I hope the new version is compatible with the previous version.

@sbrannen sbrannen added this to the Triage Queue milestone Nov 29, 2021
@sbrannen
Copy link
Member

Assigned to the Triage Queue so that the team can determine if this is a regression that needs to be fixed.

@rstoyanchev rstoyanchev added the type: regression A bug that is also a regression label Nov 29, 2021
@snicoll snicoll removed status: waiting-for-triage An issue we've not yet triaged or decided on status: feedback-provided Feedback has been provided labels Nov 29, 2021
@snicoll snicoll modified the milestones: Triage Queue, 5.3.14 Nov 29, 2021
@sbrannen sbrannen self-assigned this Dec 13, 2021
@sbrannen sbrannen added type: documentation A documentation task and removed type: regression A bug that is also a regression labels Dec 13, 2021
@jhoeller jhoeller modified the milestones: 5.3.14, 5.3.15 Dec 14, 2021
@sbrannen sbrannen modified the milestones: 5.3.15, 5.3.16 Jan 12, 2022
@sbrannen sbrannen modified the milestones: 5.3.16, 5.3.17 Feb 16, 2022
@sbrannen sbrannen changed the title BeanUtils.copyProperties breaks from 5.2.9.RELEASE to 5.3.7 Clarify behavior for generics support in BeanUtils.copyProperties Mar 12, 2022
@sbrannen
Copy link
Member

The Signer and TransferSigners do not have same types. You are right, but as it was working before upgrade to 5.3.7 I expected it to keep working

@afattahi54, since BeanUtils.copyProperties() honors generics since Spring Framework 5.3, you will need to ensure that the source and target property types are compatible in terms of Java semantics.

spring-beans version is 5.3.9, when Signer is a subtype of TransferSigners,use the BeanUtils.copyProperties(transferVO , transferRequest). the transferVO.signers is null. but when spring-beans version is 5.1.16.RELEASE is normal. I hope the new version is compatible with the previous version.

@bigdata-page, that can be addressed by using an upper-bounded wildcard for the target property type.


In commit 887389d, I added a table to the Javadoc for BeanUtils.copyProperties() which will hopefully help you and other users better understand the current semantics for property type matching.

BeanUtils copyProperties

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: documentation A documentation task
Projects
None yet
Development

No branches or pull requests

7 participants