Skip to content

Commit deb13fa

Browse files
committed
stop accessing magic attributes
1 parent c326632 commit deb13fa

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

src/Concerns/Attributes.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private function requestedAttributes(Request $request)
6868
*/
6969
private function resolveAttributes(Request $request)
7070
{
71-
return Collection::make($this->attributes)
71+
return Collection::make(property_exists($this, 'attributes') ? $this->attributes : [])
7272
->mapWithKeys(fn (string $attribute, int|string $key): array => [
7373
$attribute => fn () => $this->resource->{$attribute},
7474
])

src/Concerns/Relationships.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ private function resolveInclude(mixed $resource, string $prefix)
129129
*/
130130
private function resolveRelationships(Request $request)
131131
{
132-
return Collection::make($this->relationships ?? [])
132+
return Collection::make(property_exists($this, 'relationships') ? $this->relationships : [])
133133
->mapWithKeys(fn (string $value, int|string $key) => ! is_int($key) ? [
134134
$key => $value,
135135
] : [

tests/Unit/AttributesAsPropertiesTest.php

+21
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44

55
namespace Tests\Unit;
66

7+
use Exception;
8+
use Illuminate\Database\Eloquent\Model;
79
use Illuminate\Http\Request;
810
use Tests\Models\BasicModel;
911
use Tests\Resources\PostResource;
1012
use Tests\TestCase;
13+
use TiMacDonald\JsonApi\JsonApiResource;
1114

1215
class AttributesAsPropertiesTest extends TestCase
1316
{
@@ -78,4 +81,22 @@ public function toAttributes($request)
7881
'links' => [],
7982
], $response->getData(true)['data']);
8083
}
84+
85+
public function testItDoesntTryToAccessMagicAttributeProperty()
86+
{
87+
$instance = new class extends Model {
88+
public function getAttributesAttribute()
89+
{
90+
throw new \Exception('xxxx');
91+
}
92+
};
93+
$resource = new class ($instance) extends JsonApiResource {
94+
//
95+
};
96+
97+
$response = $resource->toResponse(Request::create('https://timacdonald.me'));
98+
99+
$this->assertValidJsonApi($response->content());
100+
$this->assertSame([], $response->getData(true)['data']['attributes']);
101+
}
81102
}

tests/Unit/RelationshipsAsPropertiesTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Tests\Unit;
66

7+
use Illuminate\Database\Eloquent\Model;
78
use Illuminate\Http\Request;
89
use Illuminate\Support\Str;
910
use Tests\Models\BasicModel;
@@ -222,4 +223,22 @@ public function toRelationships($request)
222223
], $response->getData(true)['included']);
223224
JsonApiResource::guessRelationshipResourceUsing(null);
224225
}
226+
227+
public function testItDoesntTryToAccessMagicAttributeProperty()
228+
{
229+
$instance = new class extends Model {
230+
public function getRelationshipsAttribute()
231+
{
232+
throw new \Exception('xxxx');
233+
}
234+
};
235+
$resource = new class ($instance) extends JsonApiResource {
236+
//
237+
};
238+
239+
$response = $resource->toResponse(Request::create('https://timacdonald.me'));
240+
241+
$this->assertValidJsonApi($response->content());
242+
$this->assertSame([], $response->getData(true)['data']['relationships']);
243+
}
225244
}

0 commit comments

Comments
 (0)