Skip to content

ConstraintViolationListNormalizer is overriding validation error for non ApiPlatform routes #6216

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
bbarhoum1 opened this issue Mar 13, 2024 · 1 comment

Comments

@bbarhoum1
Copy link

bbarhoum1 commented Mar 13, 2024

API Platform version(s) affected: 3.2.15

Description

ConstraintViolationListNormalizer is overriding validation error for non ApiPlateform route:
I get this response:

{
    "type": "https://tools.ietf.org/html/rfc2616#section-10",
    "title": "An error occurred",
    "status": 422,
    "detail": "Cette valeur doit être de type unknown.",
    "0": {
        "propertyPath": "",
        "message": "Cette valeur doit être de type unknown.",
        "code": null,
        "hint": "Failed to create object because the class misses the \"quantity\" property."
    }
}

Instead of:

{
    "type": "https://tools.ietf.org/html/rfc2616#section-10",
    "title": "An error occurred",
    "status": 422,
    "detail": "Cette valeur doit être de type unknown.",
    "violations": [
        {
            "propertyPath": "",
            "message": "Cette valeur doit être de type unknown.",
            "code": null,
            "hint": "Failed to create object because the class misses the \"quantity\" property."
        }
    ]
}

How to reproduce

https://github.com/bbarhoum1/api-platform-validation-error

Possible Solution

public function normalize(mixed $object, ?string $format = null, array $context = []): array
    {
        [$messages, $violations] = $this->getMessagesAndViolations($object);

        if ($context['rfc_7807_compliant_errors'] ?? false) {
            return $violations;
        }

        return ['violations' => $violations];
    }

Thank you for your help :)

@bbarhoum1
Copy link
Author

Thank you @soyuka for your help. It's working.

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

No branches or pull requests

1 participant