Skip to content

fix(types): encode nil slices as empty arrays in Allow type#538

Open
Kubudak90 wants to merge 1 commit into
coinbase:masterfrom
Kubudak90:fix-nil-slice-marshaling
Open

fix(types): encode nil slices as empty arrays in Allow type#538
Kubudak90 wants to merge 1 commit into
coinbase:masterfrom
Kubudak90:fix-nil-slice-marshaling

Conversation

@Kubudak90

Copy link
Copy Markdown

Fixes #62

Problem

By default, Go's json package marshals nil slices as null, whereas empty slices are marshaled as []. This causes issues for clients in other languages (e.g., JavaScript) that may choke on null when expecting an array.

Solution

Added a custom MarshalJSON method to the Allow type that converts nil slices to empty slices before marshaling. This ensures consistent encoding as [] regardless of whether the slice is nil or empty.

Changes

  • Added MarshalJSON method to Allow type
  • Nil slices for OperationStatuses, OperationTypes, Errors, CallMethods, and BalanceExemptions are now encoded as [] instead of null
  • Added test file nil_slice_test.go with tests for nil and empty slice marshaling

Testing

The fix ensures:

  • nil slices → [] in JSON
  • Empty slices → [] in JSON (unchanged behavior)
  • Non-empty slices → original values in JSON (unchanged behavior)

This is a backward-compatible change that only affects the JSON representation of nil slices, making the API more robust for cross-language compatibility.

Ensures nil slices are marshaled as [] instead of null for better
cross-language compatibility. JavaScript and other languages may
choke on null when expecting an array.

Fixes coinbase#62
@cb-heimdall

Copy link
Copy Markdown

🟡 Heimdall Review Status

Requirement Status More Info
Reviews 🟡 0/1
Denominator calculation
Show calculation
1 if user is bot 0
1 if user is external 0
2 if repo is sensitive 0
From .codeflow.yml 1
Additional review requirements
Show calculation
Max 0
0
From CODEOWNERS 0
Global minimum 0
Max 1
1
1 if commit is unverified 1
Sum 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Encode nil slices as empty arrays

2 participants