Skip to content

Commit

Permalink
[10.x] Can apply WithoutRelations to entire class (#48068)
Browse files Browse the repository at this point in the history
* allow applying WithoutRelations to class

* prove attribute does not interfere with other properties during serialization

* refactoring

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
cosmastech and taylorotwell committed Aug 14, 2023
1 parent 777f1fe commit c1e6281
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Queue/Attributes/WithoutRelations.php
Expand Up @@ -4,7 +4,7 @@

use Attribute;

#[Attribute(Attribute::TARGET_PROPERTY)]
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY)]
class WithoutRelations
{
//
Expand Down
11 changes: 8 additions & 3 deletions src/Illuminate/Queue/SerializesModels.php
Expand Up @@ -19,9 +19,13 @@ public function __serialize()
{
$values = [];

$properties = (new ReflectionClass($this))->getProperties();
$reflectionClass = new ReflectionClass($this);

$class = get_class($this);
[$class, $properties, $classLevelWithoutRelations] = [
get_class($this),
$reflectionClass->getProperties(),
! empty($reflectionClass->getAttributes(WithoutRelations::class)),
];

foreach ($properties as $property) {
if ($property->isStatic()) {
Expand All @@ -48,7 +52,8 @@ public function __serialize()

$values[$name] = $this->getSerializedPropertyValue(
$value,
empty($property->getAttributes(WithoutRelations::class))
! $classLevelWithoutRelations &&
empty($property->getAttributes(WithoutRelations::class))
);
}

Expand Down
37 changes: 37 additions & 0 deletions tests/Integration/Queue/ModelSerializationTest.php
Expand Up @@ -332,6 +332,26 @@ public function test_it_respects_without_relations_attribute()
);
}

public function test_it_respects_without_relations_attribute_applied_to_class()
{
$user = User::create([
'email' => 'taylor@laravel.com',
])->load(['roles']);

$serialized = serialize(new ModelSerializationAttributeTargetsClassTestClass($user, new DataValueObject('hello')));

$this->assertSame(
'O:83:"Illuminate\Tests\Integration\Queue\ModelSerializationAttributeTargetsClassTestClass":2:{s:4:"user";O:45:"Illuminate\Contracts\Database\ModelIdentifier":5:{s:5:"class";s:39:"Illuminate\Tests\Integration\Queue\User";s:2:"id";i:1;s:9:"relations";a:0:{}s:10:"connection";s:7:"testing";s:15:"collectionClass";N;}s:5:"value";O:50:"Illuminate\Tests\Integration\Queue\DataValueObject":1:{s:5:"value";s:5:"hello";}}',
$serialized
);

/** @var ModelSerializationAttributeTargetsClassTestClass $unserialized */
$unserialized = unserialize($serialized);

$this->assertFalse($unserialized->user->relationLoaded('roles'));
$this->assertEquals('hello', $unserialized->value->value);
}

public function test_serialization_types_empty_custom_eloquent_collection()
{
$class = new ModelSerializationTypedCustomCollectionTestClass(
Expand Down Expand Up @@ -526,6 +546,16 @@ public function __construct(User $user)
}
}

#[WithoutRelations]
class ModelSerializationAttributeTargetsClassTestClass
{
use SerializesModels;

public function __construct(public User $user, public DataValueObject $value)
{
}
}

class ModelRelationSerializationTestClass
{
use SerializesModels;
Expand All @@ -549,3 +579,10 @@ public function __construct($users)
$this->users = $users;
}
}

class DataValueObject
{
public function __construct(public $value = 1)
{
}
}

0 comments on commit c1e6281

Please sign in to comment.