Skip to content

Commit

Permalink
Merge pull request #102 from Lomkit/fix/filter-not-authorized-relation
Browse files Browse the repository at this point in the history
🐛 including not authorized relation
  • Loading branch information
GautierDele authored Jan 26, 2024
2 parents bc82057 + 51524b4 commit 460a3d2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Concerns/Resource/ConfiguresRestParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function isNestedField(string $field, Relation $relation = null)

$fieldRelation = $this->relation(Str::before($field, '.'));

return $fieldRelation->resource()->isNestedField(Str::after($field, '.'), $fieldRelation);
return $fieldRelation?->resource()->isNestedField(Str::after($field, '.'), $fieldRelation) ?? false;
}

return in_array($field, $this->getFields(App::make(RestRequest::class)));
Expand Down
22 changes: 22 additions & 0 deletions tests/Feature/Controllers/SearchFilteringOperationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,28 @@ public function test_getting_a_list_of_resources_filtered_by_not_authorized_fiel
$response->assertJsonStructure(['message', 'errors' => ['search.filters.0.field']]);
}

public function test_getting_a_list_of_resources_filtered_by_not_authorized_relation_field(): void
{
ModelFactory::new()->count(2)->create();

Gate::policy(Model::class, GreenPolicy::class);

$response = $this->post(
'/api/models/search',
[
'search' => [
'filters' => [
['field' => 'non_authorized_relation.field', 'value' => 'value'],
],
],
],
['Accept' => 'application/json']
);

$response->assertStatus(422);
$response->assertJsonStructure(['message', 'errors' => ['search.filters.0.field']]);
}

public function test_getting_a_list_of_resources_filtered_by_model_field_using_default_operator(): void
{
$matchingModel = ModelFactory::new()->create(['name' => 'match'])->fresh();
Expand Down

0 comments on commit 460a3d2

Please sign in to comment.