Skip to content

[Serializer] Fix collecting only first missing constructor argument #51907

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

Conversation

HypeMC
Copy link
Contributor

@HypeMC HypeMC commented Oct 9, 2023

Q A
Branch? 5.4
Bug fix? yes
New feature? no
Deprecations? no
Tickets -
License MIT

Alternative to #51866, sort of followup to #49832

Currently on 5.4 only the first exception is added to the not_normalizable_value_exceptions array when COLLECT_DENORMALIZATION_ERRORS is true or only the first argument is mentioned in the MissingConstructorArgumentsException when it is false.
On 6.3 however, the part with the MissingConstructorArgumentsException was fix with #49832, but the part with the not_normalizable_value_exceptions was overlooked.
IMO this is inconsistent behavior as the two cases are actually the same thing with the only difference being that in one case an exception is thrown while in the other the errors are collected.

I'm not sure if #51866 really qualifies as a bug or is actually more a feature, but the reason #49832 was merged onto 6.3 was because of the changes originally done in #49013, which itself was a feature.

If #51866 does qualify as a bug then it would make sense to backport #49832 to 5.4 for consistency, which is what this PR does.

The PR contains two commits:

  1. backport of [Serializer] Fix MissingConstructorArgumentsException returning missing argument one by one #49832
  2. alternative to add all missing properties #51866

If #51866 does not qualify as a bug, the first commit can be drooped and the second one can be rebased with 6.4.

PS I think it's easier to review the changes commit by commit.

@y4roc
Copy link

y4roc commented Oct 9, 2023

Your solution looks great, but I see a conflict with RequestMapper.

Do you tested your solution with PayloadMapper in 6.3?

@HypeMC HypeMC force-pushed the fix-collecting-only-first-missing-constructor-argument branch 2 times, most recently from 258ae0f to cd90ab7 Compare October 10, 2023 01:38
@HypeMC
Copy link
Contributor Author

HypeMC commented Oct 10, 2023

@y4roc Hi, you were right, my changes did cause an issue with the RequestPayloadValueResolver because the functionality used there was not covered on 5.4.
I've added a test (testCollectDenormalizationErrorsWithInvalidConstructorTypes) so this wouldn't be a problem in the future. I've also tested my changes on 6.3 to check that there are no other failing tests.

@@ -412,24 +413,26 @@ protected function instantiateObject(array &$data, string $class, array &$contex
true
);
$context['not_normalizable_value_exceptions'][] = $exception;

return $reflectionClass->newInstanceWithoutConstructor();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now handled in the try...catch block.

@nicolas-grekas nicolas-grekas force-pushed the fix-collecting-only-first-missing-constructor-argument branch from cd90ab7 to 0f398ce Compare October 17, 2023 06:59
@nicolas-grekas
Copy link
Member

Thank you @HypeMC.

@nicolas-grekas nicolas-grekas merged commit bccdccc into symfony:5.4 Oct 17, 2023
@HypeMC HypeMC deleted the fix-collecting-only-first-missing-constructor-argument branch October 17, 2023 07:12
@fabpot fabpot mentioned this pull request Oct 21, 2023
@fabpot fabpot mentioned this pull request Oct 29, 2023
nicolas-grekas added a commit that referenced this pull request Nov 20, 2023
This PR was merged into the 5.4 branch.

Discussion
----------

[Serializer] Fix denormalize constructor arguments

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #52499, Fix #52422
| License       | MIT

Since this PR: #51907, objects with partial constructor parameters were wrongly instantiated.

This PR fixes that issue by delegating the properties values assignment, by unsetting normalized data only when the constructor has been called properly.

This might correct #50759 as well.

Commits
-------

8f7c7ae [Serializer] Fix denormalize constructor arguments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants