Skip to content

ApiPlatform makes default serializer service broken #5942

Description

@karrakoliko

API Platform version(s) affected: 3.1.16

Description
I want use Serializer service to serialize Api-Platform connected entity (e.g. entity has #[ApiResource] attribute) before it is persisted.

I create serialization group productSnapshot, assign it to the needed fields/getters in my entity.

public function __construct(protected SerializerInterface $serializer)...

public function someMethod(){
// <...>
    $this->serializer->serialize($product, 'json', ['groups' => 'productSnapshot']);
}

I get Unable to generate an IRI for the item of type "App\Entity\***.
I don't even care about IRI or api-platform features in this case: i just want to be able to use Symfony Serializer on it's own.

How to reproduce

  • Entity should have #[ApiResource] attribute
  • Fill some fields to entity, but don't persist
  • Try serialize it

Possible Solution

Workaround 1

I can provide dummy iri as context:

$this->serializer->serialize($product, 'json', ['groups' => 'productSnapshot','iri'=>'take-this-iri-and-stop-throwing-exception']);

Workaround 2

I can configure custom serializer service, and use it instead of default serializer.

Workaround 3

I'm not sure about this one, but i think it is possible to:

  1. override AbstractItemNormalizer/ItemNormalizer to have a context like do-not-use-api-platform-normalizers-for-gods-sake
  2. make default symfony serializer skip api-platform normalizers in chain if that context met

All workarounds seems hacky, because, i think, api-platform serializers/normalizers should not interfere with default symfony serializer work.

In my opinion, these there must be:
a) a way to have ability to exclude api-platform normalizers/serializers from default serializer (blacklist mode)
b) api-platform normalizers/serializers should not be loaded in default serializer chain at all (whitelist mode)
c) whitelist/blacklist behaviour should be configurable

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions