jsonmapper icon indicating copy to clipboard operation
jsonmapper copied to clipboard

Fix mapping objects implementing ArrayAccess (not collections)

Open daniser opened this issue 1 year ago • 3 comments

This PR fixes issue introduced by PR #197.

PR #197 replaced ArrayObject check with more broad ArrayAccess, thus breaking BC. This PR added additional check for Traversable to make sure given item is a collection and not a simple array-like object. Every collection implementation I know implements either Iterator or IteratorAggregate (both are Traversable descendants). Moreover, sample collection from original issue #175 implements Iterator too, so I can state with great confidence that this PR won't introduce more BC breaks.

daniser avatar Jan 30 '24 22:01 daniser

Thank you for the patch. This is indeed a BC break that I did not think about. Given that the release that introduced it is already 10 months old, I will not publish it in a patch level release: This patch does introduce a BC break for people that do indeed rely on the crude/broken ArrayAccess behavior. It will be released as 5.0.0.

A question - do we need ArrayAccess at all, isn't Traversable enough?

cweiske avatar Jan 31 '24 06:01 cweiske

We can add boolean flag to control behaviour and keep this feature in v4.0. What do you think?

ArrayAccess is indeed needed, as mapArray uses it to set values. Traversable is just a flag to differ collections from array-like objects.

P.S. Just noticed we may apply same treatment for nested arrays/collections (if I got this code right): https://github.com/cweiske/jsonmapper/blob/132c75c7dd83e45353ebb9c6c9f591952995bbf0/src/JsonMapper.php#L476

daniser avatar Jan 31 '24 08:01 daniser

@cweiske Any progress on this?

daniser avatar Mar 19 '24 12:03 daniser

@daniser I rebased against current master and fixed all the conflicts that appeared because #232 was merged before. It is merged now, with a lengthy explanation about the reasoning.

Thanks for the patch.

cweiske avatar May 17 '24 16:05 cweiske

Released with v5.0.0.

cweiske avatar Sep 08 '24 10:09 cweiske