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

Fix "bug" in the thrift decoder. #8

Merged
merged 1 commit into from
Feb 16, 2024

Conversation

ugodiggi
Copy link

@ugodiggi ugodiggi commented Feb 16, 2024

Empty lists that are actually present in the protocol should be decoded as [], not as nil.

This typically does not matter because the accessor methods on a struct work the same on a nil slice an empty slice.
But it does matter if we re-serialize the struct using some different method, e.g. and most notably the go json package.

In that case, nil values would be presented as null rather than [] in the serialized json. The generated struct includes annotations on the fields (omitempty) that make so that this does not happen in most common cases (i.e. struct fields that are empty lists). Unfortunately it is not-too-hard to devise examples where these null values actually make it to the serialized json (e.g. using a list<list>; if you were to decode [[1,2], []] this would later be encoded to json as [[1,2], null]).

Lists that are actually present in the protocol should be decoded as [],
not as nil.

This typically does not matter because the accessor methods on a struct
work the same on a nil slice an empty slice.
But it does matter if we re-serialize the struct using some different
method, e.g. and most notably the go json package.

In that case, nil values would be presented as `null` rather than `[]`
in the serialized json. The generated struct includes annotations on the
fields (`omitempty`) that make so that this does not happen in most
common cases (i.e. struct fields that are empty lists).
Unfortunately it is not-too-hard to devise examples where these `null`
values actually make it to the serialized json (e.g. using a
list<list<int>>; if you were to decode [[1,2], []] this would later be
encoded to json as [[1,2], null]).
@cwaeland cwaeland merged commit ce583bf into alecthomas:master Feb 16, 2024
@ugodiggi ugodiggi deleted the ugo/decoder-fix branch February 16, 2024 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants