Repository navigation
fix: prevent unique from panicking on unhashable elements - #1700
tony1233200 wants to merge 2 commits into
Conversation
Fixes go-playground#1674. isUnique built seen as map[interface{}]struct{} and inserted elem.Interface() directly, so slices of slices, maps, []byte, or structs containing a slice panicked with "hash of unhashable type". Track seen values with a small helper: comparable values keep using the map, non-comparable values are compared with reflect.DeepEqual. Duplicate detection is unchanged for hashable elements, and unhashable duplicates are now detected instead of panicking. Tests: added TestUniqueValidationUnhashableElements covering [][]string, []struct-with-slice, [][]byte, []map, unique=Field on a slice field, and maps with unhashable values (distinct passes, duplicate fails in each case). Co-Authored-By: Muse Spark <muse-spark@meta.com>
| // check reports whether key was already recorded; if not, it records | ||
| // it and reports false. | ||
| func (t *uniqueTracker) check(key interface{}) bool { | ||
| if key == nil || reflect.TypeOf(key).Comparable() { |
There was a problem hiding this comment.
[AI] reflect.Type.Comparable() is true for a struct or array type that has an interface field, but hashing such a value still panics when the interface holds a slice or map. So the panic stays for this input (checked on this branch):
type in struct{ V any }
validator.New().Var([]in{{V: []int{1}}, {V: []int{2}}}, "unique")
// panic: hash of unhashable type: []intThe same happens for []any{in{...}}, [][1]any{...} and map[string]in. reflect.Value.Comparable looks at the value held by the interface, so this is enough:
if key == nil || reflect.ValueOf(key).Comparable() {With that change the inputs above no longer panic, duplicates are still found, and go test ./... passes. A test case with an interface field would cover it.
There was a problem hiding this comment.
Fixed: uniqueTracker.check now tests reflect.ValueOf(key).Comparable() instead of reflect.TypeOf(key).Comparable(), so a struct or array with an interface field holding a slice or map takes the DeepEqual path instead of panicking on hash. Regression cases added to TestUniqueValidationUnhashableElements cover your examples — []WithIface{{V: []int{1}}, {V: []int{2}}} (distinct passes, duplicate fails), []any{...}, [][1]any{...} and map[string]WithIface — and go test ./... passes for all 26 packages.
reflect.Type.Comparable is true for a struct or array type with an interface field, but hashing such a value still panics when the interface holds a slice or map. Check reflect.Value.Comparable instead, so those elements take the DeepEqual path like any other unhashable value. Tests: TestUniqueValidationUnhashableElements gains cases with an interface field holding a slice ([]T, []any, [][1]any, map values), distinct passes and duplicates fail. go test ./... passes. Co-Authored-By: Muse Spark <muse-spark@meta.com>
Fixes Or Enhances
Fixes #1674.
uniquepanicked withhash of unhashable typeon slices or maps whose elements cannot be used as map keys —[][]string,[]map[string]string,[][]byte, or a[]MyStructwhere the struct contains a slice field.isUniquebuiltseen := make(map[interface{}]struct{})and insertedelem.Interface()directly, so any such input killed the call instead of producing aValidationErrors.Change (in
isUnique,baked_in.go): track seen values with a smalluniqueTrackerhelper. Values whose dynamic type is comparable keep using the map exactly as before; values whose type is not comparable (reflect.Type.Comparable()is false) are kept in a list and compared withreflect.DeepEqual. The same helper covers all three sites that used the map:uniqueon slices/arrays,unique=Field(where the compared field itself may be unhashable, e.g.unique=Tagson a[]stringfield), anduniqueon maps.Behavior (verified with the issue's example program):
[][]string{{"a"}, {"b"}}withuniquenil(valid)[]WithSlice(struct with a slice field) withuniquenil(valid)[][]byte{{1}, {2}}withuniquenil(valid)[]map[string]stringwithuniquenil(valid)uniquevalidation errorNo behavior change for hashable elements: they still go through the map, so duplicate detection and performance there are unchanged. Unhashable duplicates are now detected instead of panicking.
Tests: added
TestUniqueValidationUnhashableElementscovering distinct-vs-duplicate for each of the four shapes above, plusunique=Fieldon a slice field and maps with unhashable values.go test ./...passes for all 26 packages,go vetandgofmtare clean. (gofumptflags a few pre-existing spots elsewhere in the repo, includingtranslations/ko/ko_test.go; none of them are in the changed lines.)Disclosure (per CONTRIBUTING): this patch was prepared with the help of an AI coding assistant (Muse Spark) and reviewed/tested by me; the commit carries the requested
Co-Authored-Bytrailer.Make sure that you've checked the boxes below before you submit PR:
@go-playground/validator-maintainers