ruby-grape/grape · warning

`#{self.class.name}#[]` is deprecated. Use the named accesso

Error message

`#{self.class.name}#[]` is deprecated. Use the named accessor `#{key}` instead.

What it means

Per-middleware option classes (used by the error, formatter, and versioner middlewares) are now `Data` structs with named accessors instead of Hashes. The `[]` shim kept for legacy `options[:key]` access warns via `Grape.deprecator` and forwards to the named accessor via `public_send`; unknown keys (not in `members`) return nil.

Source

Thrown at lib/grape/middleware/deprecated_options_hash_access.rb:11

# frozen_string_literal: true

module Grape
  module Middleware
    # Mixin for per-middleware +Options+ +Data+ classes that need to keep
    # accepting legacy +data[:key]+ Hash-style access while nudging callers
    # toward the named accessor. Emits a +Grape.deprecator+ warning then
    # forwards to +public_send(key)+.
    module DeprecatedOptionsHashAccess
      def [](key)
        Grape.deprecator.warn(
          "`#{self.class.name}#[]` is deprecated. " \
          "Use the named accessor `#{key}` instead."
        )
        public_send(key) if members.include?(key)
      end
    end
  end
end

View on GitHub (pinned to 22d7975629)

Solutions

  1. Switch to the named accessor: `options.some_key`
  2. When the key is dynamic, guard on members: `options.public_send(key) if options.members.include?(key)`
  3. Pin the middleware's specs against the new accessor interface so regressions surface in CI

Example fix

# before
status = options[:default_status]

# after
status = options.default_status
Defensive patterns

Strategy: validation

Validate before calling

# Prefer named accessors; if the key is dynamic, guard on members:
value = options.public_send(key) if options.members.include?(key)

Type guard

def named_option?(options, key)
  options.respond_to?(:members) && options.members.include?(key)
end

Prevention

When it happens

Trigger: Custom middleware subclassing a Grape middleware and reading `options[:some_key]` instead of `options.some_key`; code reaching into middleware option objects with Hash-style access after a Grape upgrade.

Common situations: Upgrading Grape where middleware Options classes became Data; shared middleware code written against the old Hash interface.

Related errors


AI-assisted analysis of ruby-grape/grape@22d7975629 (2026-08-21). Data as JSON: /api/errors/5a257456302a0956. Report an issue: GitHub.