bploetz / versionist

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

Best way to deprecate an old version #79

Closed kiddrew closed 7 years ago

kiddrew commented 7 years ago

I know this isn't an "issue" with the gem, but I thought someone might have some ideas for how to achieve this. I used versionist for v1 of my API, and it's time to upgrade to v2. The thing is, I need to deprecate v1 and I would like those old routes to throw a 426 (upgrade required). If I simply remove the old routes, they will throw 404s instead.

Has anyone found a good solution for this?

bploetz commented 7 years ago

Not sure if others have a better approach, but what I would do is put a before filter in the API version's base controller which halts any request to that version and returns the 426 status code and any response body you want.

kiddrew commented 7 years ago

Yeah, I did that, but the issue is the request doesn't get routed there because I removed the v1 routes. I could keep them around, but that feels sloppy.

bploetz commented 7 years ago

Ah. So replace the v1 routes with a single catch-all v1 route that has the deprecation logic? You'll probably need to map this for all of the possible verbs used by v1.

get '/v1/*', to: 'foo#no_soup_for_you'
post '/v1/*', to: 'foo#no_soup_for_you'
put '/v1/*', to: 'foo#no_soup_for_you'
...etc...

Just thinking out loud here......

kiddrew commented 7 years ago

That's the Rails magic I was missing! Good call Brian, and thanks for a great gem.

bploetz commented 7 years ago

👍