Skip to content

[Serializer] Allow to serialize to parent class if none match for DiscriminatorMap #48321

@Legion112

Description

@Legion112

Description

We have following DiscriminatorMap config.

use Symfony\Component\Serializer\Annotation\DiscriminatorMap;

#[DiscriminatorMap(typeProperty: 'type', mapping: [
    'type_child' => Child::class,
])]
class ParentClass {
    public int $id;
    public string $type;
}


class Child extends ParentClass {

}

/** @var \Symfony\Component\Serializer\SerializerInterface $serilizer */

$serilizer->deserialize(<<<JSON
{"type":"somethingElse","id": 1}
JSON,
    ParentClass::class,
    'json'
);

Current behavior.
The type "somethingElse" is not a valid value.
Probably we should rollback to the parent object by default or have ability to set the default class like so:

#[DiscriminatorMap(typeProperty: 'type', mapping: [
    'type_child' => Child::class,
    '*' => ParentClass::class,
])]

Example

No response

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