Skip to content

UriComponentsBuilder.fromUriString incorrectly captures fragment for relative URI with fragment #36759

@sashatrn

Description

@sashatrn

UriComponentsBuilder.fromUriString(...) appears to parse a relative URI with a fragment incorrectly.

For the input:

path2#foo

the resulting UriComponents has:

  • path = "path2"
  • fragment = "path2#foo"

instead of:

  • path = "path2"
  • fragment = "foo"

As a result, toUriString() produces:

path2#path2#foo

instead of:

path2#foo

Reproduction

import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

public class Repro {
    public static void main(String[] args) {
        UriComponents u = UriComponentsBuilder.fromUriString("path2#foo").build();

        System.out.println("path=" + u.getPath());
        System.out.println("fragment=" + u.getFragment());
        System.out.println("uriString=" + u.toUriString());
    }
}

Actual result

path=path2
fragment=path2#foo
uriString=path2#path2#foo

Expected result

path=path2
fragment=foo
uriString=path2#foo

Version
Observed with:

  • spring-web 6.2.18

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions