Skip to content

Conversation

@orionstudt
Copy link
Contributor

@orionstudt orionstudt commented Dec 16, 2025

In a Rails app, there is some data that gets added to the Rack Request env by the app itself while the request is handled (such as which controller and action actually handled the request after it was routed).

If you want to access any of this information in any openapi_first hooks, than you need to be able to configure tests to do the request validation only after the request has been handled.

@orionstudt orionstudt marked this pull request as ready for review December 17, 2025 00:37
@orionstudt orionstudt requested a review from ahx as a code owner December 17, 2025 00:37
@ahx
Copy link
Owner

ahx commented Dec 18, 2025

Hi! I am wondering if this should be the default behavior during tests. 🤔

I think this behavior might be useful, because it allows you to start a debugger in your controller code to debug invalid requests. I would rather make this the default and not introduce another configuration option. Do you have any thoughts on that?

@orionstudt
Copy link
Contributor Author

Hi! I am wondering if this should be the default behavior during tests. 🤔

I think this behavior might be useful, because it allows you to start a debugger in your controller code to debug invalid requests. I would rather make this the default and not introduce another configuration option. Do you have any thoughts on that?

I'm not opposed! I went to config so as not to affect other consumer's existing functionality, but if you think it should be default than I'm all for it.

@ahx
Copy link
Owner

ahx commented Dec 27, 2025

Merged as part of #436

@ahx ahx closed this Dec 27, 2025
ahx added a commit that referenced this pull request Jan 7, 2026
- Changed OpenapiFirst::Test to track the request _after_ the app has handled the request. See [PR #434](#434). You can restore the old behavior with
```ruby
  include OpenapiFirst::Test::Methods[MyApp, validate_request_before_handling: true]
```

- Added `OpenapiFirst::ValidatedRequest#unknown?` and `OpenapiFirst::ValidatedResponse#unknown?`
- Added support for a static `path_prefix` value to be set on the creation of a Definition. See [PR #432](#432):
  ```ruby
  OpenapiFirst.configure do |config|
    config.register('openapi/openapi.yaml' path_prefix: '/weather')
  end
  ```
- Added `OpenapiFirst::Test::Configuration#ignore_response_error` and `OpenapiFirst::Test::Configuration#ignore_request_error` to configure which request/response errors should not raise an error during testing:
  ```ruby
  OpenapiFirst::Test.setup do |test|
    test.ignore_request_error do |validated_request|
      # Ignore unknown requests on certain paths
      validated_request.path.start_with?('/api/v1') && validated_request.unknown?
    end

    test.ignore_response_error do |validated_response, rack_request|
      # Ignore invalid response bodies on certain paths
      validated_request.path.start_with?('/api/legacy/stuff') && validated_request.error.type ==  :invalid_body
    end
  end
  ```
@ahx ahx mentioned this pull request Jan 7, 2026
Merged
ahx added a commit that referenced this pull request Jan 7, 2026
- Changed OpenapiFirst::Test to track the request _after_ the app has handled the request. See [PR #434](#434). You can restore the old behavior with
```ruby
  include OpenapiFirst::Test::Methods[MyApp, validate_request_before_handling: true]
```

- Added `OpenapiFirst::ValidatedRequest#unknown?` and `OpenapiFirst::ValidatedResponse#unknown?`
- Added support for a static `path_prefix` value to be set on the creation of a Definition. See [PR #432](#432):
  ```ruby
  OpenapiFirst.configure do |config|
    config.register('openapi/openapi.yaml' path_prefix: '/weather')
  end
  ```
- Added `OpenapiFirst::Test::Configuration#ignore_response_error` and `OpenapiFirst::Test::Configuration#ignore_request_error` to configure which request/response errors should not raise an error during testing:
  ```ruby
  OpenapiFirst::Test.setup do |test|
    test.ignore_request_error do |validated_request|
      # Ignore unknown requests on certain paths
      validated_request.path.start_with?('/api/v1') && validated_request.unknown?
    end

    test.ignore_response_error do |validated_response, rack_request|
      # Ignore invalid response bodies on certain paths
      validated_request.path.start_with?('/api/legacy/stuff') && validated_request.error.type ==  :invalid_body
    end
  end
  ```
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.

2 participants