Skip to content

ClassPathResource protected constructor should do StringUtils.cleanPath(path) as public constructors do. [SPR-5115] #9788

@spring-projects-issues

Description

@spring-projects-issues

Igor E. Poteryaev opened SPR-5115 and commented

Protected constructor in ClassPathResource, which is called from createRelative() does not clean path parameter as public constructors do.
I think that it is inconsistent and add unneeded complication in createRelative() API usage.

Supposed fix is 1-liner. Change line 107 in org.springframework.core.io.ClassPathResource.java
from
this.path = path;
to
this.path = StringUtils.cleanPath(path);

And add test for this fix to org.springframework.core.io.ResourceTests method testClassPathResource():

public void testClassPathResource() throws IOException {
	Resource resource = new ClassPathResource("org/springframework/core/io/Resource.class");
	doTestResource(resource);
	Resource resource2 = new ClassPathResource("org/springframework/core/../core/io/./Resource.class");
	assertEquals(resource, resource2);

//++++
Resource resource3 = new ClassPathResource("org/springframework/core/").createRelative("../core/io/./Resource.class");
assertEquals(resource, resource3);
//====
// Check whether equal/hashCode works in a HashSet.
HashSet resources = new HashSet();
resources.add(resource);
resources.add(resource2);
assertEquals(1, resources.size());
}

Cheers,
Igor.


Affects: 2.5.5

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions