You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.
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.
The text was updated successfully, but these errors were encountered:
Presently, the
omitempty
tag is documented in the json library as: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.
The text was updated successfully, but these errors were encountered: