Skip to content

fix: allow invalid nested includes for morph #99

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

Closed
wants to merge 2 commits into from
Closed
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
17 changes: 13 additions & 4 deletions src/Endpoint/Concerns/IncludesData.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ private function validateInclude(
array $resources,
array $include,
string $path = '',
bool $allowInvalid = false,
): void {
foreach ($include as $name => $nested) {
foreach ($resources as $resource) {
Expand All @@ -71,14 +72,22 @@ private function validateInclude(
? array_map(fn($type) => $context->api->getResource($type), $types)
: array_values($context->api->resources);

$this->validateInclude($context, $relatedResources, $nested, $name . '.');
$this->validateInclude(
$context,
$relatedResources,
$nested,
$name . '.',
$allowInvalid || $field->collection,
);

continue 2;
}

throw (new BadRequestException("Invalid include [$path$name]"))->setSource([
'parameter' => 'include',
]);
if (!$allowInvalid) {
throw (new BadRequestException("Invalid include [$path$name]"))->setSource([
'parameter' => 'include',
]);
}
}
}
}
7 changes: 6 additions & 1 deletion src/Schema/Field/Relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ abstract class Relationship extends Field
public array $collections;
public bool $includable = false;
public bool $linkage = false;
public bool $collection = false;

/**
* Set the collection(s) that this relationship is to.
*/
public function collection(string|array $type): static
{
$this->collections = (array) $type;
$this->collection = true;

return $this;
}
Expand All @@ -31,7 +33,10 @@ public function collection(string|array $type): static
*/
public function type(string|array $type): static
{
return $this->collection($type);
$this->collections = (array) $type;
$this->collection = count($this->collections) > 1;

return $this;
}

/**
Expand Down