Skip to content

Preserve empty Go slices and maps in JSON#1528

Merged
stephentoub merged 1 commit into
github:mainfrom
qmuntal:go-omitzero-json-tags
Jun 1, 2026
Merged

Preserve empty Go slices and maps in JSON#1528
stephentoub merged 1 commit into
github:mainfrom
qmuntal:go-omitzero-json-tags

Conversation

@qmuntal
Copy link
Copy Markdown
Contributor

@qmuntal qmuntal commented Jun 1, 2026

Summary

Use Go's json:",omitzero" tag for optional generated slices/maps, and for hand-authored public fields where an empty-but-present value has distinct wire semantics.

This preserves the difference between:

nil        // omitted from JSON; use runtime/default/absent behavior
[]T{}      // emitted as []; explicit empty list
map[...]{} // emitted as {}; explicit empty object

Why

omitempty collapses nil and empty slices/maps into the same JSON shape by omitting both. That loses information for APIs where "not provided" and "provided but empty" mean different things. omitzero keeps nil as absent while allowing callers to intentionally send empty arrays/objects.

Concrete examples:

  • CustomAgentConfig.Tools
    • omitted: inherit/default tool behavior
    • []: explicitly expose no tools
  • Tool.Parameters
    • omitted: no parameter schema supplied
    • {}: an explicit JSON Schema object that accepts any parameters
  • CanvasDeclaration.InputSchema
    • omitted: no canvas input schema declared
    • {}: explicit schema accepting any canvas input
  • ElicitationResult.Content
    • omitted: no form values, e.g. cancel/decline paths
    • {}: accepted form with no fields/values

The generated Go code now applies this consistently to optional slices/maps, while scalar/pointer optionals continue to use omitempty.

Testing

  • cd scripts/codegen && npm run generate:go
  • cd go && go test
  • cd nodejs && npm test -- session-event-codegen.test.ts
  • Focused Go serialization tests for nil vs empty CustomAgentConfig, Tool, CanvasDeclaration, and ElicitationResult values

Copilot AI review requested due to automatic review settings June 1, 2026 14:19
@qmuntal qmuntal requested a review from a team as a code owner June 1, 2026 14:19
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Updates Go JSON serialization semantics to distinguish between nil and empty values for slice/map fields by emitting omitzero tags (and generating them via codegen), with tests validating the behavior.

Changes:

  • Add Go codegen helpers to generate json tags using omitzero for optional slice/map fields.
  • Update Go struct tags for slice/map fields to use ,omitzero (omit only when nil/zero).
  • Add Go tests covering nil vs empty serialization for affected fields.
Show a summary per file
File Description
scripts/codegen/go.ts Centralizes JSON tag generation and switches optional slice/map fields to omitzero semantics.
go/types.go Updates struct tags (and comments) to preserve empty slice/map values while omitting nils.
go/canvas.go Updates InputSchema tag to ,omitzero for nil vs empty behavior.
go/types_test.go Adds tests asserting nil fields are omitted while empty slice/map fields are included.

Copilot's findings

Files not reviewed (4)
  • go/rpc/zrpc.go: Language not supported
  • go/rpc/zrpc_encoding.go: Language not supported
  • go/rpc/zsession_encoding.go: Language not supported
  • go/rpc/zsession_events.go: Language not supported
  • Files reviewed: 4/8 changed files
  • Comments generated: 5

Comment thread go/types.go
Comment thread go/types.go
Comment thread go/types.go
Comment thread scripts/codegen/go.ts
Comment thread scripts/codegen/go.ts
@stephentoub stephentoub added this pull request to the merge queue Jun 1, 2026
Merged via the queue into github:main with commit 5995295 Jun 1, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants