Skip to content

Conversation

trialize
Copy link

@trialize trialize commented Oct 12, 2024

As per #2171

UPDATE: Proposed solution is not appropriate and requires further investigation

@grape-bot
Copy link

2 Warnings
⚠️ There're library changes, but not tests. That's OK as long as you're refactoring existing code.
⚠️ Unless you're refactoring existing code or improving documentation, please update CHANGELOG.md.

Here's an example of a CHANGELOG.md entry:

* [#2505](https://github.com/ruby-grape/grape/pull/2505): Prioritize env api_format over rack::content_type header - [@trialize](https://github.com/trialize).

Generated by 🚫 Danger

Copy link
Member

@dblock dblock left a comment

Choose a reason for hiding this comment

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

Looks reasonable.

This needs a test, a CHANGELOG entry, a paragraph in UPGRADING, and some documentation about order of formatters in README, please.

This is a breaking change, but also likely a bug. If no existing tests fail we are good with the above.

I see this test fail.

  1) Grape::API.format :json can be overwritten with an explicit content type
     Failure/Error: expect(last_response.body).to eq({ meaning_of_life: 42 }.to_s)

       expected: #<Encoding:US-ASCII> "{:meaning_of_life=>42}"
            got: #<Encoding:ASCII-8BIT> "\"{:meaning_of_life=>42}\""

       (compared using ==)
     # ./spec/grape/api_spec.rb:4101:in `block (4 levels) in <top (required)>'

Let's understand whether the test is just wrong?

@trialize trialize closed this Oct 13, 2024
@trialize trialize deleted the hotfix/fetch_formatter branch October 13, 2024 14:54
@trialize
Copy link
Author

trialize commented Oct 13, 2024

Closed due to lack of time to address the case according to CONTRIBUTING requirements.

Following code

get do
  content_type 'application/json'
  env['api.format'] = :binary
  '{ "meaning_of_life": 42 }'
end

Still would apply json formatter and produce "{ \"meaning_of_life\": 42 }" which is a bug.

As a workaround, for those having the need, following could be used on separate resources.

module API
  class myAPI < Grape::API
    resource :raw_json do

    content_type :raw_json, 'application/json'
    formatter :raw_json, ->(object, _env) { object }

    get do
      '{ "myRawJson": 123 }'
    end

  end
end

(P.S. This will override Grape's default JSON formatter, meaning you'd lose it one other endpoints as well)

@dblock
Copy link
Member

dblock commented Oct 14, 2024

Hopefully someone can finish this, please!

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.

3 participants