-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
env['api.format'] does not override formatter set by content-type #2171
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
Comments
It looks like the value of the content type overrides the format. See grape/lib/grape/middleware/formatter.rb Lines 55 to 58 in 02d7113
I'm not sure what the safest way is to reverse this? Could it literally just be reversed? |
If this test shows the intended behaviour, I don't think we'll be able to override the output by just changing the order the Lines 3732 to 3739 in 7741f02
If we did, it would break the behaviour where there is an explicit format set, but the implementation expects to be able to override it by changing the content type. For example: format :json
def action
content_type "text/plain"
"plain text"
end would still output as JSON if we reversed the lookup to I don't think there's any non-breaking way to override the behaviour of one of the content-type associated formatters at the moment on a per-request basis. |
So it looks like a bug because we explicitly document Related, what's a real use-case where you want to do this? Is it that you want to produce actual JSON and leave the formatter alone? Why not rely on the formatter to do this work? |
here's my use case: |
Yeah. Our use case is similar. We have cached data that's already JSON that we'd like to just return vs having to parse and re-encode. I'll take a look at starting a PR tomorrow with some options. 👍 |
The PR in #2505 fixes the issue but is not being finished. If anyone here wants to take it on, we would appreciate it! |
@dblock I think we should add a feature to return unformatted data aka It's kind of a combination of |
@ericproulx It's not a bad idea, let's see it. If I understand correctly you want something that would bypass a formatter, and it makes sense because the intent of all these existing methods is to set a content-type along with data ( |
In reference to #1989 (sorry, I have commented in there before I realised the issue was closed).
I'm having exactly the same issue.
If this is used:
Then it still calls the JSON formatter. If the
content_type
method is left off, it doesn't.Here is an example using grape-on-rack:
https://github.com/waynerobinson/grape-on-rack
Please take a look at the extra methods added to
Acme::Ping
.The only one that doesn't call the JSON formatter is the one where the content type is set to
text/plain
.The text was updated successfully, but these errors were encountered: