Skip to content

Commit 024ba91

Browse files
committed
Fix roles serialized as JSON object instead of array in PHP 8.5 / Symfony 7.4+
Symfony 7.4 TypeInfo uses the @var annotation to infer list<string> vs array, which affects whether json_encode produces [] or {}. Add the list<string> phpdoc and call array_values() in setRoles() and in UserNormalizer to guarantee sequential integer keys regardless of how the array was built.
1 parent 4a71ce8 commit 024ba91

2 files changed

Lines changed: 3 additions & 2 deletions

File tree

src/Entity/User/AbstractUser.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ abstract class AbstractUser implements SymfonyUserInterface, PasswordAuthenticat
5151
#[Groups(['User:superAdmin', 'User:output', 'Form:cwa_resource:read'])]
5252
protected ?string $emailAddress;
5353

54+
/** @var list<string> */
5455
#[ORM\Column(type: 'json')]
5556
#[Groups(['User:superAdmin', 'User:output', 'Form:cwa_resource:read'])]
5657
protected array $roles;
@@ -183,7 +184,7 @@ public function getRoles(): array
183184

184185
public function setRoles(array $roles): self
185186
{
186-
$this->roles = $roles;
187+
$this->roles = array_values($roles);
187188

188189
return $this;
189190
}

src/Serializer/Normalizer/UserNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function normalize($object, $format = null, array $context = []): float|a
8181
$context[self::ALREADY_CALLED] = true;
8282

8383
$rolesAsEntities = $object->getRoles();
84-
$object->setRoles($this->roleHierarchy->getReachableRoleNames($rolesAsEntities));
84+
$object->setRoles(array_values($this->roleHierarchy->getReachableRoleNames($rolesAsEntities)));
8585

8686
$subscribeTopics = $this->mercureAuthorization->getSubscribeTopics();
8787
$metadata = $this->resourceMetadataProvider->findResourceMetadata($object);

0 commit comments

Comments
 (0)