Conversation
`Source.MarshalJSON` re-adds `granted_actions` to the encoded body when it
is non-nil, because `omitempty` would otherwise drop a non-nil but empty
slice. The four `map[string]any` fields beside it — `capabilities`,
`external_connection`, `bank_account` and `card` — carry the same
`omitempty` tag and got no such treatment, so an empty object the API sent
disappears on re-encode.
The effect is backwards from what the type documents. `AdditionalFields`
preserves unknown properties verbatim, so an empty object Link adds in
future survives a round trip, while an empty object for a field the SDK
already models does not:
in {"id":"src_1","capabilities":{}} out {"id":"src_1"}
in {"id":"src_1","unknown_future_field":{}} out {"id":"src_1","unknown_future_field":{}}
That also erases the distinction between "the source has no capabilities"
and "the API did not report capabilities", which is the distinction the
explicit-empty handling exists to keep.
Extend the same non-nil check to the four map fields. `Balance` and the
page types are unaffected: their remaining optional fields are pointers,
where `omitempty` already only drops nil.
Testing
`TestSourcePreservesExplicitEmptyResponseObjects` fails on the parent
commit, reporting all four fields dropped, and passes with this change.
`granted_actions` and an unknown field pass either way, so the test does
not simply restate the fix.
- `go test ./...`
- `go vet ./...`
- `gofmt -l .`
|
|
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.
Motivation
omitemptydrops a non-nil but empty map or slice.Source.MarshalJSONalready works around that forgranted_actions, butcapabilities,external_connection,bank_accountandcardcarry the same tag and were missed, so an empty object the API sent disappears on re-encode.Unknown properties survive through
AdditionalFields, so the fields the SDK models are currently the ones that get lost:This also erases the difference between a source having no capabilities and the API not reporting them.
Summary
granted_actionshandlingKey design considerations
Balanceand the page types are unaffected; their optional fields are pointers, whereomitemptyonly drops nilpackages/sdk-gohas nopackage.json. CHANGELOG entry follows Surface SpendRequest idempotency key #317