Skip to content

Fix panic with DisallowUnknownField and inlined maps#849

Open
vnykmshr wants to merge 1 commit intogoccy:masterfrom
vnykmshr:fix/strict-inline-map-panic
Open

Fix panic with DisallowUnknownField and inlined maps#849
vnykmshr wants to merge 1 commit intogoccy:masterfrom
vnykmshr:fix/strict-inline-map-panic

Conversation

@vnykmshr
Copy link
Copy Markdown
Contributor

Summary

  • deleteStructKeys called NumField on inlined map types, panicking. An inlined map accepts arbitrary keys, so the fix clears the unknown fields map and returns early.
  • Uses Go 1.21 clear() builtin.

Test plan

  • New subtest: flat struct with yaml:",inline" map + DisallowUnknownField
  • New subtest: nested struct with inlined map (reporter's use case)
  • Existing DisallowUnknownField subtests still pass
  • Full test suite passes (go test ./...)

Fixes #810

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-commenter
Copy link
Copy Markdown

codecov-commenter commented Feb 16, 2026

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.49%. Comparing base (92bc79c) to head (93dd380).
⚠️ Report is 4 commits behind head on master.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

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:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown

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

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 deleteStructKeys to 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.

@vnykmshr
Copy link
Copy Markdown
Contributor Author

Hi @goccy, just checking if this needs any changes. Happy to adjust if needed.

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.

Panic with yaml.Strict() and an inlined map

3 participants