Skip to content

Commit aecbaae

Browse files
CvekCodingToflar
authored andcommitted
Fixed serializer issue (#78)
1 parent 435a52f commit aecbaae

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ As an example we use the Symfony `DateTimeNormalizer` service so we do have supp
250250
dunglas_doctrine_json_odm.serializer:
251251
class: Dunglas\DoctrineJsonOdm\Serializer
252252
arguments:
253-
- ['@serializer.denormalizer.array', '@serializer.normalizer.datetime', '@serializer.normalizer.object']
253+
- ['@dunglas_doctrine_json_odm.normalizer.array', '@serializer.normalizer.datetime', '@dunglas_doctrine_json_odm.normalizer.object']
254254
- ['@serializer.encoder.json']
255255
public: true
256256
```

src/Bundle/Resources/config/services.xml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,20 @@
55
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
66

77
<services>
8+
<service id="dunglas_doctrine_json_odm.normalizer.object" class="Symfony\Component\Serializer\Normalizer\ObjectNormalizer" public="false">
9+
<argument type="service" id="serializer.mapping.class_metadata_factory" on-invalid="ignore" />
10+
<argument>null</argument><!-- name converter -->
11+
<argument type="service" id="serializer.property_accessor" />
12+
<argument type="service" id="property_info" on-invalid="ignore" />
13+
<argument type="service" id="serializer.mapping.class_discriminator_resolver" on-invalid="ignore" />
14+
</service>
15+
16+
<service id="dunglas_doctrine_json_odm.normalizer.array" class="Symfony\Component\Serializer\Normalizer\ArrayDenormalizer" public="false" />
17+
818
<service id="dunglas_doctrine_json_odm.serializer" class="Dunglas\DoctrineJsonOdm\Serializer" public="true">
919
<argument type="collection">
10-
<argument type="service" id="serializer.denormalizer.array" />
11-
<argument type="service" id="serializer.normalizer.object" />
20+
<argument type="service" id="dunglas_doctrine_json_odm.normalizer.array" />
21+
<argument type="service" id="dunglas_doctrine_json_odm.normalizer.object" />
1222
</argument>
1323

1424
<argument type="collection">

tests/FunctionalTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,37 @@ public function testNullIsStoredAsNull()
162162

163163
$this->assertNull($stmt->fetch()['attributes']);
164164
}
165+
166+
public function testStoreAndRetrieveDocumentWithInstantiatedOtherSerializer()
167+
{
168+
/**
169+
* This call is necessary to cover this issue.
170+
*
171+
* @see https://github.com/dunglas/doctrine-json-odm/pull/78
172+
*/
173+
$serializer = self::$kernel->getContainer()->get('serializer');
174+
175+
$attribute1 = new Attribute();
176+
$attribute1->key = 'foo';
177+
$attribute1->value = 'bar';
178+
179+
$attribute2 = new Attribute();
180+
$attribute2->key = 'weights';
181+
$attribute2->value = [34, 67];
182+
183+
$attributes = [$attribute1, $attribute2];
184+
185+
$product = new Product();
186+
$product->name = 'My product';
187+
$product->attributes = $attributes;
188+
189+
$manager = self::$kernel->getContainer()->get('doctrine')->getManagerForClass(Product::class);
190+
$manager->persist($product);
191+
$manager->flush();
192+
193+
$manager->clear();
194+
195+
$retrievedProduct = $manager->find(Product::class, $product->id);
196+
$this->assertEquals($attributes, $retrievedProduct->attributes);
197+
}
165198
}

0 commit comments

Comments
 (0)