serializer icon indicating copy to clipboard operation
serializer copied to clipboard

@Inline annotation prevent serialization of scalar properties

Open stayeronglass opened this issue 5 years ago • 7 comments

Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no

SerializationVisitor simply skip scalars. why?

https://github.com/schmittjoh/serializer/blob/0edb562707c032156516fe94d27d0f685032573f/src/JsonSerializationVisitor.php#L146

stayeronglass avatar Sep 16 '20 10:09 stayeronglass

@Inline is meant to "inline" an array as if the elements were props of the current object.

Using @inline on a scalar makes no sense IMO.

goetas avatar Sep 16 '20 18:09 goetas

serializer should not silently skip property. throw an exception or rise an error.

stayeronglass avatar Sep 17 '20 06:09 stayeronglass

It would be great if you could provide a fix for this DX issue. I think that a good place to start is https://github.com/schmittjoh/serializer/blob/0edb562707c032156516fe94d27d0f685032573f/src/Metadata/Driver/AnnotationDriver.php#L272

you could add a check that verifies if the prop is marked as inline but is not an array (or arrayCollection)

goetas avatar Sep 17 '20 09:09 goetas

Annotation Driver cannot handle custom types.

stayeronglass avatar Sep 18 '20 08:09 stayeronglass

let me explain the situation.

if u have custom type, u must use @Inline annotaion. if in the end u have scalar u must not use @Inline annotation.

for now i wrote

public function serializeToJson(JsonSerializationVisitor $visitor, $data, array $type, Context $context)
    {
        if (('NestedArray' === $type['name']) && is_scalar($data)) {

            foreach ($context->getMetadataStack() as $metadata) {
                if (($metadata instanceof PropertyMetadata) && ($type['name'] === $metadata->type['name'])) {

                    $metadata->inline = false;
                    break;
                }
            }
        }

        return $data;
    }

stayeronglass avatar Sep 18 '20 08:09 stayeronglass

I have the same problem. The class is this: https://github.com/ujamii/openimmo/blob/master/src/API/DistanzenSport.php and the structure is given by the XML we get: <distanzen_sport distanz_zu_sport="SEE">15.0</distanzen_sport>.

Now, when serializing this to XML, everything is fine, but the inline scalar value is missing. I did not get the proposed solution from above to work, I tried this registration:

/**
     * @return array|array[]
     */
    public static function getSubscribingMethods()
    {
        return [
            [
                'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
                'format'    => 'json',
                'type'      => DistanzenSport::class,
                'method'    => 'serializeToJson',
            ]
        ];
    }

I did not find any documentation for the type property except for the DateTime example. What value is expected here, if it for a certain property of a certain class only? ("float" did not work either)

mgrundkoetter avatar Jun 30 '21 08:06 mgrundkoetter

I use the https://symfony.com/doc/current/components/serializer.html now, this works as expected with my objects.

mgrundkoetter avatar Jun 30 '21 11:06 mgrundkoetter