Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Serializer] Add COLLECT_EXTRA_ATTRIBUTES_ERRORS doc#16872

Open
NorthBlue333 wants to merge 1 commit into
symfony:6.2from
NorthBlue333:feature/ticket_46283_ticket_46284
Open

[Serializer] Add COLLECT_EXTRA_ATTRIBUTES_ERRORS doc#16872
NorthBlue333 wants to merge 1 commit into
symfony:6.2from
NorthBlue333:feature/ticket_46283_ticket_46284

Conversation

@NorthBlue333
Copy link
Copy Markdown

@NorthBlue333 NorthBlue333 commented Jun 12, 2022

@NorthBlue333 NorthBlue333 force-pushed the feature/ticket_46283_ticket_46284 branch from 1fc5d75 to 3ff28ab Compare June 13, 2022 06:58
@wouterj wouterj added the Waiting Code Merge Docs for features pending to be merged label Sep 20, 2022
@carsonbot carsonbot modified the milestones: 6.2, next Sep 20, 2022
@carsonbot carsonbot changed the title Add COLLECT_EXTRA_ATTRIBUTES_ERRORS doc [Serializer] Add COLLECT_EXTRA_ATTRIBUTES_ERRORS doc Dec 7, 2024
nicolas-grekas added a commit to symfony/symfony that referenced this pull request Apr 30, 2026
… full deserialization path (NorthBlue333)

This PR was merged into the 8.1 branch.

Discussion
----------

[Serializer] Add `COLLECT_EXTRA_ATTRIBUTES_ERRORS` and full deserialization path

| Q             | A
| ------------- | ---
| Branch?       | 8.1
| Bug fix?      | yes/no
| New feature?  | yes
| Deprecations? | yes
| Tickets       | Fix #46283, Fix #46284
| License       | MIT
| Doc PR        | symfony/symfony-docs#16872

The `PartialDenormalizationException` is used as unexpected extra attributes are, IMO, part of the denormalization exception.
This enables executing:

```php
  try {
       $dto = $serializer->deserialize($request->getContent(), MyDto::class, 'json', [
            DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true,
            DenormalizerInterface::COLLECT_EXTRA_ATTRIBUTES_ERRORS => true,
            DenormalizerInterface::ALLOW_EXTRA_ATTRIBUTES => false,
        ]);
    } catch (PartialDenormalizationException $e) {
        $violations = new ConstraintViolationList();
        /** `@var` NotNormalizableValueException */
        foreach ($e->getErrors() as $exception) {
            $message = sprintf('The type must be one of "%s" ("%s" given).', implode(', ', $exception->getExpectedTypes()), $exception->getCurrentType());
            $parameters = [];
            if ($exception->canUseMessageForUser()) {
                $parameters['hint'] = $exception->getMessage();
            }
            $violations->add(new ConstraintViolation($message, '', $parameters, null, $exception->getPath(), null));
        };
        if (null !== $extraAttributesException = $e->getExtraAttributesError()) {
            foreach ($extraAttributesException->getExtraAttributes() as $extraAttribute) {
                $violations->add(new ConstraintViolation('This attribute is not allowed.', '', [], null, $extraAttribute, null));
            };
        }

        return $this->json($violations, 400);
    }
```

Commits
-------

205dd70 Add COLLECT_EXTRA_ATTRIBUTES_ERRORS and full deserialization path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Serializer Status: Needs Review Waiting Code Merge Docs for features pending to be merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants