Skip to content

query: skip nil encoder interface fields instead of panicking - #197

Draft
rootkiller6788 wants to merge 2 commits into
google:masterfrom
rootkiller6788:fix-nil-encoder-interface-panic
Draft

rootkiller6788 wants to merge 2 commits into
google:masterfrom
rootkiller6788:fix-nil-encoder-interface-panic

Conversation

@rootkiller6788

Copy link
Copy Markdown

When a struct has a field of an interface type that implements Encoder, and that field is left nil, Values() crashes:

panic: interface conversion: interface is nil, not query.Encoder

That panic happens because the interface unwrap added for non-nil interfaces doesn't fire for a nil one, so we go straight into the Encoder block and call EncodeValues on a nil interface. There's no concrete encoder to call, so I skip the field the same way we skip other empty values. Non-nil values still get encoded normally.

Repro:

type myEncoder interface { query.Encoder }
type st struct { V myEncoder }
query.Values(st{}) // panics before, empty result after

Added a regression test in query/encode_test.go.

A struct field with an interface type that implements Encoder, left nil,
falls through the interface unwrap (which only fires for non-nil values)
and hits sv.Interface().(Encoder) on a nil interface, which panics with
"interface conversion: interface is nil, not query.Encoder". Treat a nil
encoder interface like any other empty value and skip the field.
Values() on a struct with a nil interface field that implements Encoder
used to panic; the test locks in the skip behavior and confirms non-nil
values still go through their custom encoder.
@willnorris
willnorris self-requested a review October 19, 2026 21:26
@willnorris

Copy link
Copy Markdown
Collaborator

(re-requested my own review, since I see it's still in draft. not sure if you were planning on doing more)

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.

2 participants