Skip to content

fix: work with normalized object data as with ArrayAccess - #150

Open
7-zete-7 wants to merge 1 commit into
dunglas:mainfrom
7-zete-7:fix/gh-149
Open

7-zete-7 wants to merge 1 commit into
dunglas:mainfrom
7-zete-7:fix/gh-149

Conversation

@7-zete-7

@7-zete-7 7-zete-7 commented Sep 9, 2025

Copy link
Copy Markdown

Fixes #149

These changes add support for ArrayObject as a result of object normalization and cover issue #149 with test.

@TamasSzigeti TamasSzigeti left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My test still fails at denormalization

Symfony\Component\Serializer\Exception\NotNormalizableValueException: Data expected to be "ArrayObject[]", "ArrayObject" given.
…
/app/vendor/dunglas/doctrine-json-odm/src/SerializerTrait.php:91

Comment thread tests/FunctionalTest.php
Comment thread tests/SerializerTest.php

@dunglas dunglas left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix. One concern: `$normalizedData[self::KEY_TYPE] = $typeName` moves `#type` from the first to the last key of every stored document. This changes the output for `json` columns (which preserve key order) and no longer matches the README examples.

Casting keeps the current shape and fixes the `ArrayObject` case, since the result always contains `#type` and is never empty:

```php
$valueData = is_scalar($normalizedData) ? [self::KEY_SCALAR => $normalizedData] : (array) $normalizedData;
```

I checked that `testEmptyArrayAsArrayObject` passes with this change. `testArrayObjectNormalization` would need to assert the `#type` key instead of the `ArrayObject` instance.

Minor: in `denormalize()`, `unset($data[self::KEY_TYPE])` mutates the caller's `ArrayObject`. Either drop the `ArrayObject` handling there (`json_decode` always returns arrays) or convert it first with `getArrayCopy()`.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Serializer decorator does not support ArrayObject as object normalization result

3 participants