Lomkit / laravel-rest-api

Generate Api in seconds
https://laravel-rest-api.lomkit.com/
MIT License
444 stars 25 forks source link

bug filters in Aggregates #107

Closed ThanhSonITNIC closed 7 months ago

ThanhSonITNIC commented 7 months ago

Description

In PerformSearch at line 255, $resource is a relation resource https://github.com/Lomkit/laravel-rest-api/blob/460a3d231208c3ca42092d9b2bfe9b8272f8e1c2/src/Query/Traits/PerformSearch.php#L222-L231

But when validate in aggregatesRules it not a relation resource for filters validation https://github.com/Lomkit/laravel-rest-api/blob/460a3d231208c3ca42092d9b2bfe9b8272f8e1c2/src/Rules/SearchRules.php#L301-L304

So Aggregates filters not work correctly

GautierDele commented 7 months ago

Hello @ThanhSonITNIC,

Can you give me an example to reproduce this to cover this in a Unit test please ?

ThanhSonITNIC commented 7 months ago
PostResource
- id
- title
StarResource
- id
- rate
- approved

Get total approved rate [POST] api/posts/search

{
  "search": {
    "aggregates": [
      {
        "relation": "stars",
        "type": "sum",
        "field": "rate",
        "filters": [
          {"field": "approved", "value": true}
        ]
      }
    ]
  }
}

It's throw an error 422 The search.aggregates.0.filters.0.field field is not valid because no field approved in validation list. After debug, I see Aggregate validation fields is [id, title], but it should be [id, rate, approved]

GautierDele commented 7 months ago

I'm gonna investigate this in detail 😄

GautierDele commented 7 months ago

Thanks for the feedback, I did adapt your fix to the package. It's fixed and covered by a new test