fix: prevent panic in uniqueItems validation for unhashable types (#1042)#1045
Open
ArdyJunata wants to merge 1 commit into
Open
fix: prevent panic in uniqueItems validation for unhashable types (#1042)#1045ArdyJunata wants to merge 1 commit into
ArdyJunata wants to merge 1 commit into
Conversation
Fixes danielgtaylor#1042 The uniqueItems check in handleArray used map[any]struct{} as a hash set. When array items contain non-hashable types such as map[string]interface{} (produced by malformed JSON like [{"k":"v"}] for a []string field), inserting into the map panics with: http: panic serving ...: hash of unhashable type: map[string]interface {} This caused the server to return no response at all — not even a 500. Fix: introduce a uniqueItemsContains helper that attempts the O(1) map lookup on the fast path. A deferred recover() catches the panic for unhashable types and falls back to an O(n) reflect.DeepEqual linear scan, which: - Never panics regardless of element type - Still detects duplicate unhashable values correctly - Has no impact on the common case (hashable types hit the fast path) Malformed input like [{"k":"v"}] for a []string field now correctly returns a 422 'expected string' validation error instead of crashing. Regression tests added in validate_test.go: - uniqueItems with unhashable object element must not panic - uniqueItems detects duplicate unhashable objects - uniqueItems with mixed hashable and unhashable elements must not panic
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1045 +/- ##
==========================================
+ Coverage 93.14% 93.16% +0.01%
==========================================
Files 23 23
Lines 4901 4913 +12
==========================================
+ Hits 4565 4577 +12
Misses 272 272
Partials 64 64 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1042
Problem
The
uniqueItemscheck inhandleArrayusedmap[any]struct{}as a hash set. When array items contain non-hashable types such asmap[string]interface{}— produced by malformed JSON like[{}]for a[]stringfield — inserting into the map panics:This caused the server to return no response at all instead of a
422 Unprocessable Entity. The panic kills the goroutine serving the request.Reproduction:
Fix
Introduce a
uniqueItemsContainshelper that:map[any]lookup (works for all hashable types — strings, ints, bools, etc.)deferred recover()catches the panic for unhashable types and falls back to an O(n)reflect.DeepEquallinear scanThis means:
[{}]for[]string→ proper422 expected stringerrorTests
Three regression tests added to
validate_test.go:uniqueItems with unhashable object element must not panic[{}]for[]stringreturns422, not a panicuniqueItems detects duplicate unhashable objects[{"a":1},{"a":1}]is still detected as non-uniqueuniqueItems with mixed hashable and unhashable elements["ok", {"k":"v"}]does not panicAll existing tests continue to pass.