-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] Ability to add custom annotations #38993
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
Comments
Regarding the second use-case with dates, I think a class Foo
{
/**
* @var \DateTime
*
* @Serializer\Context({
* DateTimeNormalizer::FORMAT_KEY = \DateTime:: RFC3339
* })
*/
public $date;
} The annotation could accept a |
To me that still feels like it's leaking the |
Sure, it does not aim to solve your custom annotations feature request, which is requiring you to write some custom code. But it solves part of the described use-case and many others, by providing out-of-the-box a way to configure existing (de)normalizers on a per-case-basis. The context keys of these normalizers are part of their API, so it's not really "leaking implementation"; it provides a flexible way to configure it. |
I don't disagree that it would be beneficial to be able to configure context on a per-field basis; I just didn't want my feature request to get lost to this new idea. Do you want me to create a new feature request for the Context annotation? Or would you be okay doing that yourself since it was your idea? |
Sure, but you could build a custom annotation on top of that: #[\Attribute]
class DateFormat extends Context
{
public function __construct(string $format)
{
parent::__construct([
DateTimeNormalizer::FORMAT_KEY => $format
]);
}
} |
Thank you for this suggestion. |
Please keep this open |
Description
Similar how we can add custom validator annotations (https://symfony.com/doc/current/validation/custom_constraint.html), it would be beneficial to be able to create custom serialization annotations to augment existing functionality on a per-property/method basis.
Right now,
Groups
,SerializedName
, andIgnore
are all hard-coded into the serialization process via theAttributeMetadaInterface
,AnnotationLoader
, and theClassMetadataInterface
. There is no easy or intuitive way to create and use custom annotations currently that I have found (If I'm mistaken, I'd love to hear more).Example
Two use cases:
@Encrypt()
annotation that can decorate the properties inside of the entities themselves and automatically encrypt - similar to how@Ignore
or@SerializedName
works.DateTime
instances, there are some circumstances where we need to arbitrarily send back a date in a different format (e.g., just a date or a date with a time or a date/time with timezone information).Thank you.
The text was updated successfully, but these errors were encountered: