Skip to content

Commit

Permalink
Fixed serializer issue (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
CvekCoding authored and Toflar committed Oct 24, 2019
1 parent 435a52f commit aecbaae
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ As an example we use the Symfony `DateTimeNormalizer` service so we do have supp
dunglas_doctrine_json_odm.serializer:
class: Dunglas\DoctrineJsonOdm\Serializer
arguments:
- ['@serializer.denormalizer.array', '@serializer.normalizer.datetime', '@serializer.normalizer.object']
- ['@dunglas_doctrine_json_odm.normalizer.array', '@serializer.normalizer.datetime', '@dunglas_doctrine_json_odm.normalizer.object']
- ['@serializer.encoder.json']
public: true
```
Expand Down
14 changes: 12 additions & 2 deletions src/Bundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,20 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="dunglas_doctrine_json_odm.normalizer.object" class="Symfony\Component\Serializer\Normalizer\ObjectNormalizer" public="false">
<argument type="service" id="serializer.mapping.class_metadata_factory" on-invalid="ignore" />
<argument>null</argument><!-- name converter -->
<argument type="service" id="serializer.property_accessor" />
<argument type="service" id="property_info" on-invalid="ignore" />
<argument type="service" id="serializer.mapping.class_discriminator_resolver" on-invalid="ignore" />
</service>

<service id="dunglas_doctrine_json_odm.normalizer.array" class="Symfony\Component\Serializer\Normalizer\ArrayDenormalizer" public="false" />

<service id="dunglas_doctrine_json_odm.serializer" class="Dunglas\DoctrineJsonOdm\Serializer" public="true">
<argument type="collection">
<argument type="service" id="serializer.denormalizer.array" />
<argument type="service" id="serializer.normalizer.object" />
<argument type="service" id="dunglas_doctrine_json_odm.normalizer.array" />
<argument type="service" id="dunglas_doctrine_json_odm.normalizer.object" />
</argument>

<argument type="collection">
Expand Down
33 changes: 33 additions & 0 deletions tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,37 @@ public function testNullIsStoredAsNull()

$this->assertNull($stmt->fetch()['attributes']);
}

public function testStoreAndRetrieveDocumentWithInstantiatedOtherSerializer()
{
/**
* This call is necessary to cover this issue.
*
* @see https://github.com/dunglas/doctrine-json-odm/pull/78
*/
$serializer = self::$kernel->getContainer()->get('serializer');

$attribute1 = new Attribute();
$attribute1->key = 'foo';
$attribute1->value = 'bar';

$attribute2 = new Attribute();
$attribute2->key = 'weights';
$attribute2->value = [34, 67];

$attributes = [$attribute1, $attribute2];

$product = new Product();
$product->name = 'My product';
$product->attributes = $attributes;

$manager = self::$kernel->getContainer()->get('doctrine')->getManagerForClass(Product::class);
$manager->persist($product);
$manager->flush();

$manager->clear();

$retrievedProduct = $manager->find(Product::class, $product->id);
$this->assertEquals($attributes, $retrievedProduct->attributes);
}
}

0 comments on commit aecbaae

Please sign in to comment.