Middleware tidy-up: dedupe versioner media types + Formatter cleanups#2749
Open
ericproulx wants to merge 1 commit into
Open
Middleware tidy-up: dedupe versioner media types + Formatter cleanups#2749ericproulx wants to merge 1 commit into
ericproulx wants to merge 1 commit into
Conversation
* `Versioner::Base#build_available_media_types`: emit the bare `application/vnd.<vendor>-<version>` once per version instead of once per (version × content_type), eliminating N-1 duplicate entries per version in `@available_media_types`. * `Formatter#read_body_input?`: chained `&&` rewritten as guard returns. * `Formatter#read_rack_input`: merge parsed body into the existing `RACK_REQUEST_FORM_HASH` in place via `merge!` instead of allocating a new hash; also tolerates a nil-valued key (previously crashed). * `Formatter#ensure_content_type`: assign content-type in place rather than allocating a merged headers hash. * `Formatter#after`: drop the no-op splat on `status, headers, bodies = *@app_response`. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
0dfc1db to
28c4528
Compare
Danger ReportNo issues found. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two unrelated middleware cleanups bundled together.
Grape::Middleware::Versioner::Base#build_available_media_typesThe bare versioned media type
"application/vnd.<vendor>-<version>"was emitted inside thecontent_types.each_keyloop, even though it doesn't depend onextension. With the 5 default content types and a single declared version, the same string ended up in@available_media_types5 times — and N times generally, where N is the number of registered content types.Hoisted the bare versioned emission out of the extension loop:
available_media_types.first(used whenAcceptis blank) is unchanged: stillapplication/vnd.<vendor>-<latest-version>+<first-content-type>. The change only affects ordering between equal-Q-value entries inRack::Utils.best_q_match; the versioner specs verify specific header matches, not relative ordering, and all pass.Grape::Middleware::Formatter#read_body_input?— chained&&rewritten as guard-style early returns; each rejection reason now reads on its own line.#read_rack_input— when merging the parsed body intoenv[RACK_REQUEST_FORM_HASH], merge in place viamerge!rather than allocating a fresh hash viamerge+ reassign. Also toleratesenv[RACK_REQUEST_FORM_HASH]being explicitly set tonil(previouslynil.merge(...)would have raised).#ensure_content_type— assign the content-type header in place rather than allocating a merged headers hash.#after— drop the no-op splat instatus, headers, bodies = *@app_response. The RHS is already a Rack response triple (Array); the*doesn't change destructuring of an Array.Test plan
bundle exec rspec spec/grape/middleware/— 356 examples, 0 failuresbundle exec rspec spec/grape/util/media_type_spec.rb— passesavailable_media_types.first(the only ordering-sensitive consumer)🤖 Generated with Claude Code