Skip to content

MimeMappings copies of DEFAULT are empty and equals is asymmetric - #52282

Open
zhoubiao188 wants to merge 1 commit into
spring-projects:4.0.xfrom
zhoubiao188:fix-web-server-mime-mappings-copy
Open

zhoubiao188 wants to merge 1 commit into
spring-projects:4.0.xfrom
zhoubiao188:fix-web-server-mime-mappings-copy

Conversation

@zhoubiao188

Copy link
Copy Markdown
Contributor

MimeMappings.DEFAULT is backed by DefaultMimeMappings, which keeps the complete set of mappings in a lazily loaded map and only overrides get, getAll and getMap. The copy constructor and unmodifiableMappings read the internal map field instead of calling getMap(), and that field is always empty for DEFAULT.

As a result:

  • new MimeMappings(MimeMappings.DEFAULT) returns an empty instance, so the usual "start from the defaults and add a custom mapping" pattern silently drops all 1021 default mappings. Passing such a copy to ConfigurableServletWebServerFactory#setMimeMappings, which copies again in ServletWebServerSettings, leaves the server without Spring Boot's MIME mappings.
  • MimeMappings.unmodifiableMappings(MimeMappings.DEFAULT) returns an empty unmodifiable view.
  • equals is asymmetric: new MimeMappings(MimeMappings.DEFAULT).equals(MimeMappings.DEFAULT) returns true, while MimeMappings.DEFAULT.equals(new MimeMappings(MimeMappings.DEFAULT)) returns false, even though the two instances hold the same mappings. This also makes the result depend on which side of the comparison is the default instance.

This commit uses getMap() in both places so that lazily loaded mappings take part in the copy and in the comparison.

The new tests in MimeMappingsTests cover copying DEFAULT (including that the copy stays independent of it), creating an unmodifiable view of DEFAULT, and the symmetry of equals. They fail without the change and pass with it. The full :module:spring-boot-web-server test suite (249 tests) passes, as do checkFormat and checkstyle.

`MimeMappings.DEFAULT` is a `DefaultMimeMappings` that keeps its mappings in
a lazily loaded map. The copy constructor and `unmodifiableMappings` read the
internal `map` field directly, which is always empty for `DEFAULT`, so a copy
of the default mappings was empty and lost all 1021 default entries.

The same field access in `equals` also made comparisons asymmetric: a copy of
`DEFAULT` considered itself equal to `DEFAULT` while `DEFAULT` considered
itself different from the copy.

Use the (possibly overridden) `getMap()` method when copying and comparing so
that lazily loaded mappings are taken into account.

Signed-off-by: zhoubiao188 <32508238+zhoubiao188@users.noreply.github.com>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Oct 18, 2026
@wilkinsona wilkinsona added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Oct 19, 2026
@wilkinsona wilkinsona added this to the 4.0.x milestone Oct 19, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: bug A general bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants