Skip to content

Middleware usage does not work as expected#258

Description

@wuarmin

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions