Skip to content

Commit

Permalink
fix: relations specified in "alwaysIncluded" method were not included
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzarbn committed Oct 31, 2022
1 parent c264c6a commit 18fe34a
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/Drivers/Standard/ParamsValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public function validateIncludes(Request $request): void
Validator::make(
$request->query(),
[
'includes' => ['sometimes', 'string', new WhitelistedQueryFields($this->includableBy)],
'include' => ['sometimes', 'string', new WhitelistedQueryFields($this->includableBy)],
]
)->validate();
}
Expand Down
29 changes: 14 additions & 15 deletions src/Drivers/Standard/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ class QueryBuilder implements \Orion\Contracts\QueryBuilder
*/
private $intermediateMode;

/**
* @var string $table
*/
private $table;

/**
* @inheritDoc
*/
Expand Down Expand Up @@ -329,7 +324,8 @@ protected function buildPivotFilterNestedQueryWhereClause(
*/
public function getQualifiedFieldName(string $field): string
{
$table = $this->table ?? (new $this->resourceModelClass)->getTable();
$table = (new $this->resourceModelClass)->getTable();

return "{$table}.{$field}";
}

Expand Down Expand Up @@ -538,7 +534,7 @@ public function applyAggregatesToQuery($query, Request $request, array $aggregat
);
}

$aggregateDescriptors = $aggregateDescriptors->merge($request->post('aggregates', []));
$aggregateDescriptors = $aggregateDescriptors->merge($request->get('aggregates', []));
}

foreach ($aggregateDescriptors as $aggregateDescriptor) {
Expand Down Expand Up @@ -586,14 +582,17 @@ public function applyIncludesToQuery($query, Request $request, array $includeDes
{
if (!$includeDescriptors) {
$this->paramsValidator->validateIncludes($request);
// Here we regroup query and post params on the same format
$includeDescriptors =
collect(explode(',', $request->query('include', '')))
->filter()
->map(function ($include) {
return ['relation' => $include];
})
->merge($request->post('includes', []));

$requestedIncludeDescriptors = collect($request->get('includes', []));

$includeDescriptors = collect($this->relationsResolver->requestedRelations($request))
->map(function ($include) use ($requestedIncludeDescriptors) {
$requestedIncludeDescriptor = $requestedIncludeDescriptors
->where('relation', $include)
->first();

return $requestedIncludeDescriptor ?? ['relation' => $include];
})->toArray();
}

foreach ($includeDescriptors as $includeDescriptor) {
Expand Down
17 changes: 11 additions & 6 deletions src/Drivers/Standard/RelationsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,17 @@ public function __construct(array $includableRelations, array $alwaysIncludedRel
*/
public function requestedRelations(Request $request): array
{
$requestedIncludesQueryStr = $request->query('include', '');
$requestedIncludesQuery = explode(',', $requestedIncludesQueryStr);
$requestedIncludesPost = collect($request->post('includes', []))->pluck('relation');
$requestedIncludes = $requestedIncludesPost->merge($requestedIncludesQuery)->unique()->filter()->all();
$requestedIncludesQuery = collect(explode(',', $request->query('include', '')));
$requestedIncludesBody = collect($request->get('includes', []))->pluck('relation');

$allowedIncludes = array_unique(array_merge($this->includableRelations, $this->alwaysIncludedRelations));
$requestedIncludes = $requestedIncludesQuery
->merge($requestedIncludesBody)
->merge($this->alwaysIncludedRelations)
->unique()->filter()->all();

$allowedIncludes = array_unique(
array_merge($this->includableRelations, $this->alwaysIncludedRelations)
);

$validatedIncludes = [];

Expand All @@ -69,7 +74,7 @@ public function requestedRelations(Request $request): array
}
}

return array_unique(array_merge($validatedIncludes, $this->alwaysIncludedRelations));
return $validatedIncludes;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Controllers/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function __construct()
'filterableBy' => $this->filterableBy(),
'sortableBy' => $this->sortableBy(),
'aggregatableBy' => $this->aggregates(),
'includableBy' => $this->includes(),
'includableBy' => array_merge($this->includes(), $this->alwaysIncludes()),
]
);
$this->relationsResolver = App::makeWith(
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Http/Controllers/BaseControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function dependencies_are_resolved_correctly()
'filterableBy' => ['test_filterable_field'],
'sortableBy' => ['test_sortable_field'],
'aggregatableBy' => ['test_aggregatable_field'],
'includableBy' => ['testRelation'],
'includableBy' => ['testRelation', 'testAlwaysIncludedRelation'],
]
)->once()->andReturn($fakeParamsValidator);

Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Http/Controllers/RelationControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function dependencies_are_resolved_correctly()
'filterableBy' => ['test_filterable_field'],
'sortableBy' => ['test_sortable_field'],
'aggregatableBy' => ['test_aggregatable_field'],
'includableBy' => ['testRelation'],
'includableBy' => ['testRelation', 'testAlwaysIncludedRelation'],
]
)->once()->andReturn($fakeParamsValidator);

Expand Down

0 comments on commit 18fe34a

Please sign in to comment.