Skip to content

query: skip nil embedded struct pointers - #200

Open
0z5a wants to merge 1 commit into
google:masterfrom
0z5a:fix/skip-nil-embedded-struct-pointers
Open

0z5a wants to merge 1 commit into
google:masterfrom
0z5a:fix/skip-nil-embedded-struct-pointers

Conversation

@0z5a

@0z5a 0z5a commented Nov 14, 2026

Copy link
Copy Markdown

An anonymous field that is a nil pointer to a struct was encoded as an empty parameter named after the embedded type:

type ListOptions struct {
	Page int `url:"page,omitempty"`
}
type Options struct {
	*ListOptions
	State string `url:"state"`
}

query.Values(Options{State: "open"}) // ListOptions=&state=open

A nil embedded struct pointer has no fields to promote, so it now encodes nothing, the same as encoding/json does. With a non-nil &ListOptions{} the output was already state=open.

Only untagged nil pointers to structs are affected. Kept as before:

  • an embedded pointer named in its url tag (url:"inner"), which is still encoded as inner= when nil, like any other nil pointer field
  • embedded non-struct types (for example *MyString), which still use the type name as the key

Two side effects:

  • A nil embedded *time.Time used to produce Time= and now produces nothing. A non-nil embedded time.Time already produced nothing.
  • A nil embedded pointer to a struct type that implements Encoder no longer has EncodeValues called with the type name as key. Non-nil embedded structs are promoted and never went through Encoder, so now neither case calls it.

Tests: the new cases in TestValues_EmbeddedStructs fail on master (they get an extra Inner / N[Inner] key) and pass with this change. The named-pointer case passes in both. go test -race ./... passes with 100% coverage; go vet and gofmt are clean.

This change was drafted with Claude Code (claude-opus-5-5); the test results above are from the runs described.

An untagged anonymous field holding a nil pointer to a struct was
encoded as an empty parameter named after the embedded type, e.g.
ListOptions=&state=open instead of state=open. A nil embedded struct
pointer has no fields to promote, so encode nothing for it, as
encoding/json does.
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.

1 participant