Skip to content

Commit b8dd43e

Browse files
committed
Allow multiple passes on the same resource to add missing fields
This solves for a scenario where you have nested includes which result in the same resource being included twice with different sub-includes. For example, if `include=author,editor.roles`, and the author and editor point to the same `users` resource, you still want the `roles` relationship to be loaded on that user.
1 parent 3b645b6 commit b8dd43e

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

src/Serializer.php

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,28 @@ private function addToMap(ResourceType $resourceType, $model, array $include): a
7171
$id = $adapter->getId($model)
7272
);
7373

74-
if (isset($this->map[$key])) {
75-
return $this->map[$key];
74+
$url = $resourceType->url($model, $this->context);
75+
76+
if (!isset($this->map[$key])) {
77+
$this->map[$key] = [
78+
'type' => $type,
79+
'id' => $id,
80+
'fields' => [],
81+
'links' => [
82+
'self' => new Structure\Link\SelfLink($url),
83+
],
84+
'meta' => $this->meta($schema->getMeta(), $model)
85+
];
7686
}
7787

78-
$this->map[$key] = [
79-
'type' => $type,
80-
'id' => $id,
81-
'fields' => [],
82-
'links' => [
83-
'self' => new Structure\Link\SelfLink($url = $resourceType->url($model, $this->context)),
84-
],
85-
'meta' => $this->meta($schema->getMeta(), $model)
86-
];
87-
8888
$fields = $this->sparseFields($type, $schema->getFields());
8989

9090
foreach ($fields as $field) {
91-
if (! evaluate($field->getVisible(), [$model, $this->context])) {
91+
if (isset($this->map[$key]['fields'][$field->getName()])) {
92+
continue;
93+
}
94+
95+
if (!evaluate($field->getVisible(), [$model, $this->context])) {
9296
continue;
9397
}
9498

0 commit comments

Comments
 (0)