Skip to content

[HttpFoundation] Adds getAcceptableFormats() method for Request #26486

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

Conversation

AndreiIgna
Copy link

@AndreiIgna AndreiIgna commented Mar 11, 2018

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #21909
License MIT
Doc PR symfony/symfony-docs#...

Adds a new method getAcceptableFormats() for Request. This reads the content types in Accept header and maps them to formats already defined in Request class

In a request made by a browser, based on the default Accept header, the getAcceptableFormats() will return an array ['html', 'xml']

In progress

  • gather feedback for my changes
  • submit changes to the documentation

@derrabus
Copy link
Member

It looks like this PR contains unrelated commits. Could you do a rebase against master?

@AndreiIgna AndreiIgna force-pushed the httpfoundation-request-add-acceptable-formats branch from 08a9337 to 00acc8c Compare March 13, 2018 12:15
@AndreiIgna
Copy link
Author

Oops used GitHub desktop, which did something unexpected to the rebase. Should be ok now

@@ -170,6 +170,11 @@ class Request
*/
protected $format;

/**
* @var string
Copy link
Member

Choose a reason for hiding this comment

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

Should be array.

@@ -10,6 +10,7 @@ CHANGELOG
* The `get()` method of the `AcceptHeader` class now takes into account the
`*` and `*/*` default values (if they are present in the Accept HTTP header)
when looking for items.
* Adds `getAcceptableFormats()` for reading acceptable formats based on Accept header
Copy link
Member

Choose a reason for hiding this comment

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

Should be added to be consistent with the wording of other entries in that file.

@AndreiIgna
Copy link
Author

Thanks noticing and letting me know about those @derrabus

The added method makes it easier to check what data formats the clients accepts, ex:

if (in_array('html', $request->getAcceptableFormats())) {
  // return html data, maybe a template
} elseif (in_array('xml', $request->getAcceptableFormats())) {
  // return xml
} else {
  // return json data
}

If this is useful and included in next release, I'll make the addition in documentation repo as well

return $this->acceptableFormats;
}

return $this->acceptableFormats = array_values(array_unique(array_filter(array_map(array($this, 'getFormat'), $this->getAcceptableContentTypes()))));
Copy link
Member

Choose a reason for hiding this comment

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

is array_values actually required?

Copy link
Author

Choose a reason for hiding this comment

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

Is added because the mix of array_filter and array_unqiue sometimes returns [0 => 'html', 2 => 'xml', 5 => 'json'].
This create problems with tests or confusion when checking the array, so it's best to reset the keys

*
* @return array List of acceptable formats by the client
*/
public function getAcceptableFormats()
Copy link
Member

@nicolas-grekas nicolas-grekas Mar 16, 2018

Choose a reason for hiding this comment

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

missing return type (and removal of the corresponding docblock line since it will become useless)

@AndreiIgna AndreiIgna force-pushed the httpfoundation-request-add-acceptable-formats branch 2 times, most recently from 1e9bf3f to eef572b Compare March 23, 2018 09:38
@nicolas-grekas nicolas-grekas modified the milestones: 4.1, next Apr 20, 2018
@nicolas-grekas
Copy link
Member

@AndreiIgna would you mind rebasing and moving the line in the changelog on 4.2?

@AndreiIgna AndreiIgna force-pushed the httpfoundation-request-add-acceptable-formats branch from eef572b to 7f062b1 Compare June 5, 2018 08:28
@AndreiIgna
Copy link
Author

Yes sure, is updated now

/**
* @var array
*/
protected $acceptableFormats;
Copy link
Member

Choose a reason for hiding this comment

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

should be private

4.2.0
-----

* Adds `getAcceptableFormats()` for reading acceptable formats based on Accept header
Copy link
Member

Choose a reason for hiding this comment

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

added

@AndreiIgna AndreiIgna force-pushed the httpfoundation-request-add-acceptable-formats branch from 7f062b1 to 9bfb693 Compare June 11, 2018 10:52
@AndreiIgna
Copy link
Author

Updated based on last review.

While checking in more detail the similar $acceptableContentTypes, saw that it's assigned null on initialize and duplicate methods. Added this for $acceptableFormats too, hope this is ok

@nicolas-grekas nicolas-grekas force-pushed the httpfoundation-request-add-acceptable-formats branch from 9bfb693 to 8a127ea Compare June 19, 2018 11:26
@nicolas-grekas
Copy link
Member

Thank you @AndreiIgna.

@nicolas-grekas nicolas-grekas merged commit 8a127ea into symfony:master Jun 19, 2018
nicolas-grekas added a commit that referenced this pull request Jun 19, 2018
…r Request (AndreiIgna)

This PR was squashed before being merged into the 4.2-dev branch (closes #26486).

Discussion
----------

[HttpFoundation] Adds getAcceptableFormats() method for Request

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #21909
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->

Adds a new method `getAcceptableFormats()` for `Request`. This reads the content types in Accept header and maps them to formats already defined in `Request` class

In a request made by a browser, based on the default Accept header, the `getAcceptableFormats()` will return an array `['html', 'xml']`

In progress
- [x] gather feedback for my changes
- [x] submit changes to the documentation

Commits
-------

8a127ea [HttpFoundation] Adds getAcceptableFormats() method for Request
javiereguiluz added a commit to symfony/symfony-docs that referenced this pull request Jun 19, 2018
…od (AndreiIgna)

This PR was squashed before being merged into the master branch (closes #9898).

Discussion
----------

[HttpFoundation] Add info for getAcceptableFormats() method

Adds info for `Request::getAcceptableFormats()`

Should it add more info on docs page? Ex: `Request::getAcceptableContentTypes` returns the accepted content types which can be `text/html`, `application/json`, etc.
`Request::getAcceptableFormats` processes that and returns the formats based on those content types `html`, `json`

ref symfony/symfony#26486

Commits
-------

65d100e [HttpFoundation] Add info for getAcceptableFormats() method

$request = new Request();
$request->headers->set('Accept', 'text/html, application/xhtml+xml, application/xml;q=0.9, */*');
$this->assertEquals(array('html', 'xml'), $request->getAcceptableFormats());
Copy link
Contributor

Choose a reason for hiding this comment

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

why would it only return html and xml if everything is acceptable according to */*? I think this shows that the method is not clear and generic enough to be added to the core IMO.

Copy link
Contributor

Choose a reason for hiding this comment

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

I can't see where I would possibly use this method. What would be useful is a similar method that accepts formats as argument and would return the ones that are acceptable according to the accept header. This would then allow to make use of */* and this is what people usually need in REST APIs etc.

@nicolas-grekas
Copy link
Member

FYI, reverted in #29047

nicolas-grekas added a commit that referenced this pull request Nov 1, 2018
…ethod for Request" (Tobion)

This PR was merged into the 4.2-dev branch.

Discussion
----------

Revert "[HttpFoundation] Adds getAcceptableFormats() method for Request"

This reverts commit 8a127ea.

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no <!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #26486
| License       | MIT
| Doc PR        |

As I said in #26486 (comment) and people wonder in https://symfony.com/blog/new-in-symfony-4-2-acceptable-request-formats#comment-22747, I don't think this method clear and generic enough to be added to the core.
I can't see where I would possibly use this method. What would be useful is a similar method that accepts formats as argument and would return the ones that are acceptable according to the accept header. This would then allow to make use of `*/*` and this is what people usually need in REST APIs etc.
But for now, we should revert it before it gets released like this.

Commits
-------

397ed83 Revert "[HttpFoundation] Adds getAcceptableFormats() method for Request"
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.2 Nov 1, 2018
This was referenced Nov 3, 2018
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.

7 participants