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 9 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

mabar commented 1 week ago

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

@mrceperka It will be probably handled again with proper data types support, but in the meanwhile I think we could add some internal debug info. Also I am planning to add invalid value sampling to the printers, but object support will be quite limited, to prevent exposing implementation details via APIs (probably will just say it is an object, no matter of which type)

mabar commented 1 week ago

As mentioned in https://github.com/orisai/object-mapper/issues/73 data types support may include rules for mapping specific data types (JsonRule, XmlRule) to allow decoding inner jsons and perhaps even mapping them.

For both data type rules and manual decoding in callbacks there should be a way to mark the data type in Type interface so printer could properly disclose that the property is encoded as json and not a part of the outer structure.