orisai / object-mapper

Raw data mapping to validated objects
Mozilla Public License 2.0
10 stars 1 forks source link

Handling stdClass and other datasource objects #67

Open mabar opened 1 year ago

mabar commented 1 year ago

Description

Currently if processor is given an output from json_decode($value), an stdClass is casted to array in case it's in place of a MappedObjectValue. But stdClass makes sense to handle also in case of an ArrayOf and a ListOf.

Handling should be also available in Value in case printer decides to print an invalid value.

We should also consider that fix on user side is as simple as json_decode($value, true) and that it may work only for some formats:

Addition information

No response

Related issues

No response

mabar commented 1 year ago

jms/serializer has Xml* attributes for that - see https://jmsyst.com/libs/serializer/master/reference/annotations

use JMS\Serializer\Annotation\XmlAttribute;
use JMS\Serializer\Annotation\XmlValue;
use JMS\Serializer\Annotation\XmlRoot;

/** @XmlRoot("price") */
class Price
{
    /** @XmlAttribute */
    private $currency = 'EUR';

    /** @XmlValue */
    private $amount = 1.23;
}

XmlValue should probably be default

mabar commented 1 year ago

symfony/serializer does something similar too, but the exact format should be mainly declared by the object and not in global config https://symfony.com/doc/current/components/serializer.html#the-xmlencoder

mabar commented 1 year ago

For now I removed support for stdClass from processor. Proper support for various formats will be implemented later https://github.com/orisai/object-mapper/commit/5edbc1adbf93cd1794cea90c1f2b2a61ae929ab0

mrceperka commented 7 months ago

TIL that any stdClass in $data will cause validation to fail. But it will not tell you where it failed.

Most like this to blame: https://github.com/orisai/object-mapper/blob/e974721d8c8d6008ae811c107bd8e7db942fe2fa/src/Processing/DefaultProcessor.php#L220

I might add a test case for this and we might also want to add error to type when this happens. https://github.com/orisai/object-mapper/blob/e974721d8c8d6008ae811c107bd8e7db942fe2fa/src/Processing/DefaultProcessor.php#L224