Skip to content

Commit 18fe34a

Browse files
committed
fix: relations specified in "alwaysIncluded" method were not included
1 parent c264c6a commit 18fe34a

File tree

6 files changed

+29
-25
lines changed

6 files changed

+29
-25
lines changed

src/Drivers/Standard/ParamsValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public function validateIncludes(Request $request): void
213213
Validator::make(
214214
$request->query(),
215215
[
216-
'includes' => ['sometimes', 'string', new WhitelistedQueryFields($this->includableBy)],
216+
'include' => ['sometimes', 'string', new WhitelistedQueryFields($this->includableBy)],
217217
]
218218
)->validate();
219219
}

src/Drivers/Standard/QueryBuilder.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,6 @@ class QueryBuilder implements \Orion\Contracts\QueryBuilder
4141
*/
4242
private $intermediateMode;
4343

44-
/**
45-
* @var string $table
46-
*/
47-
private $table;
48-
4944
/**
5045
* @inheritDoc
5146
*/
@@ -329,7 +324,8 @@ protected function buildPivotFilterNestedQueryWhereClause(
329324
*/
330325
public function getQualifiedFieldName(string $field): string
331326
{
332-
$table = $this->table ?? (new $this->resourceModelClass)->getTable();
327+
$table = (new $this->resourceModelClass)->getTable();
328+
333329
return "{$table}.{$field}";
334330
}
335331

@@ -538,7 +534,7 @@ public function applyAggregatesToQuery($query, Request $request, array $aggregat
538534
);
539535
}
540536

541-
$aggregateDescriptors = $aggregateDescriptors->merge($request->post('aggregates', []));
537+
$aggregateDescriptors = $aggregateDescriptors->merge($request->get('aggregates', []));
542538
}
543539

544540
foreach ($aggregateDescriptors as $aggregateDescriptor) {
@@ -586,14 +582,17 @@ public function applyIncludesToQuery($query, Request $request, array $includeDes
586582
{
587583
if (!$includeDescriptors) {
588584
$this->paramsValidator->validateIncludes($request);
589-
// Here we regroup query and post params on the same format
590-
$includeDescriptors =
591-
collect(explode(',', $request->query('include', '')))
592-
->filter()
593-
->map(function ($include) {
594-
return ['relation' => $include];
595-
})
596-
->merge($request->post('includes', []));
585+
586+
$requestedIncludeDescriptors = collect($request->get('includes', []));
587+
588+
$includeDescriptors = collect($this->relationsResolver->requestedRelations($request))
589+
->map(function ($include) use ($requestedIncludeDescriptors) {
590+
$requestedIncludeDescriptor = $requestedIncludeDescriptors
591+
->where('relation', $include)
592+
->first();
593+
594+
return $requestedIncludeDescriptor ?? ['relation' => $include];
595+
})->toArray();
597596
}
598597

599598
foreach ($includeDescriptors as $includeDescriptor) {

src/Drivers/Standard/RelationsResolver.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,17 @@ public function __construct(array $includableRelations, array $alwaysIncludedRel
4141
*/
4242
public function requestedRelations(Request $request): array
4343
{
44-
$requestedIncludesQueryStr = $request->query('include', '');
45-
$requestedIncludesQuery = explode(',', $requestedIncludesQueryStr);
46-
$requestedIncludesPost = collect($request->post('includes', []))->pluck('relation');
47-
$requestedIncludes = $requestedIncludesPost->merge($requestedIncludesQuery)->unique()->filter()->all();
44+
$requestedIncludesQuery = collect(explode(',', $request->query('include', '')));
45+
$requestedIncludesBody = collect($request->get('includes', []))->pluck('relation');
4846

49-
$allowedIncludes = array_unique(array_merge($this->includableRelations, $this->alwaysIncludedRelations));
47+
$requestedIncludes = $requestedIncludesQuery
48+
->merge($requestedIncludesBody)
49+
->merge($this->alwaysIncludedRelations)
50+
->unique()->filter()->all();
51+
52+
$allowedIncludes = array_unique(
53+
array_merge($this->includableRelations, $this->alwaysIncludedRelations)
54+
);
5055

5156
$validatedIncludes = [];
5257

@@ -69,7 +74,7 @@ public function requestedRelations(Request $request): array
6974
}
7075
}
7176

72-
return array_unique(array_merge($validatedIncludes, $this->alwaysIncludedRelations));
77+
return $validatedIncludes;
7378
}
7479

7580
/**

src/Http/Controllers/BaseController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public function __construct()
113113
'filterableBy' => $this->filterableBy(),
114114
'sortableBy' => $this->sortableBy(),
115115
'aggregatableBy' => $this->aggregates(),
116-
'includableBy' => $this->includes(),
116+
'includableBy' => array_merge($this->includes(), $this->alwaysIncludes()),
117117
]
118118
);
119119
$this->relationsResolver = App::makeWith(

tests/Unit/Http/Controllers/BaseControllerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function dependencies_are_resolved_correctly()
5959
'filterableBy' => ['test_filterable_field'],
6060
'sortableBy' => ['test_sortable_field'],
6161
'aggregatableBy' => ['test_aggregatable_field'],
62-
'includableBy' => ['testRelation'],
62+
'includableBy' => ['testRelation', 'testAlwaysIncludedRelation'],
6363
]
6464
)->once()->andReturn($fakeParamsValidator);
6565

tests/Unit/Http/Controllers/RelationControllerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function dependencies_are_resolved_correctly()
5353
'filterableBy' => ['test_filterable_field'],
5454
'sortableBy' => ['test_sortable_field'],
5555
'aggregatableBy' => ['test_aggregatable_field'],
56-
'includableBy' => ['testRelation'],
56+
'includableBy' => ['testRelation', 'testAlwaysIncludedRelation'],
5757
]
5858
)->once()->andReturn($fakeParamsValidator);
5959

0 commit comments

Comments
 (0)