WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Middleware usage does not work as expected #258

@wuarmin

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

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