-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Validator] Allow using attributes to declare compile-time constraint metadata #61528
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
[Validator] Allow using attributes to declare compile-time constraint metadata #61528
Conversation
289783f
to
8be45c5
Compare
defd051
to
ea317e8
Compare
d3490c8
to
9d08608
Compare
src/Symfony/Component/Validator/DependencyInjection/AttributeMetadataPass.php
Show resolved
Hide resolved
3dc6ebf
to
35c205e
Compare
src/Symfony/Component/Form/Extension/Validator/Constraints/Form.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Validator/DependencyInjection/AttributeMetadataPass.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Validator/DependencyInjection/AttributeMetadataPass.php
Show resolved
Hide resolved
325c0c3
to
cd349b8
Compare
src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
Show resolved
Hide resolved
@@ -27,6 +27,7 @@ | |||
* | |||
* @author Bernhard Schussek <bschussek@gmail.com> | |||
*/ | |||
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)] |
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.
Why adding this here ? The abstract class cannot be used as an attribute. And child classes defining concrete constraints will have to specify the proper target anyway.
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.
It's required by the engine in relation to registerAttributeForAutoconfiguration
ca89d23
to
dbe819b
Compare
dbe819b
to
0197b50
Compare
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
…-time serialization metadata (nicolas-grekas) This PR was merged into the 7.4 branch. Discussion ---------- [Serializer] Allow using attributes to declare compile-time serialization metadata | Q | A | ------------- | --- | Branch? | 7.4 | Bug fix? | no | New feature? | yes | Deprecations? | yes | Issues | - | License | MIT Very close to #61528 Prerequisite for #61287 At the moment, serialization attributes are read at runtime when `framework.serialization.enable_attributes` is true. This means they don't fit for bundles nor can't they be warmed up. This PR fixes both issues by using a new `serializer.attribute_metadata` resource tag, that's turned into a list of classes to parse for attributes at compile-time. For bundles and for apps, the tag is added by explicit service configuration: ```php ->set('my_bundle.api_resource.product', Product::class) ->resourceTag('serializer.attribute_metadata') ``` Unlike validation where we have constraint attributes to auto-discover service resources, serialization doesn't have any corresponding hooks. We do have a few like `#[DiscriminatorMap]` of `#[Groups]`, but relying on those would miss many more classes that are meant for serialization. Maybe we could introduce an attribute that'd hint that some class is serializable by the component (and require the attribute at some point in the future?) Commits ------- bc6e054 [Serializer] Allow using attributes to declare compile-time serialization metadata
Prerequisite for #61288
At the moment, validation attributes are read at runtime when
framework.validation.enable_attributes
is true.This means they don't fit for bundles nor can't they be warmed up.
This PR fixes both issues by using a new
validator.attribute_metadata
resource tag, that's turned into a list of classes to parse for attributes at compile-time.For apps, the tag is added by autoconfiguration: any
Constraint
-derived attributes found on a class in thesrc/
folder will trigger the rule to add the tag.For bundles (and for apps if they want to), the tag is added by explicit service configuration. In an "eat your own dog-food" spirit, this capability is used to declare the constraints of the
Form
class: instead of loading thevalidation.xml
file, we now declare this service resource:This reads the attributes added to the
Form
class:Bundles can do the same and replace their XML files by attributes.
As a next step, we could also deprecate runtime-discovery of attributes. This could be worth it if this discovery has a measurable performance impact. To be measured if one wants to dig this idea.
Side note: I'm hoping this could allow removing the yaml and xml config formats one day. For serialization metadata also (PR coming). BUT, this doesn't (yet) cover the use case of overriding metadata defined by bundles. For that, apps still have to use xml or yaml in config/validation/. I have an idea to cover this, coming to a next PR if it works.
(failures unrelated)