Properly parse order direction (#2812)

This commit is contained in:
Tomas Touceda 2021-11-05 13:36:05 -03:00 committed by GitHub
parent 7a22e71c69
commit 586c2f9ead
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 1 deletions

View File

@ -171,6 +171,28 @@ func makeDecoder(iface interface{}) kithttp.DecodeRequestFunc {
field.SetUint(uint64(queryValUint))
case reflect.Bool:
field.SetBool(queryVal == "1" || queryVal == "true")
case reflect.Int:
queryValInt := 0
switch queryTagValue {
case "order_direction":
switch queryVal {
case "desc":
queryValInt = int(fleet.OrderDescending)
case "asc":
queryValInt = int(fleet.OrderAscending)
case "":
queryValInt = int(fleet.OrderAscending)
default:
return fleet.ListOptions{},
errors.New("unknown order_direction: " + queryVal)
}
default:
queryValInt, err = strconv.Atoi(queryVal)
if err != nil {
return nil, errors.Wrap(err, "parsing uint from query")
}
}
field.SetInt(int64(queryValInt))
default:
return nil, errors.Errorf("Cant handle type for field %s %s", f.Name, field.Kind())
}

View File

@ -333,7 +333,7 @@ func (s *integrationTestSuite) TestVulnerableSoftware() {
lsReq := listSoftwareRequest{}
lsResp := listSoftwareResponse{}
s.DoJSON("GET", "/api/v1/fleet/software", lsReq, http.StatusOK, &lsResp, "vulnerable", "true")
s.DoJSON("GET", "/api/v1/fleet/software", lsReq, http.StatusOK, &lsResp, "vulnerable", "true", "order_key", "generated_cpe", "order_direction", "desc")
assert.Len(t, lsResp.Software, 1)
assert.Equal(t, soft1.ID, lsResp.Software[0].ID)
assert.Len(t, lsResp.Software[0].Vulnerabilities, 1)