Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option disable_invalid_includes_query_exception #906

Merged
merged 2 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions config/query-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
*/
'disable_invalid_sort_query_exception' => false,

/*
* By default the package will throw an `InvalidIncludeQuery` exception when an include in the
* URL is not allowed in the `allowedIncludes()` method.
*/
'disable_invalid_includes_query_exception' => false,

/*
* By default, the package expects relationship names to be snake case plural when using fields[relationship].
* For example, fetching the id and name for a userOwner relation would look like this:
Expand Down
19 changes: 18 additions & 1 deletion src/Concerns/AddsIncludesToQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ public function allowedIncludes($includes): static

$this->ensureAllIncludesExist();

$this->addIncludesToQuery($this->request->includes());
$includes = $this->filterNonExistingIncludes($this->request->includes());

$this->addIncludesToQuery($includes);

return $this;
}
Expand All @@ -69,6 +71,10 @@ protected function findInclude(string $include): ?AllowedInclude

protected function ensureAllIncludesExist()
{
if (config('query-builder.disable_invalid_includes_query_exception', false)) {
return;
}

$includes = $this->request->includes();

$allowedIncludeNames = $this->allowedIncludes->map(function (AllowedInclude $allowedInclude) {
Expand All @@ -83,4 +89,15 @@ protected function ensureAllIncludesExist()

// TODO: Check for non-existing relationships?
}

protected function filterNonExistingIncludes(Collection $includes): Collection
{
if (config('query-builder.disable_invalid_includes_query_exception', false) == false) {
return $includes;
}

return $includes->filter(function ($include) {
return $this->findInclude($include);
});
}
}
9 changes: 9 additions & 0 deletions tests/IncludeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,15 @@
->allowedIncludes('relatedModels');
});

it('does not throw invalid include query exception when disable in config', function () {
config(['query-builder.disable_invalid_includes_query_exception' => true]);

createQueryFromIncludeRequest('random-model')
->allowedIncludes('relatedModels');

expect(true)->toBeTrue();
});

it('can allow multiple includes', function () {
$models = createQueryFromIncludeRequest('relatedModels')
->allowedIncludes('relatedModels', 'otherRelatedModels')
Expand Down
Loading