Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: encoding/json: fix omitempty tag behavior once and for all #58738

Closed
ivanjaros opened this issue Feb 26, 2023 · 3 comments
Closed

proposal: encoding/json: fix omitempty tag behavior once and for all #58738

ivanjaros opened this issue Feb 26, 2023 · 3 comments

Comments

@ivanjaros
Copy link

ivanjaros commented Feb 26, 2023

Presently, the omitempty tag is documented in the json library as:

// The "omitempty" option specifies that the field should be omitted
// from the encoding if the field has an empty value, defined as
// false, 0, a nil pointer, a nil interface value, and any empty array,
// slice, map, or string.

The problem with this behavior is that default value of a map, slice and array are not empty values but nil.
Since null is a valid json value, this was not enforced because of some go-centric design and this decision caused actually empty values to be discarded and with it a viable information. For example, when client sends PATCH request, there is a difference between no value present - at all, null and [] or {}. All of these situations are perfectly valid cases and when omitempty drops all values, it forces the server to treat them like no value present, which is blatantly wrong behavior. I think this might be related to google's days of go development and google's strong tie to grpc and protocol buffers, which nowadays forces use of field mask to detect value not present, nil or empty. But json is not grpc nor protocol buffers so this sohuldn't have been here in the first place.

Obviously I have no way of knowing how the decision to force this behavior came to be but I think it is time to fix this "discardation" of actual tangible, useful, and most importantly purposeful, information.

PS: NULL has been invented specifically to signal that something exists(a field in an object, a column in a database) but has no value. Hence, empty map, slice, array simply CANNOT be under any circumstances treated the same way as NULL. It's just plain wrong. There's no discussion to be had, this is wrong from the get go.

@gopherbot gopherbot added this to the Proposal milestone Feb 26, 2023
@seankhliao
Copy link
Member

Duplicate of #44023

@seankhliao seankhliao marked this as a duplicate of #44023 Feb 26, 2023
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Feb 26, 2023
@ivanjaros
Copy link
Author

ivanjaros commented Feb 26, 2023

The original issue has this one quote: "I think the behavior you observe is correct, as per the docs"

That's really not how to handle a BUG. It's like government investigating itself, telling people it found no wrongdoing.

Additionally, that issues is locked so WTF are you closing this one?

@seankhliao
Copy link
Member

Remember that omitempty only applies to marshaling, not unmarshaling.

changing that is a serious breaking change, not a bug fix

The go standard library is generally designed not to distinguish between zero and nil slices, that extends here as well.

@golang golang locked and limited conversation to collaborators Feb 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants