diff --git a/CHANGELOG.md b/CHANGELOG.md index 390ec80b5..9c9d05392 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ * [#3092](https://github.com/ruby-grape/grape/pull/3092): Skip posting empty Danger report comments - [@dblock](https://github.com/dblock). * [#3095](https://github.com/ruby-grape/grape/pull/3095): Freeze Grape's constants all the way down - [@ericproulx](https://github.com/ericproulx). * [#3096](https://github.com/ruby-grape/grape/pull/3096): Define Grape's callable constants as modules rather than lambdas - [@ericproulx](https://github.com/ericproulx). +* [#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). * Your contribution here. #### Fixes diff --git a/UPGRADING.md b/UPGRADING.md index 84d60b21d..98cd7020e 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -3,6 +3,10 @@ Upgrading Grape ### Upgrading to >= 4.2.0 +#### `Grape::Util::MediaType` parses for a vendor's pattern + +`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)`. + #### A `use` of unknown params raises when the API compiles 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. diff --git a/lib/grape/middleware/versioner/header.rb b/lib/grape/middleware/versioner/header.rb index 57e30c8e5..ec4ec25f2 100644 --- a/lib/grape/middleware/versioner/header.rb +++ b/lib/grape/middleware/versioner/header.rb @@ -45,8 +45,9 @@ def initialize super @cache = Hash.new do |h, (vendor, available_media_types)| declared = available_media_types.map(&:-@).freeze + vendor_pattern = Grape::Util::MediaType.vendor_pattern(vendor) if vendor h[[vendor, declared].freeze] = [*declared, *COMMON_ACCEPT_HEADERS].to_h do |accept| - [accept, Grape::Util::MediaType.best_quality(accept, declared, vendor:)] + [accept, Grape::Util::MediaType.best_quality(accept, declared, vendor_pattern:)] end.compact.freeze end end @@ -55,6 +56,7 @@ def initialize def initialize(app, **options) super @media_type_for_accept = MediaTypeForAcceptCache[[vendor, available_media_types]] + @vendor_pattern = Grape::Util::MediaType.vendor_pattern(vendor) if vendor end # Base#call copies the middleware per request so #before can keep the @@ -93,7 +95,7 @@ def match_best_quality_media_type! strict_header_checks! # The table's keys are valid Strings, which scrubbing leaves alone, so # it is read with the header as it came. - media_type = @media_type_for_accept[env['HTTP_ACCEPT']] || Grape::Util::MediaType.best_quality(accept_header, available_media_types, vendor:) + media_type = @media_type_for_accept[env['HTTP_ACCEPT']] || Grape::Util::MediaType.best_quality(accept_header, available_media_types, vendor_pattern: @vendor_pattern) return yield media_type if media_type fail! @@ -143,7 +145,7 @@ def invalid_version_header!(message) def fail! return if env[Grape::Env::GRAPE_ALLOWED_METHODS] - media_types = q_values_mime_types.map { |mime_type| Grape::Util::MediaType.parse(mime_type, vendor:) } + media_types = q_values_mime_types.map { |mime_type| Grape::Util::MediaType.parse(mime_type, vendor_pattern: @vendor_pattern) } vendor_not_found!(media_types) || version_not_found!(media_types) end diff --git a/lib/grape/util/media_type.rb b/lib/grape/util/media_type.rb index dcac274de..051353a4c 100644 --- a/lib/grape/util/media_type.rb +++ b/lib/grape/util/media_type.rb @@ -19,7 +19,9 @@ class MediaType # version +20+. Given the vendor it is parsed for, the vendor is spelled # out and everything after it up to the format is the version. # - # Keyed by the vendors APIs declare, never by what a client sends. + # Keyed by the vendors APIs declare, never by what a client sends, and + # read through {.vendor_pattern} where a vendor is declared, so that a + # request reads no shared, mutable state. class VendorVersionHeaderRegexCache < Grape::Util::Cache def initialize super @@ -34,12 +36,12 @@ def initialize # strings are what it writes into the env. The arguments are copied # rather than frozen, as they are the caller's. # - # A +subtype+ naming a vendor other than +vendor+ is parsed as it would - # be without one. - def initialize(type:, subtype:, vendor: nil) + # A +subtype+ naming a vendor other than the one +vendor_pattern+ is for + # is parsed as it would be without one. + def initialize(type:, subtype:, vendor_pattern: nil) @type = -type @subtype = -subtype - match = VendorVersionHeaderRegexCache[vendor].match(@subtype) if vendor + match = vendor_pattern.match(@subtype) if vendor_pattern match ||= VENDOR_VERSION_HEADER_REGEX.match(@subtype) if match @vendor = match[:vendor].freeze @@ -64,17 +66,24 @@ def hash end class << self - def best_quality(header, available_media_types, vendor: nil) - parse(best_quality_media_type(header, available_media_types), vendor:) + # The pattern that parses a media type for +vendor+, keeping a hyphen + # in a version, to be passed as +vendor_pattern:+. Look it up where the + # vendor is declared, once. + def vendor_pattern(vendor) + VendorVersionHeaderRegexCache[vendor] end - def parse(media_type, vendor: nil) + def best_quality(header, available_media_types, vendor_pattern: nil) + parse(best_quality_media_type(header, available_media_types), vendor_pattern:) + end + + def parse(media_type, vendor_pattern: nil) return if media_type.blank? type, subtype = media_type.downcase.split('/', 2) return if type.blank? || subtype.blank? - new(type:, subtype:, vendor:) + new(type:, subtype:, vendor_pattern:) end def match?(media_type) diff --git a/spec/grape/util/media_type_spec.rb b/spec/grape/util/media_type_spec.rb index 5cc23addf..48f80786d 100644 --- a/spec/grape/util/media_type_spec.rb +++ b/spec/grape/util/media_type_spec.rb @@ -70,17 +70,17 @@ end it 'keeps them in the version when parsed for the vendor' do - media_type = described_class.parse(header, vendor: 'acme') + media_type = described_class.parse(header, vendor_pattern: described_class.vendor_pattern('acme')) expect([media_type.vendor, media_type.version, media_type.format]).to eq(%w[acme 2024-06-20 json]) end it 'parses a hyphenated vendor' do - media_type = described_class.parse('application/vnd.my-vendor-v1+json', vendor: 'my-vendor') + media_type = described_class.parse('application/vnd.my-vendor-v1+json', vendor_pattern: described_class.vendor_pattern('my-vendor')) expect([media_type.vendor, media_type.version]).to eq(%w[my-vendor v1]) end it 'parses another vendor as it would without one' do - expect(described_class.parse('application/vnd.other-2024-06-20+json', vendor: 'acme')) + expect(described_class.parse('application/vnd.other-2024-06-20+json', vendor_pattern: described_class.vendor_pattern('acme'))) .to eq(described_class.parse('application/vnd.other-2024-06-20+json')) end end