Skip to content
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

DISABLE_TYPE_ENFORCEMENT is ignored for non-scalar properties #5584

Closed
emomaliev opened this issue May 6, 2023 · 3 comments
Closed

DISABLE_TYPE_ENFORCEMENT is ignored for non-scalar properties #5584

emomaliev opened this issue May 6, 2023 · 3 comments
Labels

Comments

@emomaliev
Copy link

emomaliev commented May 6, 2023

I'm trying to disable type enforcement so I don't get type errors on nullable=false properties.
Instead, I want to see error messages from Assert\NotBlank, Assert\NotNull, etc.

#[ApiResource(
    operations: [
        new Post(
            denormalizationContext: [
                'disable_type_enforcement' => true,
                'collect_denormalization_errors' => true
            ],
        ),
    ],
 )]
class Entity
{
 
    #[ORM\ManyToOne]
    #[ORM\JoinColumn(nullable: false)]
    #[Assert\NotBlank()]
    private ?Brand $brand = null;

    public function getBrand(): ?Brand
    {
        return $this->brand;
    }
    
   public function setBrand(?Brand $brand): void
    {
        $this->brand = $brand;
    }

}

My request:

POST: 
{
  brand: null
}

Actual answer:

{
   "violations":[
      {
         "propertyPath":"brand",
         "message":"This value should be of type array|string.",
         "code":"0"
      }
}

Expected response:

{
   "violations":[
      {
         "propertyPath":"brand",
         "message":"This value should not be blank.",
         "code":"0"
      }
}

As you can see from the actual answer DISABLE_TYPE_ENFORCEMENT does not work for any objects. I have tested it for relations and enum classes (Works for only scalar types).

After researching the problem, I found that in properties with the Object type, we simply do not get to this condition.

if ($context[static::DISABLE_TYPE_ENFORCEMENT] ?? false) {
return $value;
}

What do you think about this? Maybe this was expected behavior for you?

This issue has also been mentioned here: #1786

@felipyamorim
Copy link

felipyamorim commented May 9, 2023

I made a workaround using composer to replace a snippet of code from the AbstractItemNormalizer class.

    "scripts": {
        "post-autoload-dump": [
            "sed -i 's/null === \\$value && \\$type->isNullable()/null === \\$value \\&\\& (\\$type->isNullable() \\|\\| \\$context[static::DISABLE_TYPE_ENFORCEMENT] \\?\\? false)/g' vendor/api-platform/core/src/Serializer/AbstractItemNormalizer.php"
        ],
    }

Same solution as suggested in a previous issue #4250

It's not ideal, but it fixes the problem temporarily. Soon I will make a pull request.

@soyuka
Copy link
Member

soyuka commented May 22, 2023

can you try my patch at #5593 ?

@felipyamorim
Copy link

@soyuka thank you for the fix.

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

No branches or pull requests

3 participants