0
votes

Using Api Platform, I have a problem using an input class and its transformation.

The following documentation has been followed. https://api-platform.com/docs/core/dto/#using-data-transfer-objects-dtos

After Data Transformer service executes transformation and returns an object of the correct class, the object that is picked up by api-platform appears to be empty, so it either fails validation, if validation is present, or persistence to the database fails - due its fields appear to be empty.

Here is a simplified code of DataTransformer service methods - it produces an object with hardcoded values:

public function transform($object, string $to, array $context = [])
{
    $newCreativeElement = new CreativeElement();
    $newCreativeElement->setKeyName("HARDCODED VALUE");
    $newCreativeElement->setIntValue(42);
    return $newCreativeElement;
}

public function supportsTransformation($object, string $to, array $context = []): bool
{
    if ($object instanceof CreativeElement){
        return false;
    }
    $result = CreativeElement::class === $to && null !== ($context['input']['class'] ?? null);
    return $result;
}
1

1 Answers

0
votes

Edit: It's solved by 2.4 release. Upgrade your composer.json and enjoy.

I have the same problem. Something i tried is return for an array instead of an object transform method. It's working but not real solution.

it's appear that denormalizer is called 2 times : once for your transformer, and after to transform "CreativeElement" into "CreativeElement" by AbstractItemNormalizer


        $context['api_denormalize'] = true;
        $context['resource_class'] = $class;
        $inputClass = $this->getInputClass($class, $context);

        if (null !== $inputClass && null !== $dataTransformer = $this->getDataTransformer($data, $class, $context)) {
            $data = $dataTransformer->transform(
                parent::denormalize($data, $inputClass, $format, ['resource_class' => $inputClass] + $context),
                $class,
                $context
            );
        }

        return parent::denormalize($data, $class, $format, $context);

Looking for solution too