Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.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 ourterms of service andprivacy 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?
Uh oh!
There was an error while loading.Please reload this page.
Conversation
Uh oh!
There was an error while loading.Please reload this page.
#[SerializedResponse]
Attribute to serialize Controller Result#[SerializedResponse]
Attribute to serialize Controller Result8e54594
to9e16d8c
Compare This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
a30ee49
to06087b9
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 withAs
when they describe more the config of the class (AsCommand or AsMessageHandler)
here it is like a mapping/transformation, perhaps like your other PR#49138MapResponse
src/Symfony/Component/HttpKernel/EventListener/SerializeControllerResultListener.php OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
src/Symfony/Component/HttpKernel/EventListener/SerializeControllerResultListener.phpShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
src/Symfony/Component/HttpKernel/EventListener/SerializeControllerResultListener.php OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
9eea8e6
to9d1fb32
CompareThis test is failinghttps://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
to3213f4b
Comparefbc4eae
toca0b60d
Compare This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
9845ae5
tof350273
Comparenicolas-grekas commentedApr 7, 2023 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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. |
Koc commentedApr 7, 2023 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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 viaNelmioApiDocBundle. It will reduce even more boilerplate code for some projects. |
8ee1da3
to9058a4b
Compare9058a4b
toad86cea
CompareRebased, renamed @nicolas-grekas please review 🙏 |
ad86cea
to3c9a9b6
Compare3c9a9b6
to490a4ad
CompareI'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 alreadysuccessfully adopted |
* @author Konstantin Myakshin <molodchick@gmail.com> | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_METHOD)] | ||
final class Serialize |
DavidGarciaCatFeb 1, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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, theSerializerInterface
and itsserialize
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 <molodchick@gmail.com> | ||
*/ | ||
#[\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 likeMapResponsePayload
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
Uh oh!
There was an error while loading.Please reload this page.
#[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