-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] Add flag to require all properties to be listed in the input #49553
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
[Serializer] Add flag to require all properties to be listed in the input #49553
Conversation
Hey! I see that this is your first PR. That is great! Welcome! Symfony has a contribution guide which I suggest you to read. In short:
Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change. When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor! I am going to sit back now and wait for the reviews. Cheers! Carsonbot |
I don't think the last failed test run had anything to do with this PR. The first time the unit tests run through (only the Psalm run was cancelled) and the only change made afterwards was in the CHANGELOG.md. |
@dunglas The history says that I removed a request for you to review the PR. I don' think I did any I don't even think it's possible for me to do so. In any way I can't request a new one. Is this part of the normal process? Sorry, for the stupid questions, it's my first contribution. |
@maxbeckers @dunglas This is my first PR. The status is now reviewed. What is left to do for the PR to be merged? Anything I can help with? 🙂 |
@christian-kolb no todos for you ... will be reviewed and merged as soon as possible |
src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php
Outdated
Show resolved
Hide resolved
@mtarld @maxbeckers Sorry to bother you guys. I'm getting worried about whether this pull request is getting merged as the 6.3 Milestone is very close now. Who is responsible for merging the PRs and will do this? 🙂 |
i tend to believe for this signature: public function __construct(
public string $name,
public ?int $costsInCent,
) {
}
to me it hints a what should be doable is detecting uninitialized (aka not given) without using constructor promotion:
in this case you could detect "given" properties from initialized properties, thus |
@ro0NL Sorry, I don't think I understand what you want to say.
That's what would still work out of the box with for example the Property Normalizer. The only thing changing through the PR is that you can supply an additional context to configure that you expect |
pesonally i dont see any value in enforcing |
The value is the option to prevent bugs. We have a class with the property This could be covered by more tests, more validation, ... but all of those are then just replacements for the missing control I would also already have with typing. Personally I think the current behaviour is to loose. There is a concept for missing properties and that's optional values. That could also be typed like In the end the discussion about this is the same as with using typing or not. I have had exactly the problems described here multiple times in multiple projects. So this is just a way to eliminate one bug category 🙂 |
Right! What about something similar like forms do: https://symfony.com/doc/current/reference/forms/types/form.html#allow-extra-fields |
That's an awesome idea. That would fix the second part of my example I didn't even think to solve yet. The two problems in my example are:
There is a value for a property expected, that isn't supplied This could happen through:
It would be more likely to catch those issues when an endpoint is only used in one place, but it's possible that it's used in for example 5 places in the client but only adapted in 4 of them. Which becomes more relevant, when we're not talking about properties on the first level, but the second or third what we might have with value objects (I'm using those a lot). There is an additional value supplied, that is not needed on the current object level This could happen through:
And we must not forget that the serializer is not just used for client / API combinations. I for example use it also as part of custom doctrine types to store and retrieve value objects in JSON. Or to return read models directly from custom queries (again with multiple levels of object structures) or to dump and retrieve aggregates in an event sourcing context for snapshotting. This pull requests solves the first of the described problems. I'm not sure if the Serializer structure allowed for an |
@chalasr @nicolas-grekas Sorry to bother you. It's my first contribution to Symfony and I don't know how to get this merged. I'm hanging here for two months. Could you please help me here or point me to someone who can? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The implementation looks good to me. I'm not sure about the name of the option though. Maybe FORCE_PASS_ALL_PROPS
? Or NO_DEFAULT_VALUES
?
@dunglas Thanks for the additional approval. I'm not 100% about the naming myself. But I don't think your suggestions are better. My thoughts where:
It would be way easier with the nullable fallback being the default 🙂 What just came to mind: Would it be reasonable to switch the logic. So that the default of the context is I really don't want to make it miss the 6.3 milestone because of it. Anyone around who could bless that change? |
@dunglas Ok, sorry scratch that. The flags have to be provided specifically and there is not a single context flag that works like that. So that's no way ether. Still happy to hear other namings 🙂 Otherwise very happy when it would get merged 🙂 |
I'd go with |
@mtarld That's unfortunately not possible. That's what I meant in my last comment. |
REQUIRE_ALL_PROPERTIES? |
Sounds great yes |
@nicolas-grekas @mtarld Ok, then I will change it to |
@nicolas-grekas Updated it to Anything left for me to do to get it merged and released with 6.3? 🙂 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, just minor things
src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Serializer/Context/Normalizer/AbstractNormalizerContextBuilder.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Serializer/Context/Normalizer/AbstractNormalizerContextBuilder.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php
Show resolved
Hide resolved
4aab172
to
d62410a
Compare
Thank you @christian-kolb. |
…izer::REQUIRE_ALL_PROPERTIES` context option (Christian Kolb, christian-kolb) This PR was merged into the 6.3 branch. Discussion ---------- [Serializer] Add feature description for `AbstractNormalizer::REQUIRE_ALL_PROPERTIES` context option This PR adds the feature description for the new context flag for the Serializer component introduced here: symfony/symfony#49553 Commits ------- 71a4ef4 Rename context flag ea5c341 Update components/serializer.rst 3b24418 Update components/serializer.rst e1f5d7c Update components/serializer.rst b665eeb Update components/serializer.rst 0b49d9b Fix broken code example 1b0e274 Add feature description
The PR #40522 introduced a fallback for nullable properties to be set to
null
when they aren't provided as parameters.A drawback of that approach is that it easier for bugs to appear through typos or renamings of those properties. I think the current implementation makes perfect sense as a default. Therefore, this PR introduces a new context flag that prevents that fallback behaviour. This way nothing changes for existing systems, but for people wanting more control, it's possible to set a flag.
Example