Skip to content

Commit 65b3c51

Browse files
ericproulxclaude
andcommitted
Remove the dead format keyword from Grape::Router::Pattern
`Pattern#initialize` took a `format:` keyword and, in `extract_capture`, constrained the mustermann `:format` capture with it. But nothing ever passed a non-nil value: the endpoint stopped passing `format:` when the vestigial `Grape::Endpoint::Options` `format` member was removed (#2779), and grape-swagger's helper passes `format: nil`. It was also redundant — a route's `:format` capture comes from the path suffix built by `Grape::Path` (`(.json)` for a specific format, `(.:format)` otherwise), not from the pattern. Drop the keyword and the dead capture branch. `DEFAULT_CAPTURES` still lists `format` (the path-derived capture) and `map_str` still serves `version`, so matching is unchanged. `Grape::Router::Pattern.new(..., format: …)` now raises `unknown keyword: :format`; corrected the UPGRADING note that previously described it as an optional keyword. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 9ece4d9 commit 65b3c51

4 files changed

Lines changed: 7 additions & 11 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* [#2777](https://github.com/ruby-grape/grape/pull/2777): Add `Grape::Endpoint#mounted_app` reader and `app:` keyword - [@ericproulx](https://github.com/ericproulx).
1010
* [#2778](https://github.com/ruby-grape/grape/pull/2778): Rename `Grape::Endpoint`'s `for:` keyword to `api:` and expose `#api` - [@ericproulx](https://github.com/ericproulx).
1111
* [#2779](https://github.com/ruby-grape/grape/pull/2779): Promote route metadata (`version`, `namespace`, `prefix`, `requirements`, `anchor`, `settings`) to `Grape::Router::Route` keyword arguments, and drop the unused `Grape::Endpoint::Options` `format` member - [@ericproulx](https://github.com/ericproulx).
12+
* [#2782](https://github.com/ruby-grape/grape/pull/2782): Remove the dead `format` keyword from `Grape::Router::Pattern` - [@ericproulx](https://github.com/ericproulx).
1213
* Your contribution here.
1314

1415
#### Fixes

UPGRADING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ What changed is the raw bag. `route.options` now holds only what was declared fo
5858

5959
The `:format` member was removed from the endpoint's internal `Options`. Nothing ever passed `format:` to `Grape::Endpoint.new``config.format` was always `nil` — so this has no runtime effect (the `(.:format)`/`(.json)` route suffix comes from `Grape::Path`, not from this value). But `Grape::Endpoint.new(..., format: …)` now raises `unknown keyword: :format` instead of silently ignoring it.
6060

61-
`Grape::Router::Pattern#initialize` still accepts `format:` — it remains a valid capture constraint — but it is now optional (`format: nil`).
61+
`Grape::Router::Pattern#initialize` no longer accepts `format:` either. It was never given a non-`nil` value — a route's `:format` capture comes from the path suffix built by `Grape::Path`, not from the pattern — so the keyword was dead. `Grape::Router::Pattern.new(..., format: …)` now raises `unknown keyword: :format`.
6262

6363
### Upgrading to >= 3.3
6464

lib/grape/router/pattern.rb

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ class Pattern
1313
def_delegators :to_regexp, :===
1414
alias match? ===
1515

16-
def initialize(origin:, suffix:, anchor:, params:, version:, requirements:, format: nil)
16+
def initialize(origin:, suffix:, anchor:, params:, version:, requirements:)
1717
@origin = origin
1818
@path = PatternCache[[build_path_from_pattern(@origin, anchor), suffix]]
19-
@pattern = MustermannPattern.new(@path, uri_decode: true, params:, capture: extract_capture(format, version, requirements))
19+
@pattern = MustermannPattern.new(@path, uri_decode: true, params:, capture: extract_capture(version, requirements))
2020
@to_regexp = @pattern.to_regexp
2121
end
2222

@@ -28,14 +28,10 @@ def captures_default
2828

2929
private
3030

31-
def extract_capture(format, version, requirements)
32-
capture = {}
33-
capture[:format] = map_str(format) if format.present?
34-
capture[:version] = map_str(version) if version.present?
31+
def extract_capture(version, requirements)
32+
return requirements if version.blank?
3533

36-
return capture if requirements.blank?
37-
38-
requirements.merge(capture)
34+
requirements.merge(version: map_str(version))
3935
end
4036

4137
def build_path_from_pattern(pattern, anchor)

spec/grape/router/route_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
suffix: '',
1111
anchor: true,
1212
params: {},
13-
format: nil,
1413
version: nil,
1514
requirements: {}
1615
)

0 commit comments

Comments
 (0)