Skip to content

denormalization does not ignore type enforcement for not nullable relationship fields. #4250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
felipyamorim opened this issue Apr 26, 2021 · 9 comments

Comments

@felipyamorim
Copy link

API Platform version(s) affected: 2.6.4

Description

When i use the this config, the option "disable_type_enforcement" does not work for not nullable relationship fields.

Possible related issue: api-platform/api-platform#1786

denormalization_context:
    disable_type_enforcement: true

How to reproduce

    /**
     * @Assert\NotBlank()
     * @ORM\ManyToOne(targetEntity=Foo::class)
     * @ORM\JoinColumn(nullable=false)
     */
    private $foo;

Send null value for the field in a post operation.

Possible Solution
Allow denormalization process ignore type enforcement even when field is not nullable, like this:

        if (null === $value && ($type->isNullable() || $context[static::DISABLE_TYPE_ENFORCEMENT] ?? false)) {
            return $value;
        }

Additional Context

@guilliamxavier
Copy link
Contributor

From #1957, API Platform's behavior is supposed to match Symfony's. What happens when using the Symfony Serializer directly? (i.e. something like $symfonySerializer->denormalize(['foo' => null], MainEntity::class))

@felipyamorim
Copy link
Author

I tested using this code, and it worked as expected, accepted null value for not nullable relationship fields.

        $normalizer = new ObjectNormalizer(new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())));
        $serializer = new Serializer([$normalizer]);

        $idioma = $serializer->denormalize(
            ['tipoIdioma' => null],
            Idioma::class,
            null,
            ['groups' => ['write']]
        );

@guilliamxavier
Copy link
Contributor

guilliamxavier commented Apr 27, 2021

I forgot in my "something like", obviously you should add 'disable_type_enforcement' => true to the context (4th arg to denormalize()). But you say it works even without, so the problem may be elsewhere...

Anyway it appears now that you're probably not giving enough information: your entity snippet is too incomplete (getter/setter? ApiResource annotation?...), and most importantly, what precise error do you get?

@felipyamorim
Copy link
Author

felipyamorim commented Apr 27, 2021

Sorry, my mistake, i'll exemplify better.

When i send a single post request with the property "tipoIdioma" with null value, the api give me the following error: "Expected IRI or nested document for attribute "tipoIdioma", "NULL" given."

/**
 * @ApiResource(
 *     attributes={"pagination_client_items_per_page"=true},
 *     normalizationContext={"groups"={"read"}},
 *     denormalizationContext={"groups"={"write"}, "disable_type_enforcement"=true}
 * )
 * @ApiFilter(OrderFilter::class, properties={"tipoIdioma.dsTipoIdioma"}, arguments={"orderParameterName": "order"})
 * @ORM\Entity(repositoryClass=IdiomaRepository::class)
 * @ORM\Table(name="tb_idioma")
 */
class Idioma
{
	
    /**
     * @Groups({"read", "write"})
     * @Assert\NotBlank()
     * @ORM\ManyToOne(targetEntity=TipoIdioma::class)
     * @ORM\JoinColumn(nullable=false)
     */
    private $tipoIdioma;
    
    public function getTipoIdioma(): ?TipoIdioma
    {
        return $this->tipoIdioma;
    }

    public function setTipoIdioma(?TipoIdioma $tipoIdioma): self
    {
        $this->tipoIdioma = $tipoIdioma;

        return $this;
    }    
}

@felipyamorim
Copy link
Author

Someone ? Is that a bug?

@guilliamxavier
Copy link
Contributor

Just noticed your @ApiResource denormalizationContext "disable_type_enforcement" is false (not true), typo?

Anyway, there are many open issues, maybe you will get more attention if you provide a failing test (PHPUnit or Behat) and propose a fix (PR) (no guarantee but if you have some time...)

@felipyamorim
Copy link
Author

Yes, my mistake, I already corrected the post.

I had found a workaround using service decorator, but I lost the issue link :/

@stale
Copy link

stale bot commented Nov 5, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Nov 5, 2022
@stale
Copy link

stale bot commented Jan 4, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants