ruby-grape / ruby-grape/grape

Middleware called for incorrect versions

Open
#1,466 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug?
Dominant language
Ruby
Stars
10k
Forks
1.2k
Avg merge
14h 38m
Merged PRs (30d)
92

Description

When setting up api versions that fall back from newer to older, middlewares will be called twice. I believe its related to how lib/grape/router.rb tries the first matching endpoint it finds, then falls back to other matching endpoints if the first response is empty. Because the version is checked in middleware, it executes all the middleware above it before breaking because its the incorrect version.

One workaround would be to add any custom middleware after the Version middleware, but the insert_after methods are not available in 0.16.0.

I took a stab at adding a version check in lib/grape/router.rb, but the version code is spread across multiple middlewares so it would need to be consolidated first.

It might also be possible to update the registration of the Version middleware to put it at the top of the stack.

I'm happy to help code up a solution if needed, but I could use some guidance from those more familiar with the inner Grape workings.

Reference:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with lib/grape/router.rb and the test linked in the issue's referenced commit, then review how version middleware is registered and how fallback endpoints are selected. Reproduce the duplicate middleware calls for an API version fallback and use the mailing-list discussion for context. Done means middleware runs only for the appropriate version while fallback routing still works.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.