mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-12-05 05:43:36 -05:00
feat(graph): accept sortProperties on search requests
Validated against the sortable-field whitelist (name, size, lastModifiedDateTime, photo.takenDateTime) and forwarded to the search service as order_by. Also fixes the stub search service's IndexSpace signature (streaming response) so the suite builds again. (cherry picked from commit 9aa6a3492de3468da7a8480a3b9ef5748cee1f5d) (cherry picked from commit 35a6cad8ea6a7890bdc0bc98c7ac97bdcbf477cc)
This commit is contained in:
1 parent
01e4c21b29
commit
7982c06794
2 files changed
+59
-23
No files matched your search
@@ -46,10 +46,6 @@ func (g Graph) SearchQuery(w http.ResponseWriter, r *http.Request) {
|
||||
// every request is validated before the first one runs
|
||||
searches := make([]*searchsvc.SearchRequest, 0, len(req.Requests))
|
||||
for _, sr := range req.Requests {
|
||||
if property := unsupportedProperty(sr); property != "" {
|
||||
errorcode.NotSupported.Render(w, r, http.StatusNotImplemented, property+" is not supported yet")
|
||||
return
|
||||
}
|
||||
prepared, err := searchRequestOf(sr)
|
||||
if err != nil {
|
||||
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, err.Error())
|
||||
@@ -89,15 +85,6 @@ func validateSearchExpand(r *http.Request) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// unsupportedProperty names a property of the spec this endpoint does not
|
||||
// evaluate yet.
|
||||
func unsupportedProperty(sr libregraph.SearchRequest) string {
|
||||
if len(sr.SortProperties) > 0 {
|
||||
return "sortProperties"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// searchRequestOf validates one request against the spec and the index rules
|
||||
// (the search service checks them again) and translates it. Fields are
|
||||
// validated under the request's names so an error reads like the request.
|
||||
@@ -111,6 +98,9 @@ func searchRequestOf(sr libregraph.SearchRequest) (*searchsvc.SearchRequest, err
|
||||
if err := validateAggregations(sr.Aggregations); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := validateSortProperties(sr.SortProperties); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
aggregations := libregraphAggregationsToSearch(sr.Aggregations)
|
||||
filters, err := aggregationFiltersToSearch(sr.AggregationFilters)
|
||||
if err != nil {
|
||||
@@ -135,6 +125,7 @@ func searchRequestOf(sr libregraph.SearchRequest) (*searchsvc.SearchRequest, err
|
||||
PageSize: &size,
|
||||
Aggregations: aggregations,
|
||||
AggregationFilters: filters,
|
||||
OrderBy: libregraphSortToSearch(sr.SortProperties),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -260,6 +251,43 @@ func validateAggregations(aggs []libregraph.AggregationOption) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// sortableFields is the set of fields accepted in sortProperties (mirrored in
|
||||
// the openapi spec). A field qualifies only if it is indexed as a sortable
|
||||
// type in both search backends AND carried on the Match entity: the service
|
||||
// layer re-sorts matches when merging the per-space result streams and needs
|
||||
// the sort key on the match itself.
|
||||
var sortableFields = map[string]struct{}{
|
||||
"name": {},
|
||||
"size": {},
|
||||
"lastModifiedDateTime": {},
|
||||
"photo.takenDateTime": {},
|
||||
}
|
||||
|
||||
// validateSortProperties rejects sorting by fields outside sortableFields.
|
||||
func validateSortProperties(sortProperties []libregraph.SortProperty) error {
|
||||
for _, sp := range sortProperties {
|
||||
if _, ok := sortableFields[sp.Name]; !ok {
|
||||
return fmt.Errorf("field %q is not sortable; sortable fields: name, size, lastModifiedDateTime, photo.takenDateTime", sp.Name)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func libregraphSortToSearch(in []libregraph.SortProperty) []*searchsvc.SortProperty {
|
||||
if len(in) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make([]*searchsvc.SortProperty, 0, len(in))
|
||||
for _, sp := range in {
|
||||
p := &searchsvc.SortProperty{Name: sp.Name}
|
||||
if sp.IsDescending != nil {
|
||||
p.IsDescending = *sp.IsDescending
|
||||
}
|
||||
out = append(out, p)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// renderSearchError answers with the status the search service failed with.
|
||||
func (g Graph) renderSearchError(w http.ResponseWriter, r *http.Request, err error) {
|
||||
e := merrors.Parse(err.Error())
|
||||
|
||||
@@ -440,16 +440,6 @@ var _ = ginkgo.Describe("SearchQuery", func() {
|
||||
Expect(aggs[1].Metric.Value).To(BeNil(), "no value, not zero")
|
||||
})
|
||||
|
||||
ginkgo.DescribeTable("answers a property it does not evaluate with 501 instead of ignoring it",
|
||||
func(fragment, property string) {
|
||||
rr := postSearchQuery(graphWithoutSearch(), searchQueryBody(fragment))
|
||||
Expect(rr.Code).To(Equal(http.StatusNotImplemented), rr.Body.String())
|
||||
Expect(rr.Body.String()).To(ContainSubstring("notSupported"))
|
||||
Expect(rr.Body.String()).To(ContainSubstring(property))
|
||||
},
|
||||
ginkgo.Entry("sortProperties", `"sortProperties": [{"name": "name"}]`, "sortProperties"),
|
||||
)
|
||||
|
||||
ginkgo.It("rejects more than 5 requests with 400", func() {
|
||||
one := `{"entityTypes": ["driveItem"], "query": {"queryString": "a"}}`
|
||||
body := `{"requests": [` + strings.Repeat(one+",", 5) + one + `]}`
|
||||
@@ -482,6 +472,24 @@ var _ = ginkgo.Describe("SearchQuery", func() {
|
||||
Expect(captured().GetAggregationFilters()[0].GetTerms()).To(Equal([]string{"u4pru"}))
|
||||
})
|
||||
|
||||
ginkgo.It("forwards sortProperties to the search service as order_by", func() {
|
||||
g, captured := graphWithSearchAnswer(&searchsvc.SearchResponse{})
|
||||
rr := postSearchQuery(g, searchQueryBody(`"sortProperties": [{"name": "photo.takenDateTime", "isDescending": true}, {"name": "name"}]`))
|
||||
Expect(rr.Code).To(Equal(http.StatusOK), rr.Body.String())
|
||||
|
||||
Expect(captured().GetOrderBy()).To(HaveLen(2))
|
||||
Expect(captured().GetOrderBy()[0].GetName()).To(Equal("photo.takenDateTime"))
|
||||
Expect(captured().GetOrderBy()[0].GetIsDescending()).To(BeTrue())
|
||||
Expect(captured().GetOrderBy()[1].GetName()).To(Equal("name"))
|
||||
Expect(captured().GetOrderBy()[1].GetIsDescending()).To(BeFalse())
|
||||
})
|
||||
|
||||
ginkgo.It("rejects sorting by an unsupported field with 400", func() {
|
||||
rr := postSearchQuery(graphWithoutSearch(), searchQueryBody(`"sortProperties": [{"name": "photo.iso"}]`))
|
||||
Expect(rr.Code).To(Equal(http.StatusBadRequest), rr.Body.String())
|
||||
Expect(rr.Body.String()).To(ContainSubstring("photo.iso"))
|
||||
})
|
||||
|
||||
ginkgo.It("rejects an $expand it does not know with 400", func() {
|
||||
req := httptest.NewRequest(http.MethodPost, "/search/query?$expand=thumbnails,permissions", bytes.NewBufferString(searchQueryBody(`"from": 0`)))
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
Reference in new issue
Block a user