From d729dc37fa7aaddb7658c52020678cc5294387f1 Mon Sep 17 00:00:00 2001 From: Michele Locati Date: Thu, 23 Feb 2023 16:22:16 +0100 Subject: [PATCH] feat: register DateTimeNormalizer by default (#119) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add a note about DateTimeNormalizer in README Without it, serializing DateTimeInterface instances leads to huge serialized data. * Register the DateTimeNormalizer in the Symfony bundle * minor tweaks --------- Co-authored-by: Kévin Dunglas --- README.md | 19 +++++++++++-------- src/Bundle/Resources/config/services.xml | 16 ++++++++++------ tests/Fixtures/AppKernel.php | 1 + 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 26159c9..b239d65 100644 --- a/README.md +++ b/README.md @@ -40,12 +40,14 @@ use Dunglas\DoctrineJsonOdm\Serializer; use Dunglas\DoctrineJsonOdm\Type\JsonDocumentType; use Symfony\Component\Serializer\Encoder\JsonEncoder; use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer; +use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer; +use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; if (!Type::hasType('json_document')) { Type::addType('json_document', JsonDocumentType::class); Type::getType('json_document')->setSerializer( - new Serializer([new ArrayDenormalizer(), new ObjectNormalizer()], [new JsonEncoder()]) + new Serializer([new BackedEnumNormalizer(), new DateTimeNormalizer(), new ArrayDenormalizer(), new ObjectNormalizer()], [new JsonEncoder()]) ); } @@ -214,7 +216,6 @@ For using the built-in type mapper: ); ``` - ### Limitations when updating nested properties Due to how Doctrine works, it will not detect changes to nested objects or properties. @@ -291,21 +292,23 @@ class Kernel extends BaseKernel **How can I add additional normalizers?** -The Symfony Serializer is easily extensible. This bundle registers and uses a service with ID `dunglas_doctrine_json_odm.serializer` as the serializer for the JSON type. This means we can easily override it in our `services.yaml` to use additional normalizers. -As an example we use the Symfony `DateTimeNormalizer` service, so we do have support for any property that is an instance of `\DateTimeInterface`. Be aware that the order of the normalizers might be relevant depending on the normalizers you use. +The Symfony Serializer is easily extensible. This bundle registers and uses a service with ID `dunglas_doctrine_json_odm.serializer` as the serializer for the JSON type. +This means we can easily override it in our `services.yaml` to use additional normalizers. +As an example we inject a custom normalizer service. Be aware that the order of the normalizers might be relevant depending on the normalizers you use. ```yaml # Add DateTime Normalizer to Dunglas' Doctrine JSON ODM Bundle dunglas_doctrine_json_odm.serializer: class: Dunglas\DoctrineJsonOdm\Serializer arguments: - - ['@dunglas_doctrine_json_odm.normalizer.array', '@serializer.normalizer.datetime', '@dunglas_doctrine_json_odm.normalizer.object'] + - ['@App\MyCustom\Normalizer', '@?dunglas_doctrine_json_odm.normalizer.backed_enum', '@dunglas_doctrine_json_odm.normalizer.datetime', '@dunglas_doctrine_json_odm.normalizer.array', '@dunglas_doctrine_json_odm.normalizer.object'] - ['@serializer.encoder.json'] + - '@?dunglas_doctrine_json_odm.type_mapper' public: true + autowire: false + autoconfigure: false ``` -As a side note: If you happen to use [Autowiring](https://symfony.com/doc/current/service_container/autowiring.html) in your `services.yaml` you might need to set `autowire: false` too. Same goes for `autoconfigure: false` in case you're using [Autoconfiguration](https://symfony.com/doc/current/service_container.html#the-autoconfigure-option). - **When the namespace of a used entity changes** For classes without [type aliases](#using-type-aliases), because we store the `#type` along with the data in the database, you have to migrate the already existing data in your database to reflect the new namespace. @@ -321,7 +324,7 @@ WHERE 'AppBundle\\\Entity\\\Bar' = JSON_EXTRACT(misc, '$."#type"'); ## Credits -This bundle is brought to you by [Kévin Dunglas](https://dunglas.fr) and [awesome contributors](https://github.com/dunglas/doctrine-json-odm/graphs/contributors). +This bundle is brought to you by [Kévin Dunglas](https://dunglas.dev) and [awesome contributors](https://github.com/dunglas/doctrine-json-odm/graphs/contributors). Sponsored by [Les-Tilleuls.coop](https://les-tilleuls.coop). ## Former Maintainers diff --git a/src/Bundle/Resources/config/services.xml b/src/Bundle/Resources/config/services.xml index 30acc32..ad0eb25 100644 --- a/src/Bundle/Resources/config/services.xml +++ b/src/Bundle/Resources/config/services.xml @@ -5,6 +5,15 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> + + + + + + + + + null @@ -13,15 +22,10 @@ - - - - - - + diff --git a/tests/Fixtures/AppKernel.php b/tests/Fixtures/AppKernel.php index c9cb300..e8ca073 100644 --- a/tests/Fixtures/AppKernel.php +++ b/tests/Fixtures/AppKernel.php @@ -46,6 +46,7 @@ protected function configureContainer(ContainerBuilder $container, LoaderInterfa $container->loadFromExtension('framework', [ 'secret' => 'jsonodm', 'test' => null, + 'http_method_override' => false, ]); $container->loadFromExtension('doctrine', [