Skip to content
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

[WIP] Accept header - Version support #612

Closed
wants to merge 19 commits into from

Conversation

amenophis
Copy link
Contributor

This is a first PR about my idea. It will add support for allow or not the access to methods by checking version passed into the Accept HTTP header

This is a first PR about my idea. It will add support for allow or not
the access to methods by checking version passed into the Accept HTTP
header
@amenophis
Copy link
Contributor Author

@lsmith77 Do you see more what i'm doing ?

@lsmith77
Copy link
Member

Thank you for the PR. I am at a conference this weekend but should definitely have time on the train home tomorrow.

@lsmith77
Copy link
Member

related to #607

'event' => 'kernel.controller',
'method' => 'onKernelController',
'priority' => -255
))
Copy link
Member

Choose a reason for hiding this comment

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

This should be done in the DI extension, not in a compiler pass

@amenophis
Copy link
Contributor Author

Hi @stof,
Thanks for your feedback !
I've done some corrections based on your remarks !

Could you review ?

'event' => 'kernel.controller',
'method' => 'onKernelController',
'priority' => -255
))
Copy link
Member

Choose a reason for hiding this comment

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

This should be defined in the XML file like other services, not in the PHP file

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I do this in PHP as i'm not sure how to make the condition for serialization context argument. Can you say me more ?

Copy link
Member

Choose a reason for hiding this comment

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

<argument type="service" id="fos_rest.version.serialisation_context" on-invalid="null" />

@amenophis
Copy link
Contributor Author

@stof @lsmith77 Is it better like this ?

@adrienbrault
Copy link
Contributor

Just to make sure, with this implementation declaring 2 controllers with the same path but with different versions won't work in practice right ?

@amenophis
Copy link
Contributor Author

The idea I want to adress is, I want to expose methods only for version. I want to have the same application serve multiple version of the API.

And, yes, if you declare 2 controller with an action with the same route, it do not work. But we don't need to have a same method in two controllers .

@mvrhov
Copy link

mvrhov commented Nov 25, 2013

Sometimes there is just too much difference between two versions that having both in one controller would be impractical.

@amenophis
Copy link
Contributor Author

Yes, but in this case, the method you use the Until annotation, and you declare another method with a Since annotation. If you don't want to change the name of the method, you have to switch on the version inside the main method and route into Vx or Vy method

@lsmith77
Copy link
Member

right .. this is why ideally the entire version handling would be handled inside the routing or at least be associated with the routing configuration. see also #561

@amenophis
Copy link
Contributor Author

Yes, it could be better to handle the version inside the routing. Any idea on how to implement it ?

@amenophis
Copy link
Contributor Author

@lsmith77 Can you explain me more ? IRC ?

@lsmith77
Copy link
Member

@amenophis sure catch me tomorrow in #symfony-dev on freenode. today i am too tired from practice.

@amenophis
Copy link
Contributor Author

@lsmith77 OK ! thanks

@lsmith77
Copy link
Member

lsmith77 commented Dec 9, 2013

so FOSRestBundle master now can set the matched media type as a request attribute #621

@amenophis
Copy link
Contributor Author

Can i do something ?

public function onKernelRequest(GetResponseEvent $event) {
$request = $event->getRequest();

$acceptHeader = $request->headers->get('Accept');
Copy link
Member

Choose a reason for hiding this comment

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

this should now be $request->attributes->get('media_type');

however it requires FOSRestBundle master with the format listener enabled.

@amenophis
Copy link
Contributor Author

@lsmith77 I'm not sure about my rebase ... is it OK ?

return $this->version;
}

public function __construct(Reader $reader, $regex, Context $context = null) {
Copy link
Member

Choose a reason for hiding this comment

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

{ needs to be on a newline

@amenophis
Copy link
Contributor Author

@lsmith77 Done.
Do i need to move the regex into configuration ? If yes, where ? Which key ?

@lsmith77
Copy link
Member

lsmith77 commented Dec 9, 2013

like discussed via IRC:

  1. move the regexp setting to a setter method and out of the constructor
  2. drop the context from the listener, instead set the version on the view handler (requires inject the ViewHandler params to make them dynamically settable #628)
  3. the version listener should be enabled explicitly (off by default) like all the other listeners
  4. add some documentation (especially that the version listener needs the format listener so that the ``media_type` is set as a request attribute

@@ -11,6 +11,7 @@

namespace FOS\RestBundle\Controller;

use JMS\Serializer\SerializationContext;
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 removed

@lsmith77
Copy link
Member

lsmith77 commented Dec 9, 2013

@Toflar
Copy link

Toflar commented Dec 9, 2013

The JMS Serializer has annotations for Since and Until too. Any reason why we cannot reuse them? :)

@merk
Copy link
Member

merk commented Dec 9, 2013

Because FOSRestBundle supports the Symfony serializer as well.

@Toflar
Copy link

Toflar commented Dec 9, 2013

I see, thx for the hint :)

return;
}

foreach ($annotations as $annotation) {
Copy link
Member

Choose a reason for hiding this comment

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

one thing I am wondering here is how to make it possible that one could annotate multiple controllers that each cover a different version of the API for the same path.

Copy link
Member

Choose a reason for hiding this comment

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

@lsmith77
Copy link
Member

@amenophis I have extracted the version detection part of your PR into a new PR -> #636

This way we can add the feature quickly. For the annotations I then want to come up with a fancier solution which would work as explained in #637

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants