Skip to content

Creating relative resource for URL resource should create relative Resource even if relative path has URL in it #28522

Closed
@filiphr

Description

@filiphr

Affects: 5.3.19 (most likely any version)

We were using the combination of ResourcePatternResolver#getResources and Resource#createRelative for something and received a bug report from a client that doing Resource#createRelative was resolving to an entirely different resource.

e.g.

We have an empty folder named reports on the classpath and if we run the test below it is going to fail because the creation of a relative resource for "http://localhost:8080" will be a resource pointing to that URL and not relative to "classpath*:reports/"

public class UrlResourceTest {

    @Test
    void testRelativeResource() throws IOException {
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        for (Resource resource : resolver.getResources("classpath*:reports/")) {
            Resource relative = resource.createRelative("image.png");
            assertThat(relative.getURL().toString())
                    .contains("reports");
            Resource relativeUrl = resource.createRelative("http://localhost:8080");

            assertThat(relativeUrl.getURL().toString())
                    .contains("reports");

        }
    }
}

We realized that in this example the resolved resource for "classpath*:reports/" is a UrlResource and doing createRelative on it will delegate to the JDK new URL(this.url, relativePath).

We were expecting that doing Resource#createRelative will treat the passed relative path as relative, irregardless whether it is a URL, or something else. Were we wrong with our expectations?

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)in: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions