Files
opencloud/services/search/pkg/mapping/validate_test.go
T
Dominik Schmidt fc41995a47 fix(search): validate CaseInsensitive against the effective field type
The guard only rejected CaseInsensitive when a non-keyword/path Type was set explicitly. With no Type, isCasedType treated the field as cased, so CaseInsensitive on an inferred numeric/bool/datetime field passed validation but produced no _lowercase sibling, and the query would silently match nothing. Validate now falls back to the inferred Go type.
2026-08-31 13:40:42 +02:00

82 lines
2.2 KiB
Go

package mapping
import (
"reflect"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
type inner struct {
Artist string `json:"artist"`
}
type sample struct {
Name string `json:"Name"`
Audio *inner `json:"audio,omitempty"`
Location *struct { //nolint:unused
Lon float64 `json:"longitude"`
Lat float64 `json:"latitude"`
} `json:"location,omitempty"`
}
var _ = Describe("Validate", func() {
It("accepts known override keys", func() {
err := Validate(reflect.TypeFor[sample](), map[string]FieldOpts{
"Name": {},
"audio": {Type: TypeObject},
"audio.artist": {},
"location": {Type: TypeGeopoint},
})
Expect(err).ToNot(HaveOccurred())
})
It("rejects unknown override keys", func() {
err := Validate(reflect.TypeFor[sample](), map[string]FieldOpts{
"nope": {},
"audio.zzz": {},
})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("nope"))
Expect(err.Error()).To(ContainSubstring("audio.zzz"))
})
It("accepts empty overrides", func() {
Expect(Validate(reflect.TypeFor[sample](), nil)).To(Succeed())
})
It("rejects CaseInsensitive on a non-keyword/path field", func() {
True := true
err := Validate(reflect.TypeFor[sample](), map[string]FieldOpts{
"Name": {Type: TypeFulltext, CaseInsensitive: &True},
})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Name"))
Expect(err.Error()).To(ContainSubstring("CaseInsensitive"))
})
It("rejects CaseInsensitive on an inferred non-keyword field (empty Type)", func() {
True := true
type doc struct {
Size uint64 `json:"Size"`
}
err := Validate(reflect.TypeFor[doc](), map[string]FieldOpts{
"Size": {CaseInsensitive: &True}, // no explicit Type -> inferred numeric
})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Size"))
})
It("accepts CaseInsensitive on an inferred keyword field (empty Type)", func() {
True := true
type doc struct {
Name string `json:"Name"`
Tags []string `json:"Tags"`
}
Expect(Validate(reflect.TypeFor[doc](), map[string]FieldOpts{
"Name": {CaseInsensitive: &True},
"Tags": {CaseInsensitive: &True},
})).To(Succeed())
})
})