Skip to content

Commit

Permalink
stop accessing magic attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald committed Feb 14, 2023
1 parent c326632 commit deb13fa
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Concerns/Attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private function requestedAttributes(Request $request)
*/
private function resolveAttributes(Request $request)
{
return Collection::make($this->attributes)
return Collection::make(property_exists($this, 'attributes') ? $this->attributes : [])
->mapWithKeys(fn (string $attribute, int|string $key): array => [
$attribute => fn () => $this->resource->{$attribute},
])
Expand Down
2 changes: 1 addition & 1 deletion src/Concerns/Relationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private function resolveInclude(mixed $resource, string $prefix)
*/
private function resolveRelationships(Request $request)
{
return Collection::make($this->relationships ?? [])
return Collection::make(property_exists($this, 'relationships') ? $this->relationships : [])
->mapWithKeys(fn (string $value, int|string $key) => ! is_int($key) ? [
$key => $value,
] : [
Expand Down
21 changes: 21 additions & 0 deletions tests/Unit/AttributesAsPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

namespace Tests\Unit;

use Exception;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Tests\Models\BasicModel;
use Tests\Resources\PostResource;
use Tests\TestCase;
use TiMacDonald\JsonApi\JsonApiResource;

class AttributesAsPropertiesTest extends TestCase
{
Expand Down Expand Up @@ -78,4 +81,22 @@ public function toAttributes($request)
'links' => [],
], $response->getData(true)['data']);
}

public function testItDoesntTryToAccessMagicAttributeProperty()
{
$instance = new class extends Model {
public function getAttributesAttribute()
{
throw new \Exception('xxxx');
}
};
$resource = new class ($instance) extends JsonApiResource {
//
};

$response = $resource->toResponse(Request::create('https://timacdonald.me'));

$this->assertValidJsonApi($response->content());
$this->assertSame([], $response->getData(true)['data']['attributes']);
}
}
19 changes: 19 additions & 0 deletions tests/Unit/RelationshipsAsPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Tests\Unit;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Tests\Models\BasicModel;
Expand Down Expand Up @@ -222,4 +223,22 @@ public function toRelationships($request)
], $response->getData(true)['included']);
JsonApiResource::guessRelationshipResourceUsing(null);
}

public function testItDoesntTryToAccessMagicAttributeProperty()
{
$instance = new class extends Model {
public function getRelationshipsAttribute()
{
throw new \Exception('xxxx');
}
};
$resource = new class ($instance) extends JsonApiResource {
//
};

$response = $resource->toResponse(Request::create('https://timacdonald.me'));

$this->assertValidJsonApi($response->content());
$this->assertSame([], $response->getData(true)['data']['relationships']);
}
}

0 comments on commit deb13fa

Please sign in to comment.