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

Review collection/map converter optimizations that don't create new collection instances [SPR-6213] #10881

Closed
spring-projects-issues opened this issue Oct 9, 2009 · 6 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: declined A suggestion or change that we don't feel we should currently apply type: enhancement A general enhancement

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Oct 9, 2009

Lance Arlaus opened SPR-6213 and commented

See comments below

This bug was originally encountered while converting lists. Passing in a list for conversion to list, one would expect a new list containing the elements of the original list.
Instead, the converted object that's returned is the original list itself.

The test case below demonstrates this.

Note that this test succeeds in version 3.0.0.M4

public class DefaultConversionServiceBugTest {

	@Test
	@SuppressWarnings("unchecked")
	public void testUnmodifiableListConversion() {
		List<String> stringList = new ArrayList<String>();
		stringList.add("foo");
		stringList.add("bar");

		List<String> frozenList = Collections.unmodifiableList(stringList);
		ConversionService conversionService = new DefaultConversionService();
		
		List<String> converted = conversionService.convert(frozenList, List.class);

		// The converted list should contain all the elements in the original list
		Assert.assertEquals(frozenList, converted);
		Assert.assertNotSame(frozenList, converted);
	}
	
}

Affects: 3.0 RC1

Issue Links:

@spring-projects-issues
Copy link
Collaborator Author

Keith Donald commented

This issue should be fixed in trunk and for upcoming rc2. Can you confirm for your env?

@spring-projects-issues
Copy link
Collaborator Author

Lance Arlaus commented

This issue has not been resolved in RC2
I ran the unit test defined above which still fails

@spring-projects-issues
Copy link
Collaborator Author

Keith Donald commented

What is happening is the CollectionToCollection converter is deciding not to create a new List since the target List type is compatible with the source List type AND the element types of the source list is compatible with the target list. This is basically an optimization. We need to consider if this is desirable default behavior. I assume in your case you're concerned about the frozen list being subsequently modified after being bound to the target field?

@spring-projects-issues
Copy link
Collaborator Author

Keith Donald commented

Re-opening but also updating description and reclassifying as improvement.

@spring-projects-issues
Copy link
Collaborator Author

Lance Arlaus commented

After giving this one a little more thought, I would be inclined to stick with the current behavior of not copying the list for a noop conversion.

The responsibility of a conversion service, after all, is to convert, not to clone. The user of the conversion service can decide to copy the resulting collection. Otherwise, it's best not to incur the overhead of what could be an expensive (and undesired) operation.

@spring-projects-issues
Copy link
Collaborator Author

Keith Donald commented

We agree. Closing out.

@spring-projects-issues spring-projects-issues added status: declined A suggestion or change that we don't feel we should currently apply type: enhancement A general enhancement in: core Issues in core modules (aop, beans, core, context, expression) labels Jan 11, 2019
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) status: declined A suggestion or change that we don't feel we should currently apply type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants