ruby-grape / ruby-grape/grape

request is nil in middleware on missing or incorrect content_type

Open
#1,792 4 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

We have a Logger that extends from the Grape middleman base. This logger need to have access to the request from env["api.request"] in the after method, which when a http request is sent with no content_type defined env["api.request"].request is nil which raises an error.

We are currently mounted with the use method

The following is the middle man call trace. Our middleware is API::Logger.

# Middleware call trace

## GOOD, correct content_type
### Before
API::Logger
Grape::Middleware::Versioner::Path
Grape::Middleware::Formatter

- Calls app endpoint here, logs sql whatnot

### After
Grape::Middleware::Formatter
Grape::Middleware::Versioner::Path
API::Logger

## BAD, missing content_type
### Before
API::Logger
Grape::Middleware::Versioner::Path
Grape::Middleware::Formatter

- Does NOT hit api

### After
Grape::Middleware::Versioner::Path
API::Logger
- raises error because request is missing from env["api.endpoint"]

One weird part in the bad calls is it goes up.

API::Logger
Grape::Middleware::Versioner::Path
Grape::Middleware::Formatter

but then one way down.

Grape::Middleware::Versioner::Path
API::Logger

My question is the request deleted if it does not match the content_type required or is this a bug?

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

No source files or tests are named. Start by reproducing a request with missing or incorrect content_type and trace Grape::Middleware::Formatter, Grape::Middleware::Versioner::Path, and API::Logger around env["api.request"]. Establish the expected middleware and request behavior, then verify it with regression coverage.

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
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.