spring-projects / spring-framework

Spring Framework
https://spring.io/projects/spring-framework
Apache License 2.0
55.27k stars 37.62k forks source link

handling of path segments with leading spaces in HierarchicalUriComponents #32705

Open diamondT opened 3 weeks ago

diamondT commented 3 weeks ago

hi,

was wondering, is there a reason that path segments are trimmed in HierarchicalUriComponents.FullPathComponent?

https://github.com/spring-projects/spring-framework/blob/aaf33100d9fdfa11917f76048a71ca21f3eace3b/spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java#L900

for example the following test will fail

    @Test
    void pathWithLeadingSpace() {
        String theUrl = "https://path.com/seg1/seg2/ seg-with-leading-space/file.png";
        UriComponents uriComponents = UriComponentsBuilder.fromUriString(theUrl).build();
        assertThat(uriComponents.getPathSegments()).hasSameElementsAs(
                List.of("seg1", "seg2", " seg-with-leading-space", "file.png")
        );
    }

mind you, .toUri() still returns the correct thing (https://path.com/seg1/seg2/%20seg-with-leading-space/file.png), it's just that it seems weird to store the "wrong" path components.

looks like a simple change for the above line like

String[] segments = StringUtils.tokenizeToStringArray(getPath(), PATH_DELIMITER_STRING, false, true);

should do the trick.

insights?

thanks,