hanami / controller

Complete, fast and testable actions for Rack and Hanami
http://hanamirb.org
MIT License
246 stars 111 forks source link

Where `application/x-www-form-urlencoded` and `multipart/form-data`? #439

Open AlexeyMatskevich opened 5 months ago

AlexeyMatskevich commented 5 months ago

I'm trying to contribute to the rspec-openapi library and I'm using hanami 2 in production. In rspec-openapi, there is a dummy test application on rails that I am trying to re-implementon hanami.

In particular, the original application in some cases transmits data in the following formats: application/x-www-form-urlencoded and multipart/form-data when upload images.

I'm reading this article https://guides.hanamirb.org/v2.1/actions/formats-and-mime-types/ and I'm following this link over 50 of the most common MIME types and I can't find the MIME-type I need in the list.

By default I'm using form :json, so I'm trying to get around the problem manually:

module HanamiTest

  class App < Hanami::App
    config.actions.formats.add(:form, "application/x-www-form-urlencoded")
    config.actions.formats.add(:multipart, "multipart/form-data")
    config.actions.format :json, :form, :multipart
  end
end

But I only succeed with form data.

I researched Hanami::Action::Mime.accepted_mime_type?

        def accepted_mime_type?(mime_type, config)
          accepted_mime_types(config).any? { |accepted_mime_type|
            ::Rack::Mime.match?(mime_type, accepted_mime_type)
          }
        end

i get

irb(Hanami::Action::Mime):001:0> mime_type
=> "multipart/form-data; boundary=----------XnJLe9ZIbbGUYtzPQJ16u1"
irb(Hanami::Action::Mime):002:0> accepted_mime_types(config)
=> ["multipart/form-data"]

I also found this code for FormParser but couldn't find any documentation for it and have no idea how to use it.

Any help would be appreciated.

AlexeyMatskevich commented 5 months ago

I found a workaround, remove format everywhere above inheritance and from app.rb Action

module HanamiTest
  class Action < Hanami::Action
    format :json - remove
  end
end

App.rb

module HanamiTest
  class App < Hanami::App
    config.actions.format :json - remove
  end
end
cllns commented 1 month ago

@AlexeyMatskevich Thanks for reporting this issue. I've opened a PR adding :form and :multipart as built-in formats, which will be very helpful.

I think your "workaround" above is actually the solution to this problem. The config formats are the highest level and you were manually overriding them lower down the stack in your actions, so they weren't being respected. Does that sound correct?