Skip to content

Move content-type helpers from Middleware::Base into PrecomputedContentTypes#2719

Open
ericproulx wants to merge 1 commit into
masterfrom
refactor/precomputed-content-types-move
Open

Move content-type helpers from Middleware::Base into PrecomputedContentTypes#2719
ericproulx wants to merge 1 commit into
masterfrom
refactor/precomputed-content-types-move

Conversation

@ericproulx
Copy link
Copy Markdown
Contributor

Summary

Middleware::Base#content_types / #mime_types / #content_type_for / #content_type / #content_types_indifferent_access are only consumed by middleware that include Grape::Middleware::PrecomputedContentTypes — namely Formatter, Error, and Versioner::Base. The other concrete middlewares (Filter, Auth::Base) never call them.

Move the five methods into PrecomputedContentTypes. Base keeps the storage layer (@options / @env / @app / Rack::Request wrapper / header merge / query_params); the content-type concern lives with the mixin that already takes responsibility for warming its caches.

Why

Currently Base reaches into the options Hash twice — options[:content_types] and options[:format] — to compute helpers that not every subclass needs. That coupling makes it impossible to migrate a subclass's options to a typed value object without also retrofitting Hash semantics onto that value object, just to keep Base happy.

After this move, Base doesn't touch its options hash at all. The mixin still uses options[:…] — but now only middleware that opted in by including the mixin pay for that coupling, and a follow-up can replace the [] access inside the mixin without rippling through Base.

Scope

  • lib/grape/middleware/base.rb — five methods removed.
  • lib/grape/middleware/precomputed_content_types.rb — same five methods added, identical bodies, ivar caching preserved.

No call sites moved or renamed; PrecomputedContentTypes is currently included in exactly the three middlewares that called these helpers, so behaviour is unchanged.

Test plan

  • Full suite: bundle exec rspec — 2307 examples, 0 failures
  • RuboCop clean
  • CI green across Gemfile variants

🤖 Generated with Claude Code

…ntTypes

`Middleware::Base#content_types` / `#mime_types` / `#content_type_for` /
`#content_type` / `#content_types_indifferent_access` are only consumed by
middleware that include `PrecomputedContentTypes` — namely `Formatter`,
`Error`, and `Versioner::Base`. The other concrete middlewares
(`Filter`, `Auth::Base`) never call them.

Move the five methods into `PrecomputedContentTypes`. `Base` keeps the
ivar plumbing (`@options`, `@env`, `@app`), `Rack::Request` wrapper,
header merge, and `query_params`; the content-type concern lives with
the mixin that already takes responsibility for warming its caches.

This narrows `Base` to the storage / lifecycle layer and unblocks future
work to give per-middleware `Options` value objects without `Base`
reaching back into the option hash. No behaviour change — every
middleware that called those helpers already included
`PrecomputedContentTypes`.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@ericproulx ericproulx force-pushed the refactor/precomputed-content-types-move branch from 51f7cc2 to 2532013 Compare May 14, 2026 18:16
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 14, 2026

Danger Report

No issues found.

View run

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

Successfully merging this pull request may close these issues.

1 participant