ruby-grape / grape

An opinionated framework for creating REST-like APIs in Ruby.
http://www.ruby-grape.org
MIT License
9.86k stars 1.22k forks source link

Support different incoming to outgoing content-type #1697

Open stevelacey opened 6 years ago

stevelacey commented 6 years ago

I am attempting to upgrade to v1 and have been banging my head against problems caused by #1589

We do some JSON Merge Patch – prior to #1589 that was as simple as:

if request.content_type == 'application/merge-patch+json'
  # do something different
end

Now I have to do some of this to dodge 406's

content_type :json_merge_patch, 'application/merge-patch+json'
format :json_merge_patch
formatter :json_merge_patch, Grape::Formatter::ActiveModelSerializers
error_formatter :json_merge_patch, ACME::JsonErrorFormatter
parser :json_merge_patch, Grape::Parser::Json

And this works, but now the API serves out Content-Type: application/merge-patch+json – but it should still be application/json, the request was partial but the response is full.

I expect I need to adjust the formatter to do this... but given ours is ActiveModelSerializers it's pretty abstract and I am not sure where JSON comes into it.

Is there really the need for all of this boilerplate to do this?

dblock commented 6 years ago

Just to rephrase the problem, given an incoming content type you want a different outgoing content type. Generally that seems pretty simple, you declare a formatter for the outgoing content type then set the content type inside that if. Is that not working? Care to try and produce a spec that shows what you're trying to accomplish and we can simplify it and possibly add some code to avoid the boilerplate?