Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[VarDumper] Only select HtmlDumper if Accept header is set with non-cli SAPI #58070

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

Open
wants to merge 1 commit into
base: 7.3
Choose a base branch
Loading
from

Conversation

alexandre-daubois
Copy link
Member

@alexandre-daubois alexandre-daubois commented Aug 23, 2024

Q A
Branch? 7.2
Bug fix? no
New feature? yes
Deprecations? no
Issues -
License MIT

dump() and dd() are very practical but sometimes a little too verbose, typically when debugging an JSON API for example. This change will only enable HtmlDumper when the Accept header exists and contains either text/html or */* in non-cli SAPI env.

@stof
Copy link
Member

stof commented Aug 23, 2024

The implementation does not seem to do what you describe. It still uses the HtmlDumper for all non-CLI SAPIs but uses it also for some cases of CLI SAPIs.

@alexandre-daubois
Copy link
Member Author

alexandre-daubois commented Aug 23, 2024

I'm not sure to get what you mean. The goal is to use CliDumper if we're in a CLI env, just as before. But if we're in another env or the Accept header is present with text/html, then we use HtmlDumper.

If you debug a JSON API, you don't want to HTML output because it messes with the output.

@stof
Copy link
Member

stof commented Aug 23, 2024

This is not what your new condition does

@alexandre-daubois
Copy link
Member Author

alexandre-daubois commented Aug 23, 2024

I'm sorry, I don't get where the condition is wrong 😕 Could you maybe precisely point at the mistake please?

It still uses the HtmlDumper for all non-CLI SAPIs

It is the current behavior. All this PR does is "forcing" the HTML output in case of the Accept header.

@stof
Copy link
Member

stof commented Aug 23, 2024

@alexandre-daubois in your condition, anything that is not a CLI SAPI will still use the HtmlDumper. what you changed is that you use the HtmlDumper in more cases, not less cases.

@alexandre-daubois
Copy link
Member Author

alexandre-daubois commented Aug 23, 2024

Alright, I think I figured out the problem, thank you for your explanation. Is it better now? If the header is present with the good value, we chose HtmlDumper. Otherwise, we fallback on CliDumper (just like proposed in #54772 (comment) actually).

At this point, I'm not 100% sure if this should be considered a bug or a feature 🤔

Copy link
Member

@stof stof left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me, we should keep CliDumper as the best guess for CLI sapis instead of trying to read $_SERVER['HTTP_ACCEPT'] for them

@stof
Copy link
Member

stof commented Aug 23, 2024

I would treat it as a feature.

@alexandre-daubois alexandre-daubois force-pushed the auto-html-vd branch 2 times, most recently from 13dbdcc to 21042df Compare August 23, 2024 12:04
@alexandre-daubois alexandre-daubois changed the title [VarDumper] Add automatic HTML output when Accept: text/html is set [VarDumper] Only select HtmlDumper if Accept header is set with non-cli SAPI Aug 23, 2024
@alexandre-daubois
Copy link
Member Author

alexandre-daubois commented Aug 23, 2024

It is now updated with your suggestions, thank you!

@Jimbolino
Copy link

Jimbolino commented Aug 23, 2024

I would recommend to create a new "plain text only" dumper, and use that as the default fallback (if no accept html header is present)
HtmlDumper and CliDumper are imho not good fallbacks for most usecases

@pierresh
Copy link

pierresh commented Aug 23, 2024

I would recommend to create a new "plain text only" dumper, and use that as the default fallback (if no accept html header is present) HtmlDumper and CliDumper are imho not good fallbacks for most usecases

You are right, the CliDumper does not output anything if it is not in a terminal. I tried to see what would be the result of CliDumper in Chrome dev tool and it is just empty.

Thus the problem I reported in 54772 (and probably the one described in 49754 as well) is not solved by using CLiDumper instead of HtmlDumper. A plain text dumper is indeed necessary.

@alexandre-daubois
Copy link
Member Author

@pierresh I think that your case could (and should) be solved by launching a local VarDumper server so the output doesn't messes up with your API response

@Jimbolino
Copy link

@pierresh I think that your case could (and should) be solved by launching a local VarDumper server so the output doesn't messes up with your API response

a dd() will (and should) always mess up the output, no one excepts valid formatted json output if they call dd(), else you would return a new JsonResponse() instead of calling dd().

Also when debugging production / staging / docker etc, its not always possible to launch a VarDumper server.

@alexandre-daubois
Copy link
Member Author

I'm not convinced that using dd() is the most suitable solution for debugging a production application. Apart from that, have you looked at the static property AbstractDumper::$defaultOutput, but also AbstractDumper::setOutput()? You can pass any callable/resource/output-path to it, which may solve your issue.

I think that the conversation is moving away from the original purpose of the PR. Perhaps it's a good idea to continue the discussion in the respective issues to avoid any confusion? 🙂

Comment on lines 126 to 127
$acceptHeader = $_SERVER['HTTP_ACCEPT'] ?? null;
if (null === $acceptHeader || str_contains($acceptHeader, 'text/html') || str_contains($acceptHeader, '*/*')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure why we should use html when */* is found but not html

Suggested change
$acceptHeader = $_SERVER['HTTP_ACCEPT'] ?? null;
if (null === $acceptHeader || str_contains($acceptHeader, 'text/html') || str_contains($acceptHeader, '*/*')) {
if (str_contains($_SERVER['HTTP_ACCEPT'] ?? 'html', 'html')) {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the header is present and we reach this condition, we are necessarily in a SAPI context other than CLI. So I'm thinking that it's still worth rendering the HTML if the client sends "accept all". It seems more interesting to me than using CliDumper with the various escape characters that won't really work outside of a terminal. What do you think?

Copy link

@pierresh pierresh Sep 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is used by Chrome dev tools (accept: application/json, text/plain, */*)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, updated 👍

@javiereguiluz javiereguiluz added the ❄️ Feature Freeze Important Pull Requests to finish before the next Symfony "feature freeze" label Oct 9, 2024
@nicolas-grekas
Copy link
Member

Does this really work on Symfony apps? Don't we register another handler that'd also need a similar logic, but that'd use the request_stack instead of the globals to get the accept header?

@alexandre-daubois
Copy link
Member Author

I tried with a new Symfony app and a dummy controller like the following:

class HomeController
{
    #[Route(path: '/')]
    public function home(): Response
    {
        dd(PHP_SAPI);
    }
}

It dumped the value in the CLI, and dumped as HTML in the browser when adding Accept: text/html. Is there a place or a case I'm missing?

@fabpot fabpot modified the milestones: 7.2, 7.3 Nov 20, 2024
@OskarStark OskarStark changed the title [VarDumper] Only select HtmlDumper if Accept header is set with non-cli SAPI [VarDumper] Only select HtmlDumper if Accept header is set with non-cli SAPI May 6, 2025
@nicolas-grekas nicolas-grekas removed the ❄️ Feature Freeze Important Pull Requests to finish before the next Symfony "feature freeze" label May 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.