-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpKernel] Create #[Serialize]
Attribute to serialize Controller Result
#49518
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
base: 7.4
Are you sure you want to change the base?
Conversation
#[SerializedResponse]
Attribute to serialize Controller Result#[SerializedResponse]
Attribute to serialize Controller Result
8e54594
to
9e16d8c
Compare
a30ee49
to
06087b9
Compare
#[SerializedResponse]
Attribute to serialize Controller Result#[Serialized]
Attribute to serialize Controller Result
#[Serialized]
Attribute to serialize Controller Result#[Serialize]
Attribute to serialize Controller Result
* Controller tag to serialize response. | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_METHOD)] | ||
class Serialize |
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.
indeed, others attribute are prefixed with As
when they describe more the config of the class (AsCommand or AsMessageHandler)
here it is like a mapping/transformation, perhaps like your other PR #49138 MapResponse
src/Symfony/Component/HttpKernel/EventListener/SerializeControllerResultListener.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/HttpKernel/EventListener/SerializeControllerResultListener.php
Show resolved
Hide resolved
src/Symfony/Component/HttpKernel/EventListener/SerializeControllerResultListener.php
Outdated
Show resolved
Hide resolved
9eea8e6
to
9d1fb32
Compare
This test is failing https://github.com/symfony/symfony/actions/runs/4272061115/jobs/7436960556#step:7:8239 You probably have to add |
yes, thanx, it was added in #49138. I've not added it here to prevent merge conflicts. Also I want extend functional tests added in mentioned PR with new Attribute |
9d1fb32
to
3213f4b
Compare
fbc4eae
to
ca0b60d
Compare
9845ae5
to
f350273
Compare
For what it's worth, I'm personally not convinced we should merge this. The reason is that the resulting behavior is very limited and resorting to explicit code comes very quickly - as soon as one needs more precise control over the resulting response for example. In the end, this saves two/three lines that are legit to have in a controller/similar to me. |
Exactly, this can save few lines in the Controller and reduce boilerplate. Of course it can't cover all needs for an every project, but same applicable for other stuff provided by Framework. 80/20 rule in action. We have few microservices that uses only We can go further and build OpenApi documentation based on Controller return type + Attribute existence via NelmioApiDocBundle. It will reduce even more boilerplate code for some projects. |
8ee1da3
to
9058a4b
Compare
9058a4b
to
ad86cea
Compare
Rebased, renamed @nicolas-grekas please review 🙏 |
ad86cea
to
3c9a9b6
Compare
3c9a9b6
to
490a4ad
Compare
I've rebased PR one more time + fixed tests + actualized changelog. And as I've already mentioned - this feature needed not only for reducing boilerplate of serialization but also for simplification of the OpenApi spec generation via NelmioApiDocBundle. They already successfully adopted |
* @author Konstantin Myakshin <[email protected]> | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_METHOD)] | ||
final class Serialize |
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.
Shouldn't this #[Serialize]
attribute allow to pass the groups to be used during the serialization process?
Also, the SerializerInterface
and its serialize
method allows us to pass the preferred format (JSON, XML, ...) so making it an argument (even with a predefined value) makes sense to me 🤔
==== EDIT ====
By the way: with "passing groups" I meant that they seem to be a relevant setting so maybe it's a good idea to add them with a shortcut (instead of passing them via context), but that's just a secondary matter - the primary concern here is the missing format:
$data = $this->serializer->serialize(
data: $output,
format: 'json',
context: ['groups' => ['Group1', 'Group2']]
);
so it could be like this:
#[Serialize(code: 201, format: 'json', headers: [], serializationContext: ['groups' => ['Group1', 'Group2']])]
or even
#[Serialize(code: 201, format: 'json', headers: [], groups: ['Group1', 'Group2'])]
* @author Konstantin Myakshin <[email protected]> | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_METHOD)] | ||
final class Serialize |
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.
Hi,
I think this is a great idea and would be very useful for anyone developing webservice so thank you.
However wouldn't it be more logical for consistency to name the attribute in the same manner as the others ?
Something like MapResponsePayload
seems more intuitive IMHO.
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.
this attributes can be renamed soon, see #58709
#[Serialize(code: 200, headers: [], serializationContext: [])]
Allows automatically serialize Controller Result to format based on Request format.Usage example 🔨
Todo
#[MapRequestPayload]
and#[MapQueryString]
to map Request input to typed objects #49138 will be merged