Fix #6065: APPLY_JSON_INCLUDE_FOR_CONTAINERS does not fully remove empty collection#6070
Open
seonwooj0810 wants to merge 1 commit into
Open
Conversation
…remove empty collection Content-level `@JsonInclude` filtering (added for containers in FasterXML#5369) suppresses empty/null elements at serialization time, but the collection serializers' `isEmpty()` only checked the raw `Collection.isEmpty()`. So a collection that becomes empty *after* content filtering (e.g. a `List` holding only nulls under `content = NON_EMPTY`) was still emitted as `[]` and not dropped by value-level `NON_EMPTY`, whereas the equivalent `Map` was correctly dropped (FasterXML#1649). Make `isEmpty()` content-aware for `CollectionSerializer`, `IndexedListSerializer` and `StaticListSerializerBase`, mirroring `MapSerializer`: when `APPLY_JSON_INCLUDE_FOR_CONTAINERS` is enabled and content suppression is configured, a container whose every element would be suppressed is reported as empty. Gated on `_needToCheckFiltering`, so default behavior is unchanged.
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.
Fixes #6065
Root cause
Content-level
@JsonIncludefiltering for containers (added in #5369) suppresses empty/null elements during serialization, but the collection serializers'isEmpty()only consulted the rawCollection.isEmpty(). As a result a collection that is non-empty by size but becomes empty once content filtering is applied — e.g. aListholding onlynulls (or only emptyStrings) undercontent = NON_EMPTY— was still emitted as[]and therefore not dropped by the enclosing property's value-levelNON_EMPTY. The equivalentMapwas already dropped becauseMapSerializer.isEmpty()recurses into its values (#1649), producing the inconsistency reported here.Change
Make
isEmpty()content-aware for the collection serializers, mirroringMapSerializer:CollectionSerializer,IndexedListSerializer— reuse a new shared helperAsArraySerializerBase#_allElementsSuppressed(...)that walks elements and reports the container empty when every element would be suppressed (reusing the existing dynamic element-serializer resolution).StaticListSerializerBase(String collections) — equivalent inline check for the natural-type element path.The new logic is gated on the existing
_needToCheckFiltering(ctxt)guard (APPLY_JSON_INCLUDE_FOR_CONTAINERSenabled and content suppression configured), so behavior with the feature disabled — the default — is unchanged.Verification
Added
JsonIncludeContainerEmpty6065Test(the reporter's cases plus object lists, sets, retained-content and feature-disabled guards). The 4 content-empty assertions fail on3.xbefore the change and pass after; the retained-content / feature-off assertions pass both ways. ExistingJsonIncludeCollectionTest,JsonIncludeArrayTest,MapInclusionTestand the fullser.filtersuite (133 tests) remain green.Verification done: (1) searched open PRs/timeline — no in-flight PR; (2) no self-claim in thread; (3) fix is in
.javaserializers; (4) reproduced the bug on latestupstream/3.xwith a failing test; (5) confirmed the value/content-inclusion mechanism vs. the Map path (#1649/#5369).