Skip to content

Commit 712b029

Browse files
ericproulxclaude
andcommitted
Look up the header versioner's vendor pattern once, when it is built
A request whose Accept header missed the versioner's table read VendorVersionHeaderRegexCache, a singleton behind a Monitor. The versioner now passes the pattern down, and Grape::Util::MediaType takes vendor_pattern: in place of vendor:. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 197369a commit 712b029

5 files changed

Lines changed: 31 additions & 15 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* [#3090](https://github.com/ruby-grape/grape/pull/3090): Call Danger's workflows from danger/danger-pr-comment, where they moved - [@ericproulx](https://github.com/ericproulx).
1616
* [#3092](https://github.com/ruby-grape/grape/pull/3092): Skip posting empty Danger report comments - [@dblock](https://github.com/dblock).
1717
* [#3095](https://github.com/ruby-grape/grape/pull/3095): Freeze Grape's constants all the way down - [@ericproulx](https://github.com/ericproulx).
18+
* [#3098](https://github.com/ruby-grape/grape/pull/3098): Look up the header versioner's vendor pattern once, when it is built - [@ericproulx](https://github.com/ericproulx).
1819
* Your contribution here.
1920

2021
#### Fixes

‎UPGRADING.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ Upgrading Grape
33

44
### Upgrading to >= 4.2.0
55

6+
#### `Grape::Util::MediaType` parses for a vendor's pattern
7+
8+
`Grape::Util::MediaType.parse`, `.best_quality` and `.new` take `vendor_pattern:`, the pattern `Grape::Util::MediaType.vendor_pattern(vendor)` answers, in place of `vendor:` ([#3098](https://github.com/ruby-grape/grape/pull/3098)). The header versioner looks its vendor's pattern up as it is built, so a request no longer reads a cache. Only code calling these internals is affected: pass `vendor_pattern: Grape::Util::MediaType.vendor_pattern(vendor)`.
9+
610
#### A `use` of unknown params raises when the API compiles
711

812
A mounted API can now `use` the reusable `params` of the scope it is mounted in ([#3087](https://github.com/ruby-grape/grape/pull/3087)). An API's `params` blocks run before it is mounted, so a name `use` cannot find no longer raises `Params :name not found!` as the API class is defined. It raises when the API compiles, either on its first request or at `compile!`. A Rails application that eager loads compiles the APIs mounted in its routes at boot, so a misspelled name still fails the boot there. Elsewhere, call `compile!` on your API at boot, or in a spec, to catch one before a request does.

‎lib/grape/middleware/versioner/header.rb‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ def initialize
4545
super
4646
@cache = Hash.new do |h, (vendor, available_media_types)|
4747
declared = available_media_types.map(&:-@).freeze
48+
vendor_pattern = Grape::Util::MediaType.vendor_pattern(vendor) if vendor
4849
h[[vendor, declared].freeze] = [*declared, *COMMON_ACCEPT_HEADERS].to_h do |accept|
49-
[accept, Grape::Util::MediaType.best_quality(accept, declared, vendor:)]
50+
[accept, Grape::Util::MediaType.best_quality(accept, declared, vendor_pattern:)]
5051
end.compact.freeze
5152
end
5253
end
@@ -55,6 +56,7 @@ def initialize
5556
def initialize(app, **options)
5657
super
5758
@media_type_for_accept = MediaTypeForAcceptCache[[vendor, available_media_types]]
59+
@vendor_pattern = Grape::Util::MediaType.vendor_pattern(vendor) if vendor
5860
end
5961

6062
# Base#call copies the middleware per request so #before can keep the
@@ -93,7 +95,7 @@ def match_best_quality_media_type!
9395
strict_header_checks!
9496
# The table's keys are valid Strings, which scrubbing leaves alone, so
9597
# it is read with the header as it came.
96-
media_type = @media_type_for_accept[env['HTTP_ACCEPT']] || Grape::Util::MediaType.best_quality(accept_header, available_media_types, vendor:)
98+
media_type = @media_type_for_accept[env['HTTP_ACCEPT']] || Grape::Util::MediaType.best_quality(accept_header, available_media_types, vendor_pattern: @vendor_pattern)
9799
return yield media_type if media_type
98100

99101
fail!
@@ -143,7 +145,7 @@ def invalid_version_header!(message)
143145
def fail!
144146
return if env[Grape::Env::GRAPE_ALLOWED_METHODS]
145147

146-
media_types = q_values_mime_types.map { |mime_type| Grape::Util::MediaType.parse(mime_type, vendor:) }
148+
media_types = q_values_mime_types.map { |mime_type| Grape::Util::MediaType.parse(mime_type, vendor_pattern: @vendor_pattern) }
147149
vendor_not_found!(media_types) || version_not_found!(media_types)
148150
end
149151

‎lib/grape/util/media_type.rb‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ class MediaType
1919
# version +20+. Given the vendor it is parsed for, the vendor is spelled
2020
# out and everything after it up to the format is the version.
2121
#
22-
# Keyed by the vendors APIs declare, never by what a client sends.
22+
# Keyed by the vendors APIs declare, never by what a client sends, and
23+
# read through {.vendor_pattern} where a vendor is declared, so that a
24+
# request reads no shared, mutable state.
2325
class VendorVersionHeaderRegexCache < Grape::Util::Cache
2426
def initialize
2527
super
@@ -34,12 +36,12 @@ def initialize
3436
# strings are what it writes into the env. The arguments are copied
3537
# rather than frozen, as they are the caller's.
3638
#
37-
# A +subtype+ naming a vendor other than +vendor+ is parsed as it would
38-
# be without one.
39-
def initialize(type:, subtype:, vendor: nil)
39+
# A +subtype+ naming a vendor other than the one +vendor_pattern+ is for
40+
# is parsed as it would be without one.
41+
def initialize(type:, subtype:, vendor_pattern: nil)
4042
@type = -type
4143
@subtype = -subtype
42-
match = VendorVersionHeaderRegexCache[vendor].match(@subtype) if vendor
44+
match = vendor_pattern.match(@subtype) if vendor_pattern
4345
match ||= VENDOR_VERSION_HEADER_REGEX.match(@subtype)
4446
if match
4547
@vendor = match[:vendor].freeze
@@ -64,17 +66,24 @@ def hash
6466
end
6567

6668
class << self
67-
def best_quality(header, available_media_types, vendor: nil)
68-
parse(best_quality_media_type(header, available_media_types), vendor:)
69+
# The pattern that parses a media type for +vendor+, keeping a hyphen
70+
# in a version, to be passed as +vendor_pattern:+. Look it up where the
71+
# vendor is declared, once.
72+
def vendor_pattern(vendor)
73+
VendorVersionHeaderRegexCache[vendor]
6974
end
7075

71-
def parse(media_type, vendor: nil)
76+
def best_quality(header, available_media_types, vendor_pattern: nil)
77+
parse(best_quality_media_type(header, available_media_types), vendor_pattern:)
78+
end
79+
80+
def parse(media_type, vendor_pattern: nil)
7281
return if media_type.blank?
7382

7483
type, subtype = media_type.downcase.split('/', 2)
7584
return if type.blank? || subtype.blank?
7685

77-
new(type:, subtype:, vendor:)
86+
new(type:, subtype:, vendor_pattern:)
7887
end
7988

8089
def match?(media_type)

‎spec/grape/util/media_type_spec.rb‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,17 @@
7070
end
7171

7272
it 'keeps them in the version when parsed for the vendor' do
73-
media_type = described_class.parse(header, vendor: 'acme')
73+
media_type = described_class.parse(header, vendor_pattern: described_class.vendor_pattern('acme'))
7474
expect([media_type.vendor, media_type.version, media_type.format]).to eq(%w[acme 2024-06-20 json])
7575
end
7676

7777
it 'parses a hyphenated vendor' do
78-
media_type = described_class.parse('application/vnd.my-vendor-v1+json', vendor: 'my-vendor')
78+
media_type = described_class.parse('application/vnd.my-vendor-v1+json', vendor_pattern: described_class.vendor_pattern('my-vendor'))
7979
expect([media_type.vendor, media_type.version]).to eq(%w[my-vendor v1])
8080
end
8181

8282
it 'parses another vendor as it would without one' do
83-
expect(described_class.parse('application/vnd.other-2024-06-20+json', vendor: 'acme'))
83+
expect(described_class.parse('application/vnd.other-2024-06-20+json', vendor_pattern: described_class.vendor_pattern('acme')))
8484
.to eq(described_class.parse('application/vnd.other-2024-06-20+json'))
8585
end
8686
end

0 commit comments

Comments
 (0)