oroinc / platform

Main OroPlatform package with core functionality.
Other
628 stars 351 forks source link

Problem denormalizing 2nd level children file entities. Issue with ConfigurableEntityNormalizer. #718

Open dimonixx opened 7 years ago

dimonixx commented 7 years ago
class ObjectA {
    protected $id;
    protected $name;
    /**
     * @ORM\ManyToMany(targetEntity="AssetFile",indexBy="id")
         * @ORM\JoinTable(
         *     name="object_a_assets",
         *     joinColumns={
         * @ORM\JoinColumn(name="object_a_id",referencedColumnName="id",onDelete="CASCADE")
         *     },
         *     inverseJoinColumns={
         * @ORM\JoinColumn(name="asset_file_id",referencedColumnName="id",onDelete="CASCADE")
         *     }
         * )
         */
    protected $assetFiles;
}

The following class has relation to ORO\Bundle\AttachmentBundle\Entity\File created with the AttachmentExtension in a migration.

class AssetFile {

    protected $id;

    protected $description;
}

Denormalizing data for an object of type ObjectA with ConfigurableEntityNormalizer fails, when it comes to denormalization of the Oro\Bundle\AttachmentBundle\Entity\File.

It is done with FileNormalizer in:

/**
     * {@inheritdoc}
     */
    public function denormalize($data, $class, $format = null, array $context = [])
    {
        $result = null;
        $entity = $this->fileManager->createFileEntity($data);
        if ($entity) {
            $violations = $this->validator->validate(
                $entity->getFile(),
                $context['entityName'],
                $context['fieldName']
            );
            if (!$violations->count()) {
                $result = $entity;
            }
        }

        return $result;
    }

The $context['entityName'] in this case will be ObjectA and validation will fail.
The $context['entityName'] should have value AssetFile.

In the denormalize method of ConfigurableEntityNormalizer at line 80 $context is being updated as follows:

$context = array_merge($context, ['fieldName' => $fieldName]);

so $context['entityName'] is not being updated before denormalizing next $value

$value is instance of Symfony\Component\HttpFoundation\File\File $entityClass = Oro\Bundles\AttachmentBundle\Entity\File

$value = $this->serializer->denormalize($value, $entityClass, $format, $context);
mymias commented 7 years ago

@dimonixx , thank you for reporting the issue.

We've created an task in our backlog (BAP-14978, I'm providing it here for internal usage) to fix this issue.

Thank you again for your contribution!

Best regards.