softfineware / discriminator-default-normalizer
Symfony serializer normalizer for normalizing to parent object in case of not found DiscriminatorMap
Installs: 1 379
Dependents: 1
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 7
Requires
- php: ^8.1
- psr/log: ^3.0
- symfony/property-access: ^6.1
- symfony/serializer: ^6.1
Requires (Dev)
- phpunit/phpunit: ^9.5.26
This package is auto-updated.
Last update: 2024-10-31 00:26:26 UTC
README
How to use it
use SoftFineWare\SerializerDiscriminatorDefault\Attributes\DiscriminatorDefault; use Symfony\Component\Serializer\Annotation\DiscriminatorMap; #[DiscriminatorDefault(class: DefaultRequest::class)] #[DiscriminatorMap(typeProperty: 'type', mapping: [ 'a' => ARequest::class, ])] abstract class BaseRequest { public function __construct( public readonly string $id, public readonly string $type ) { } } class DefaultRequest extends BaseRequest {}
In case of no match json will de denormalized to default class specifing by DiscriminatorDefault
attribute
DiscriminatorDefault
should not point to a class with DiscriminatorMap
new child class must be create as DefaultRequest
above
Installation
This normalizer must have priority -999 before:
\Symfony\Component\Serializer\Normalizer\ObjectNormalizer::class