bploetz / versionist

A plugin for versioning Rails based RESTful APIs.
MIT License
971 stars 51 forks source link

Accept parameters for versioned media types #1

Closed steveklabnik closed 12 years ago

steveklabnik commented 12 years ago

I'm not sure that Rails actually supports this, but instead of application/vnd.mycompany.com-v1, accept application/vnd.mycompany.com; version=1. RFC2046 has examples of this.

atambo commented 12 years ago

@steveklabnik, it doesn't: https://github.com/rails/rails/issues/4127#issuecomment-4167613

steveklabnik commented 12 years ago

Word up. Time to write my first patch to Rails. cracks fingers

atambo commented 12 years ago

I would :heart: for rails to have a better mime type parser. I have to special case Accept headers way too often to prevent that missing template error.

bploetz commented 12 years ago

Yeah, this Accept header bug in Rails has bitten us at work too.

They've merged a fix into Rails' master branch (which is Rails 4.0.0 beta), but have not back ported it to Rails 3.x yet.

https://github.com/rails/rails/pull/4918

I'll keep an eye on the ticket to get the fix into Rails 3.x that @atambo pointed out, and if/when they do that I'll make sure this works with Versionist as well.

steveklabnik commented 12 years ago

This only fixes q parameters, they should accept arbitrary ones.

steveklabnik commented 12 years ago

Pull requested: https://github.com/rails/rails/pull/5290

steveklabnik commented 12 years ago

Aaaand merged. Damn, jose is fast. :)

bploetz commented 12 years ago

high five

bploetz commented 12 years ago

I've switched our API to use this variant of the Accept header as suggested by @steveklabnik ....

application/vnd.mycompany.com; version=1

...and it works just fine with Rails 3.1.4 and Versionist 0.2.0. I'll update the README to use this format as an example for the Accept header and close this issue. If anyone out there finds that this isn't working for them, please re-open this issue.

steveklabnik commented 12 years ago

Wooo!

bploetz commented 12 years ago

The one gotcha here is with the generators. If you use parameters, you must wrap the Accept header string in quotes, otherwise Thor chops off everything from the semi-colin on. For example:

FAIL:
rails generate versionist:new_api_version v2 V2 header:Accept value:application/vnd.mycompany.com; version=2
       route  api_version(:module => "V2", :header=>"Accept", :value=>"application/vnd.mycompany.com") do
  end

FAIL:
rails generate versionist:new_api_version v2 V2 header:Accept value:application/vnd.mycompany.com;version=2
       route  api_version(:module => "V2", :header=>"Accept", :value=>"application/vnd.mycompany.com") do
  end

SUCCESS:
rails generate versionist:new_api_version v2 V2 header:Accept value:"application/vnd.mycompany.com; version=2"
       route  api_version(:module => "V2", :header=>"Accept", :value=>"application/vnd.mycompany.com; version=2") do
  end