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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Middleware usage does not work as expected #258

Open
wuarmin opened this issue Apr 13, 2023 · 0 comments
Open

Middleware usage does not work as expected #258

wuarmin opened this issue Apr 13, 2023 · 0 comments

Comments

@wuarmin
Copy link

wuarmin commented Apr 13, 2023

Hey 馃憢 ,

I have following routes:

module MyApp
  class Routes < Hanami::Routes
    root { "Hello from MyApp-server" }

    slice :payments, at: "/payments" do
      use :body_parser, :json # json body-parser should be used for all payments-requests.
      
      scope :auth do
        post "/login", to: "auth.login"
      end

      scope :graphql do
        use MyApp::Middlewares::Authorization
        post "/api", to: "graphql.api"
      end

    end
  end
end

It is expected that the json-body-parser is applied to all payment requests, regardless if payments/auth/login or payments/graphql/api is requested. But that's not the case. The body payload of a payments/auth/login is not parsed and fails. A request to payments/graphql/api is parsed and runs through the MyApp::Middlewares::Authorization-middlware.

user@6beeaaf00e22:~/app$ bin/hanami middleware
/                    Dry::Monitor::Rack::Middleware (instance)
/                    Rack::Session::Cookie
/payments            Hanami::Middleware::BodyParser
/payments/graphql    PayNRed::Middlewares::Authorization

If I remove the MyApp::Middlewares::Authorization-middleware, the body-parsing for payments/auth/login and payments/graphql/api works as expected.

To make it work, I had to change the routes to following:

module MyApp
  class Routes < Hanami::Routes
    root { "Hello from MyApp-server" }

    slice :payments, at: "/payments" do

      scope :auth do
        use :body_parser, :json # json body-parser should be used for all payments-requests.
        post "/login", to: "auth.login"
      end

      scope :graphql do
        use :body_parser, :json # json body-parser should be used for all payments-requests.
        use MyApp::Middlewares::Authorization
        post "/api", to: "graphql.api"
      end

    end
  end
end
user@6beeaaf00e22:~/app$ bin/hanami middleware
/                    Dry::Monitor::Rack::Middleware (instance)
/                    Rack::Session::Cookie
/payments/auth       Hanami::Middleware::BodyParser
/payments/graphql    Hanami::Middleware::BodyParser
/payments/graphql    PayNRed::Middlewares::Authorization

Is this the expected behavior?

Thanks and best regards

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

1 participant