fix: omitempty must not drop structs with non-zero unexported fields - #400
Open
sonnemusk wants to merge 1 commit into
Open
fix: omitempty must not drop structs with non-zero unexported fields#400sonnemusk wants to merge 1 commit into
sonnemusk wants to merge 1 commit into
Conversation
isEmptyValue for structs used exported fields only, so a value with non-zero unexported state was treated as empty and omitted. Use reflect.Value.IsZero so the check considers all fields. Fixes vmihailenco#378
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.
Problem
isEmptyValuedecided whether a struct taggedomitemptywas empty by scanning only exported fields (structs.Fields+OmitEmpty). A struct with zero-valued exported fields but non-zero unexported fields was incorrectly treated as empty and omitted, even though it is not the zero value of its type.Minimal reproduction (#378)
Fix
For
reflect.Struct, usereflect.Value.IsZero, which checks all fields (exported and unexported). A struct is omitted only when it equals the zero value of its type.Zero structs continue to be omitted; non-zero unexported state no longer causes data loss for exported fields or custom encoders.
Tests
omitemptyunexpectedly omits a member when the member is a struct with a non-zero unexported member #378All existing tests pass, including
ExampleMarshal_ignore_simple_zero_structs_when_tagged_with_omitempty.Fixes #378
Note on #390
#390 discusses broader omitempty vs
encoding/jsonsemantics (interface/anyemptiness, futureomitzero). This PR only fixes the unexported-field empty check from #378 and does not change interface/anyomitempty behavior.