Skip to content

Commit b00575f

Browse files
authored
[cherry-pick] update the orm filter func (#22209)
update the orm filter func to extend the enhancement from #21924 to fuzzy and range match. After the enhance, the orm.ExerSep is not supported in any sort of query keywords. Signed-off-by: wy65701436 <[email protected]>
1 parent 8596b09 commit b00575f

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

src/lib/orm/query.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,17 +161,36 @@ func setFilters(ctx context.Context, qs orm.QuerySeter, query *q.Query, meta *me
161161
log.Warningf("The separator '%s' is not valid in the query parameter '%s__%s'. Please use the correct field name.", orm.ExprSep, keyPieces[0], keyPieces[1])
162162
continue
163163
}
164-
k := keyPieces[0]
165-
mk, filterable := meta.Filterable(k)
164+
fieldKey := keyPieces[0]
165+
mk, filterable := meta.Filterable(fieldKey)
166166
if !filterable {
167167
// This is a workaround for the unsuitable usage of query, the keyword format for field and method should be consistent
168168
// e.g. "ArtifactDigest" or the snake case format "artifact_digest" should be used instead:
169169
// https://github.com/goharbor/harbor/blob/v2.2.0/src/controller/blob/controller.go#L233
170-
mk, filterable = meta.Filterable(snakeCase(k))
170+
mk, filterable = meta.Filterable(snakeCase(fieldKey))
171171
if mk == nil || !filterable {
172172
continue
173173
}
174174
}
175+
176+
// only accept the below operators
177+
if len(keyPieces) == 2 {
178+
operator := orm.ExprSep + keyPieces[1]
179+
allowedOperators := map[string]struct{}{
180+
"__icontains": {},
181+
"__in": {},
182+
"__gte": {},
183+
"__lte": {},
184+
"__gt": {},
185+
"__lt": {},
186+
"__exact": {},
187+
}
188+
if _, ok := allowedOperators[operator]; !ok {
189+
log.Warningf("the operator '%s' in query parameter '%s' is not supported, the query will be skipped.", operator, key)
190+
continue
191+
}
192+
}
193+
175194
// filter function defined, use it directly
176195
if mk.FilterFunc != nil {
177196
qs = mk.FilterFunc(ctx, qs, key, value)

0 commit comments

Comments
 (0)