Skip to content

Unable to Mutate ServerHttpRequest with Void Method #34420

@dominic-mallo

Description

@dominic-mallo

Previously, using Spring Boot 3.3.7 & Spring Cloud 2023.0.3, it was possible to mutate a request inside of a void method.

For example, simply by calling mutate on an ServerHttpRequest object, it was possible to update the request without reassigning the variable.

In the screenshot below, I am able to mutate the request headers on the original object (request), without reassigning the variable.

Image

However, with the latest Spring Boot 3.4.2 & Spring Cloud 2024.0.0, this is no longer working. Now, it looks like the request object must have it's pointer reassigned.

Image

This seems to have been asked as-well on StackOverflow. However, the suggestion to reassign the variable seems to require unnecessary refactor.

Is this a bug or just something the community will have to deal with?

Here's an example in our application where this ability was previously useful:

Image

Image

POC Code:

@ExtendWith(MockitoExtension.class)
class HeadersUtilTest {

  @Test
  @DisplayName("Should Add Header to Request")
  void shouldAddHeaderToRequest() {
    ServerHttpRequest request = MockServerHttpRequest.post("/unit-test").build();

    ServerHttpRequest mutated = request.mutate().header("x", "y").build();

    boolean worksMutated = mutated.getHeaders().containsKey("x");
    assertTrue(worksMutated);

    boolean worksOriginal = request.getHeaders().containsKey("x");  // used to work on 3.3.7 , is broken on 3.4.2
    assertTrue(worksOriginal);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: webIssues in web modules (web, webmvc, webflux, websocket)status: invalidAn issue that we don't feel is valid

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions