Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[FrameworkBundle] BackedEnum Serializer priority Change Broke API Platform BackedEnum Serialization #54478

Copy link
Copy link
Closed
@gnito-org

Description

@gnito-org
Issue body actions

Symfony version(s) affected

7.0.6

Description

The BackedEnum serializer priority change broke API Platform's serialization of backed enums, where the backed enum is declared as an API resource. Maybe it broke Symfony's serialization as well, but I did not test that.

After I upgraded to Symfony 7.0.6, backed enums are serialized in API Platform to just a simple array with the string values of the enum, and not as a full API resource. If I hack the BackedEnum Serializer priority back to -915 in Symfony\Component\DependencyInjection\Loader\Configurator\serializer.php, the serialization works 100% like prior to 7.0.6.

The API Platform version did not change in my app.

How to reproduce

Serialize a backed enum in API Platform, as an API Platform resource.

Possible Solution

No response

Additional Context

<?php

namespace App\ApiResource;

use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Serializer\Filter\PropertyFilter;
use App\StateProvider\TeamMemberRoleCodeProvider;
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
use Symfony\Component\Serializer\Annotation\Groups;

#[ApiResource(operations: [
        new Get(),
        new GetCollection(),
    ], normalizationContext: [
        'groups' => [self::RESOURCE_GET],
        'openapi_definition_name' => 'get',
    ], security: 'is_granted("'.AuthenticatedVoter::IS_AUTHENTICATED.'")', provider: TeamMemberRoleCodeProvider::class,)]
enum TeamMemberRoleCode: string
{
    case Contributor = 'contributor';
    case Reader = 'reader';
    case Manager = 'manager';
    case Owner = 'owner';
    public const string RESOURCE_GET = 'team_member_role_code:get';

    public static function createInstance(?string $id): ?self
    {
        return self::tryFrom($id);
    }

    #[Groups([self::RESOURCE_GET])]
    #[ApiProperty(description: AppEntityDescriptions::getCodeDescription, writable: false)]
    public function getCodeDescription(): string
    {
        return match ($this) {
            self::Owner => 'The person has no privilege restrictions on any resources in the user account.',
            self::Manager => 'The person can read from and write to all resources that are available to the team, and can invite, remove and change the roles of team members.',
            self::Contributor => 'The person can read from and write to all resources that are available to the team.',
            self::Reader => 'The person can read from all resources that are available to the team.',
        };
    }

    #[Groups([self::RESOURCE_GET])]
    #[ApiProperty(description: AppEntityDescriptions::id, writable: false, identifier: true)]
    public function getId(): string
    {
        return $this->value;
    }
}
<?php

namespace App\StateProvider;

use ApiPlatform\Metadata\CollectionOperationInterface;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\ApiResource\TeamMemberRoleCode;

class TeamMemberRoleCodeProvider implements ProviderInterface
{
    public function provide(
        Operation $operation,
        array $uriVariables = [],
        array $context = []
    ): array|object|null {
        if ($operation instanceof CollectionOperationInterface) {
            return TeamMemberRoleCode::cases();
        }

        return TeamMemberRoleCode::createInstance($uriVariables['id']);
    }
}

API Platform output prior to 7.0.6

{
  "@context": "/api/contexts/TeamMemberRoleCode",
  "@id": "/api/team-member-role-code",
  "@type": "hydra:Collection",
  "hydra:totalItems": 4,
  "hydra:member": [
    {
      "@id": "/api/team-member-role-code/contributor",
      "@type": "TeamMemberRoleCode",
      "codeDescription": "The person can read from and write to all resources that are available to the team.",
      "id": "contributor"
    },
    {
      "@id": "/api/team-member-role-code/reader",
      "@type": "TeamMemberRoleCode",
      "codeDescription": "The person can read from all resources that are available to the team.",
      "id": "reader"
    },
    {
      "@id": "/api/team-member-role-code/manager",
      "@type": "TeamMemberRoleCode",
      "codeDescription": "The person can read from and write to all resources that are available to the team, and can invite, remove and change the roles of team members.",
      "id": "manager"
    },
    {
      "@id": "/api/team-member-role-code/owner",
      "@type": "TeamMemberRoleCode",
      "codeDescription": "The person has no privilege restrictions on any resources in the user account.",
      "id": "owner"
    }
  ],
  "hydra:search": {
    "@type": "hydra:IriTemplate",
    "hydra:template": "/api/team-member-role-code{?properties[]}",
    "hydra:variableRepresentation": "BasicRepresentation",
    "hydra:mapping": [
      {
        "@type": "IriTemplateMapping",
        "variable": "properties[]",
        "property": null,
        "required": false
      }
    ]
  }
}

API Platform output with 7.0.6

{
  "@context": "/api/contexts/TeamMemberRoleCode",
  "@id": "/api/team-member-role-code",
  "@type": "hydra:Collection",
  "hydra:totalItems": 4,
  "hydra:member": [
    "contributor",
    "reader",
    "manager",
    "owner"
  ],
  "hydra:search": {
    "@type": "hydra:IriTemplate",
    "hydra:template": "/api/team-member-role-code{?properties[]}",
    "hydra:variableRepresentation": "BasicRepresentation",
    "hydra:mapping": [
      {
        "@type": "IriTemplateMapping",
        "variable": "properties[]",
        "property": null,
        "required": false
      }
    ]
  }
}

Service Config

This service configuration in 7.0.6 makes the problem go away:

    serializer.normalizer.backed_enum:
        class: Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer
        tags:
            - { name: 'serializer.normalizer', priority: -915 }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.