Repository navigation
MimeMappings copies of DEFAULT are empty and equals is asymmetric - #52282
Open
zhoubiao188 wants to merge 1 commit into
Open
zhoubiao188 wants to merge 1 commit into
zhoubiao188 wants to merge 1 commit into
Conversation
`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>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
MimeMappings.DEFAULTis backed byDefaultMimeMappings, which keeps the complete set of mappings in a lazily loaded map and only overridesget,getAllandgetMap. The copy constructor andunmodifiableMappingsread the internalmapfield instead of callinggetMap(), and that field is always empty forDEFAULT.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 toConfigurableServletWebServerFactory#setMimeMappings, which copies again inServletWebServerSettings, leaves the server without Spring Boot's MIME mappings.MimeMappings.unmodifiableMappings(MimeMappings.DEFAULT)returns an empty unmodifiable view.equalsis asymmetric:new MimeMappings(MimeMappings.DEFAULT).equals(MimeMappings.DEFAULT)returnstrue, whileMimeMappings.DEFAULT.equals(new MimeMappings(MimeMappings.DEFAULT))returnsfalse, 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
MimeMappingsTestscover copyingDEFAULT(including that the copy stays independent of it), creating an unmodifiable view ofDEFAULT, and the symmetry ofequals. They fail without the change and pass with it. The full:module:spring-boot-web-servertest suite (249 tests) passes, as docheckFormatandcheckstyle.