Skip to content

Consider the re-design of the HttpHeaders inheritance hierarchy #35762

@mipo256

Description

@mipo256

Hey Spring Team, thank you all for the great work you're doing! I kindly request to review the following proposition:

Currently, we have HttpHeaders class in the spring http package. That class defines the methods like add or addAll to add headers. Sometimes it really helps and become really useful. However, we recently faced a bug in production, that I think will explain the problem.

Imagine the following method:

public void addRedirectionIfNecessary(HttpHeaders headers) {
    if (someCondition) {
        headers.add(HttpHeaders.LOCATION, computeLocation());
    }
}

The problem is that this code would not always work. Currently, nothing prevents a developer to either accidentally, or by totally innocent mistake pass here an instance of ReadOnlyHttpHeaders. The ReadOnlyHttpHeaders overrides the add/addAll methods and throws an UnsupportedOperationException.

This design resembles a Java Collection API. As admitted by Oracle, this is one of the most "controversial" decisions that was ever made in the Collections Framework design. Kotlin, for instance, that Spring Framework recently announced partnership with not that long ago, differentiates between mutable collections and immutable ones (like MutableList vs just List). I would take a responsibility to state, that, at least, based on my knowledge, the Kotlin's approach is generally considered to be superior of some sort in the industry since it prevents the kind of bugs that would otherwise happen.

I hope that this proposal makes sense and resonates for the core Spring team. I'll leave this issue as the placeholder for future discussions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: webIssues in web modules (web, webmvc, webflux, websocket)status: declinedA suggestion or change that we don't feel we should currently apply

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions