-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DX][WebProfilerBundle] Add Pretty Print functionality for Request Content #28919
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
[DX][WebProfilerBundle] Add Pretty Print functionality for Request Content #28919
Conversation
I like this proposal a lot. I would apply this behaviour by default, without any button or toggle. To prevent issues with complex queries, I'd apply a |
I think it should be pretty by default, but a |
and if we want to do it all the time (with a tab switcher to view the raw one), it would make sense to do the pretty-printing server-side. |
👍 Sounds good. I'll look at getting that implemented. If we're doing the pretty-printing on the server, that would mean we're putting the entire payload into the DOM twice, once "pretty-printed", the other raw – I'm assuming most payloads aren't too big so this won't be an issue - are we okay with this? |
also instead of pretty JSON, wouldnt VarDumper (thus collapsible) be far more useful? |
d985841
to
d56ed85
Compare
Okay, so, I've taken the above feedback, rebased and updated the UI. There are now "Pretty" and "Raw" tabs. I've left the formatting down to JS for reasons discussed later. I definitely prefer this. Thank you for the suggestions 👍
I don't have much experience with the internals of the profiler. However, from what I can workout there are a few options we have to pretty print on the server. I'm assuming the method of pretty-printing we'd use is
It would be great to hear some feedback on the above options as I'm not really sure which route to take.
I had been thinking about this. My main motivation for not using VarDumper was maintaining the ability to copy the pretty-printed output and edit it (e.g. for use in Postman). If we output using the Sidenote: I've had to create commit 9cdcf0b as there appears to be a bug when nesting tabs in the profiler panels. Looks like until this PR it's never been required. Shall I split this out into a separate PR? |
You could add a getter on the Collector object returning the pretty-printed JSON, without the need to do it at collect time (it can be computed from the raw content at render time) |
src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/request.html.twig
Outdated
Show resolved
Hide resolved
src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/request.html.twig
Outdated
Show resolved
Hide resolved
d73ef2e
to
06361a0
Compare
src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/base_js.html.twig
Show resolved
Hide resolved
src/Symfony/Component/HttpKernel/DataCollector/RequestDataCollector.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/HttpKernel/DataCollector/RequestDataCollector.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/HttpKernel/Tests/DataCollector/RequestDataCollectorTest.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/HttpKernel/Tests/DataCollector/RequestDataCollectorTest.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/HttpKernel/DataCollector/RequestDataCollector.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/HttpKernel/DataCollector/RequestDataCollector.php
Outdated
Show resolved
Hide resolved
We could also add a 3rd tab: "Debug" that uses |
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.
depending on #28919 (comment)
Thanks for the approval @ro0NL – I've updated the regex as suggested 👌 — It will match I'm currently trying to setup the "Debug" tab as suggested but am struggling with some unexpected errors. I'll keep trying. |
@SamFleming perhaps better to introduce the debug tab in a new PR so we dont block/postpone this one (i was just brainfarting :)) But i definitively think it's a nice addition.
yes, i tweaked it to more generic. Now matches |
Awesome, 👌 Anything left for me to do to get this ready for merging? |
let's wait :) i do have one last question though; what happens if the request content is not valid JSON? Even if the content-type says so.. does it crash the panel? Will developer see the error? |
Perhaps fallback to raw content only without tabs then. I.e. the current behavior |
Done 63c0219 Is this too... horrible? |
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 !="null" check is implicit, but if the null is valid raw&pretty show the same content anyway, so it doesnt really hurt and avoids 2 tabs showing null 👍
df6deac
to
9446deb
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.
Explicit also fine for me 😅 t:
Oh... I thought I saw a comment about checking I definitely prefer, as it removes any abiquity. However, if we're going this route, is there any point checking the Content-Type for being JSON? Is it worth attempting the |
the extra overhead might bite us as we try to parse each request content, every time. Tend to prefer the explicit check here. |
I also wonder if it's worth caching the result of |
👌 Fair |
actually we dont parse during the request :D (im tired), only during display in the profiler. But yeah, i think we should be conservative, and expand as needed. |
correct. We should return string|null (checking for last_error intermediary) and call it once in twig. |
Apologies, not quite sure what you mean here. I was thinking this 91a2b39 |
return |
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.
👌
@dunglas was there anything else you wanted me to do on this? |
Thank you @SamFleming. |
9fcbb1e
to
9f85103
Compare
… for Request Content (SamFleming) This PR was squashed before being merged into the 4.3-dev branch (closes #28919). Discussion ---------- [DX][WebProfilerBundle] Add Pretty Print functionality for Request Content | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | n/a ? ## Why? Quite often when attempting to debug issues with JSON requests sent to a Symfony API, I use the Web Profiler to check the request content. More often than not the request content isn't easily readable (99% of the time it's all stuck on a single line and impossible to read). I always find myself copying + pasting the content into a random online tool to have it "pretty-print" the JSON. Usually this isn't an issue, but can be annoying when offline. There's also the security issue of sending entire JSON payloads to a third-party server just for formatting 😳. Alternatively, maybe developers copy+paste into their chosen editors and this PR is all a waste of time — I hope not 😛. ## How? This PR adds "Pretty-Print" JSON functionality straight into the profiler. We can use `collector.requestheaders` to detect if the request was JSON and conditionally show the Pretty Print button. When the button is clicked, we format the JSON from the "Request Content" card. ## What does it look like? Before:  After:  Non-JSON Requests (unchanged):  ## Things to consider - Is `JSON.stringify(JSON.parse(content));` the safest, most efficient way to do this? - Should the "Pretty Print" button be in-line next to the "Request Content" header? I couldn't find a pattern for this sort of thing elsewhere in the profiler. - Do people want JSON formatted with 4 spaces, would 2 spaces be preferred? Should this be a configuration option stored in localStorage (such as the light/dark theme configuration)? - Should this be a toggle? E.g. click to pretty print, then click to undo ## Future Improvements Depending on how this is received it could be extended to support formatting different request content-types (e.g. XML formatting) — I assume. ## Progress - [x] Gather feedback and decide where to perform the pretty-print: [server-side, or client-side](symfony/symfony#28919 (comment)). *It was decided server-side would be better.* Commits ------- 9f85103151 [DX][WebProfilerBundle] Add Pretty Print functionality for Request Content
… for Request Content (SamFleming) This PR was squashed before being merged into the 4.3-dev branch (closes #28919). Discussion ---------- [DX][WebProfilerBundle] Add Pretty Print functionality for Request Content | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | n/a ? ## Why? Quite often when attempting to debug issues with JSON requests sent to a Symfony API, I use the Web Profiler to check the request content. More often than not the request content isn't easily readable (99% of the time it's all stuck on a single line and impossible to read). I always find myself copying + pasting the content into a random online tool to have it "pretty-print" the JSON. Usually this isn't an issue, but can be annoying when offline. There's also the security issue of sending entire JSON payloads to a third-party server just for formatting 😳. Alternatively, maybe developers copy+paste into their chosen editors and this PR is all a waste of time — I hope not 😛. ## How? This PR adds "Pretty-Print" JSON functionality straight into the profiler. We can use `collector.requestheaders` to detect if the request was JSON and conditionally show the Pretty Print button. When the button is clicked, we format the JSON from the "Request Content" card. ## What does it look like? Before:  After:  Non-JSON Requests (unchanged):  ## Things to consider - Is `JSON.stringify(JSON.parse(content));` the safest, most efficient way to do this? - Should the "Pretty Print" button be in-line next to the "Request Content" header? I couldn't find a pattern for this sort of thing elsewhere in the profiler. - Do people want JSON formatted with 4 spaces, would 2 spaces be preferred? Should this be a configuration option stored in localStorage (such as the light/dark theme configuration)? - Should this be a toggle? E.g. click to pretty print, then click to undo ## Future Improvements Depending on how this is received it could be extended to support formatting different request content-types (e.g. XML formatting) — I assume. ## Progress - [x] Gather feedback and decide where to perform the pretty-print: [server-side, or client-side](symfony/symfony#28919 (comment)). *It was decided server-side would be better.* Commits ------- 9f85103151 [DX][WebProfilerBundle] Add Pretty Print functionality for Request Content
… for Request Content (SamFleming) This PR was squashed before being merged into the 4.3-dev branch (closes #28919). Discussion ---------- [DX][WebProfilerBundle] Add Pretty Print functionality for Request Content | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | n/a ? ## Why? Quite often when attempting to debug issues with JSON requests sent to a Symfony API, I use the Web Profiler to check the request content. More often than not the request content isn't easily readable (99% of the time it's all stuck on a single line and impossible to read). I always find myself copying + pasting the content into a random online tool to have it "pretty-print" the JSON. Usually this isn't an issue, but can be annoying when offline. There's also the security issue of sending entire JSON payloads to a third-party server just for formatting 😳. Alternatively, maybe developers copy+paste into their chosen editors and this PR is all a waste of time — I hope not 😛. ## How? This PR adds "Pretty-Print" JSON functionality straight into the profiler. We can use `collector.requestheaders` to detect if the request was JSON and conditionally show the Pretty Print button. When the button is clicked, we format the JSON from the "Request Content" card. ## What does it look like? Before:  After:  Non-JSON Requests (unchanged):  ## Things to consider - Is `JSON.stringify(JSON.parse(content));` the safest, most efficient way to do this? - Should the "Pretty Print" button be in-line next to the "Request Content" header? I couldn't find a pattern for this sort of thing elsewhere in the profiler. - Do people want JSON formatted with 4 spaces, would 2 spaces be preferred? Should this be a configuration option stored in localStorage (such as the light/dark theme configuration)? - Should this be a toggle? E.g. click to pretty print, then click to undo ## Future Improvements Depending on how this is received it could be extended to support formatting different request content-types (e.g. XML formatting) — I assume. ## Progress - [x] Gather feedback and decide where to perform the pretty-print: [server-side, or client-side](symfony/symfony#28919 (comment)). *It was decided server-side would be better.* Commits ------- 9f85103 [DX][WebProfilerBundle] Add Pretty Print functionality for Request Content
… for Request Content (SamFleming) This PR was squashed before being merged into the 4.3-dev branch (closes #28919). Discussion ---------- [DX][WebProfilerBundle] Add Pretty Print functionality for Request Content | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | n/a ? ## Why? Quite often when attempting to debug issues with JSON requests sent to a Symfony API, I use the Web Profiler to check the request content. More often than not the request content isn't easily readable (99% of the time it's all stuck on a single line and impossible to read). I always find myself copying + pasting the content into a random online tool to have it "pretty-print" the JSON. Usually this isn't an issue, but can be annoying when offline. There's also the security issue of sending entire JSON payloads to a third-party server just for formatting 😳. Alternatively, maybe developers copy+paste into their chosen editors and this PR is all a waste of time — I hope not 😛. ## How? This PR adds "Pretty-Print" JSON functionality straight into the profiler. We can use `collector.requestheaders` to detect if the request was JSON and conditionally show the Pretty Print button. When the button is clicked, we format the JSON from the "Request Content" card. ## What does it look like? Before:  After:  Non-JSON Requests (unchanged):  ## Things to consider - Is `JSON.stringify(JSON.parse(content));` the safest, most efficient way to do this? - Should the "Pretty Print" button be in-line next to the "Request Content" header? I couldn't find a pattern for this sort of thing elsewhere in the profiler. - Do people want JSON formatted with 4 spaces, would 2 spaces be preferred? Should this be a configuration option stored in localStorage (such as the light/dark theme configuration)? - Should this be a toggle? E.g. click to pretty print, then click to undo ## Future Improvements Depending on how this is received it could be extended to support formatting different request content-types (e.g. XML formatting) — I assume. ## Progress - [x] Gather feedback and decide where to perform the pretty-print: [server-side, or client-side](#28919 (comment)). *It was decided server-side would be better.* Commits ------- 9f85103 [DX][WebProfilerBundle] Add Pretty Print functionality for Request Content
Why?
Quite often when attempting to debug issues with JSON requests sent to a Symfony API, I use the Web Profiler to check the request content. More often than not the request content isn't easily readable (99% of the time it's all stuck on a single line and impossible to read). I always find myself copying + pasting the content into a random online tool to have it "pretty-print" the JSON.
Usually this isn't an issue, but can be annoying when offline. There's also the security issue of sending entire JSON payloads to a third-party server just for formatting 😳. Alternatively, maybe developers copy+paste into their chosen editors and this PR is all a waste of time — I hope not 😛.
How?
This PR adds "Pretty-Print" JSON functionality straight into the profiler.
We can use
collector.requestheaders
to detect if the request was JSON and conditionally show the Pretty Print button.When the button is clicked, we format the JSON from the "Request Content" card.
What does it look like?
Before:

After:

Non-JSON Requests (unchanged):

Things to consider
JSON.stringify(JSON.parse(content));
the safest, most efficient way to do this?Future Improvements
Depending on how this is received it could be extended to support formatting different request content-types (e.g. XML formatting) — I assume.
Progress
It was decided server-side would be better.