Skip to content
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

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

Open
AlexeyMatskevich opened this issue Mar 28, 2024 · 3 comments
Open

Comments

@AlexeyMatskevich
Copy link

AlexeyMatskevich commented Mar 28, 2024

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
Copy link
Author

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
Copy link
Member

cllns commented Jul 26, 2024

@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?

@AlexeyMatskevich
Copy link
Author

@cllns Yeah, that sounds about right! For me, this problem comes down to this example - “If I have a basic API application and I define format :json in the Action class - then I can't override the format in subclasses when I need to use multipart or form mimetype in one or two places, simply because that mimetype doesn't exist”.

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

No branches or pull requests

2 participants