Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

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

Sign up
Appearance settings

[VarDumper] Only selectHtmlDumper ifAccept 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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
alexandre-daubois wants to merge1 commit intosymfony:7.4
base:7.4
Choose a base branch
Loading
fromalexandre-daubois:auto-html-vd

Conversation

alexandre-daubois
Copy link
Member

@alexandre-dauboisalexandre-daubois commentedAug 23, 2024
edited
Loading

QA
Branch?7.2
Bug fix?no
New feature?yes
Deprecations?no
Issues-
LicenseMIT

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

@stof
Copy link
Member

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
MemberAuthor

alexandre-daubois commentedAug 23, 2024
edited
Loading

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 envor the Accept header is present withtext/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

This isnot what your new condition does

@alexandre-daubois
Copy link
MemberAuthor

alexandre-daubois commentedAug 23, 2024
edited
Loading

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

@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 inmore cases, not less cases.

@alexandre-daubois
Copy link
MemberAuthor

alexandre-daubois commentedAug 23, 2024
edited
Loading

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

@stofstof 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

alexandre-daubois reacted with eyes emoji
@stof
Copy link
Member

I would treat it as a feature.

alexandre-daubois reacted with thumbs up emoji

@alexandre-dauboisalexandre-dauboisforce-pushed theauto-html-vd branch 2 times, most recently from13dbdcc to21042dfCompareAugust 23, 2024 12:04
@alexandre-dauboisalexandre-daubois changed the title[VarDumper] Add automatic HTML output whenAccept: text/html is set[VarDumper] Only select HtmlDumper ifAccept header is set with non-cli SAPIAug 23, 2024
@alexandre-daubois
Copy link
MemberAuthor

alexandre-daubois commentedAug 23, 2024
edited
Loading

It is now updated with your suggestions, thank you!

@Jimbolino
Copy link

Jimbolino commentedAug 23, 2024
edited
Loading

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 and lilj reacted with thumbs up emoji

@pierresh
Copy link

pierresh commentedAug 23, 2024
edited
Loading

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 in54772 (and probably the one described in49754 as well) is not solved by using CLiDumper instead of HtmlDumper. A plain text dumper is indeed necessary.

@alexandre-daubois
Copy link
MemberAuthor

@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.

pierresh reacted with heart emoji

@alexandre-daubois
Copy link
MemberAuthor

I'm not convinced that usingdd() is the most suitable solution for debugging a production application. Apart from that, have you looked at the static propertyAbstractDumper::$defaultOutput, but alsoAbstractDumper::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? 🙂

pierresh reacted with confused emoji

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

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 nothtml

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
MemberAuthor

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

@pierreshpierreshSep 2, 2024
edited
Loading

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
MemberAuthor

Choose a reason for hiding this comment

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

Alright, updated 👍

@javiereguiluzjaviereguiluz added the ❄️ Feature FreezeImportant Pull Requests to finish before the next Symfony "feature freeze" labelOct 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
MemberAuthor

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

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

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

@fabpotfabpot modified the milestones:7.2,7.3Nov 20, 2024
@OskarStarkOskarStark changed the title[VarDumper] Only select HtmlDumper ifAccept header is set with non-cli SAPI[VarDumper] Only selectHtmlDumper ifAccept header is set with non-cli SAPIMay 6, 2025
@nicolas-grekasnicolas-grekas removed the ❄️ Feature FreezeImportant Pull Requests to finish before the next Symfony "feature freeze" labelMay 17, 2025
@fabpotfabpot modified the milestones:7.3,7.4May 26, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@nicolas-grekasnicolas-grekasnicolas-grekas left review comments

@pierreshpierreshpierresh left review comments

@stofstofstof requested changes

@javiereguiluzjaviereguiluzjaviereguiluz approved these changes

Assignees
No one assigned
Projects
None yet
Milestone
7.4
Development

Successfully merging this pull request may close these issues.

8 participants
@alexandre-daubois@stof@Jimbolino@pierresh@nicolas-grekas@javiereguiluz@fabpot@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp