Skip to content

Fix first-request capture for multi-call examples#370

Open
Kris-LIBIS wants to merge 1 commit into
exoego:masterfrom
Kris-LIBIS:fix/first-request-openapi-capture
Open

Fix first-request capture for multi-call examples#370
Kris-LIBIS wants to merge 1 commit into
exoego:masterfrom
Kris-LIBIS:fix/first-request-openapi-capture

Conversation

@Kris-LIBIS

@Kris-LIBIS Kris-LIBIS commented Jun 26, 2026

Copy link
Copy Markdown

Closes #371

This fixes OpenAPI generation for examples that make multiple requests against the same path.

Problem:

  • When an example performs a follow-up request after the request that should define the documented operation, the generator could reuse the later request/response metadata and contaminate the operation details for the shared path.

Fix:

  • Capture the first request/response exchange seen in an example and prefer that exchange during extraction.
  • This keeps operation metadata stable when a single example exercises multiple verbs or repeated calls on the same path.

Tests:

  • Added a regression spec that reproduces the multi-request shared-path case and verifies the generated OpenAPI keeps the correct GET/DELETE metadata separate.
  • Verified locally with the new regression spec passing.

end

session = context.instance_variable_get(:@integration_session)
return [session.request, session.response] if session&.request && session&.response
@@ -0,0 +1,81 @@
# frozen_string_literal: true

module RSpec::OpenAPI::RequestRecorder

module RSpec::OpenAPI::RequestRecorder
THREAD_KEY = :rspec_openapi_first_exchange
VERBS = %i[get post put patch delete head options].freeze
THREAD_KEY = :rspec_openapi_first_exchange
VERBS = %i[get post put patch delete head options].freeze

module RackMethodsTracking
end
end

module RailsRunnerTracking
return if @installed

prepend_tracking_module(::Rack::Test::Methods, RackMethodsTracking) if defined?(::Rack::Test::Methods)
prepend_tracking_module(::ActionDispatch::Integration::Runner, RailsRunnerTracking) if defined?(::ActionDispatch::Integration::Runner)
expect(get_op.dig('responses', '404', 'description')).to eq('returns 404 when missing=true')

expect(delete_op['summary']).to eq('Delete multi request resource')
expect(delete_op.dig('responses', '200', 'description')).to eq('deletes resource and verifies it is gone with a follow-up GET')
@codecov

codecov Bot commented Jun 27, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.21053% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 91.32%. Comparing base (0842541) to head (a50da6a).

Files with missing lines Patch % Lines
lib/rspec/openapi/request_recorder.rb 85.10% 1 Missing and 6 partials ⚠️
lib/rspec/openapi/extractors/hanami.rb 50.00% 0 Missing and 1 partial ⚠️
lib/rspec/openapi/extractors/rack.rb 50.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #370      +/-   ##
==========================================
- Coverage   92.33%   91.32%   -1.01%     
==========================================
  Files          23       24       +1     
  Lines         900      957      +57     
  Branches      179      193      +14     
==========================================
+ Hits          831      874      +43     
- Misses         14       20       +6     
- Partials       55       63       +8     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenAPI metadata gets mixed up when performing multiple requests on the same URL in an example

2 participants