Skip to content

UriComponentsBuilder interprets empty request parameters as null [SPR-13537] #18113

@spring-projects-issues

Description

@spring-projects-issues

Sam Brannen opened SPR-13537 and commented

Status Quo

#18101 brought it to our attention that UriComponentsBuilder returns null for empty request parameters instead of an empty string.

For example, given a request such as http://example.com/login?error, the error request parameter is empty but still present in the query string. Thus we would expect UriComponentsBuilder to store its value as an empty string; however, UriComponentsBuilder currently stores it in its query map as null.

This is contrast to Spring MVC's support for @RequestParameter. When using @RequestParameter in a controller handler method, Spring always passes in an empty string for an empty request parameter value, regardless of whether the = sign is present in the query string.

Analysis

The source of this behavior is in UriComponentsBuilder.query(String), specifically in this line:

queryParam(name, (value != null ? value : (StringUtils.hasLength(eq) ? "" : null)));

For empty request parameter values, if the = sign is present, the value is set to an empty string ""; otherwise, the value is set to null.

Furthermore, similar logic exists in UriComponentsBuilder.queryParam(String, Object...): queryParam(...) stores null instead of an empty string for an empty value.

Deliverables

  1. Ensure that UriComponentsBuilder stores empty request parameters as empty strings.
    • Updated both query(String) and queryParam(String, Object...)
    • See TODO in UriComponentsBuilderTests.

Issue Links:

Referenced from: commits c52f9d3

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: testIssues in the test modulein: webIssues in web modules (web, webmvc, webflux, websocket)status: bulk-closedAn outdated, unresolved issue that's closed in bulk as part of a cleaning process

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions