Fix panic with DisallowUnknownField and inlined maps#849
Fix panic with DisallowUnknownField and inlined maps#849vnykmshr wants to merge 1 commit intogoccy:masterfrom
Conversation
deleteStructKeys assumed the type of an inlined field is always a struct, calling NumField on it unconditionally. When the inlined field is a map (e.g. map[string]string), this panics. An inlined map accepts arbitrary keys by definition, so when deleteStructKeys encounters a map type it clears the unknown fields map and returns early. Fixes goccy#810
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #849 +/- ##
=======================================
Coverage 80.48% 80.49%
=======================================
Files 22 22
Lines 6846 6849 +3
=======================================
+ Hits 5510 5513 +3
Misses 914 914
Partials 422 422 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR fixes a panic that occurred when using DisallowUnknownField (or Strict()) with structs containing inline maps. The panic was caused by deleteStructKeys calling reflect.Type.NumField() on map types, which is invalid since only struct types have numbered fields.
Changes:
- Added early return in
deleteStructKeysto detect inline map types and clear unknown fields - Added two comprehensive test cases covering flat and nested struct scenarios with inline maps
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| decode.go | Added map type detection before structFieldMap call to prevent panic and correctly handle inline maps by clearing all unknown fields |
| decode_test.go | Added two test cases: one for flat struct with inline map, one for nested struct with inline map (matching the reporter's use case) |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Hi @goccy, just checking if this needs any changes. Happy to adjust if needed. |
Summary
deleteStructKeyscalledNumFieldon inlined map types, panicking. An inlined map accepts arbitrary keys, so the fix clears the unknown fields map and returns early.clear()builtin.Test plan
yaml:",inline"map +DisallowUnknownFieldDisallowUnknownFieldsubtests still passgo test ./...)Fixes #810