Skip to content
Merged

2.7.0 #358

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

## Unreleased

- Allow to override path for schema matching with `env[OpenapiFirst::PATH]` (https://github.com/ahx/openapi_first/pull/345)
## 2.7.0

- Allow to override path for schema matching with `config.path = ->(request) { '/prefix' + request.path } ` (https://github.com/ahx/openapi_first/issues/349)
- Support passing in a Definition instance when registering an OAD for tests (https://github.com/ahx/openapi_first/issues/353)
- Fix registering multiple APIs for testing (https://github.com/ahx/openapi_first/issues/352)

## 2.6.0

Expand Down
30 changes: 15 additions & 15 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
openapi_first (2.6.0)
openapi_first (2.7.0)
hana (~> 1.3)
json_schemer (>= 2.1, < 3.0)
openapi_parameters (>= 0.3.3, < 2.0)
Expand Down Expand Up @@ -45,7 +45,7 @@ GEM
bigdecimal (3.1.9)
builder (3.3.0)
concurrent-ruby (1.3.5)
connection_pool (2.5.0)
connection_pool (2.5.3)
crass (1.0.6)
diff-lcs (1.6.1)
docile (1.4.1)
Expand All @@ -54,15 +54,15 @@ GEM
hana (1.3.7)
i18n (1.14.7)
concurrent-ruby (~> 1.0)
json (2.10.2)
json (2.11.3)
json_schemer (2.4.0)
bigdecimal
hana (~> 1.3)
regexp_parser (~> 2.0)
simpleidn (~> 0.2)
language_server-protocol (3.17.0.4)
lint_roller (1.1.0)
logger (1.6.6)
logger (1.7.0)
loofah (2.24.0)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
Expand All @@ -73,13 +73,13 @@ GEM
racc (~> 1.4)
openapi_parameters (0.5.0)
rack (>= 2.2)
parallel (1.26.3)
parser (3.3.7.2)
parallel (1.27.0)
parser (3.3.8.0)
ast (~> 2.4.1)
racc
prism (1.4.0)
racc (1.8.1)
rack (3.1.12)
rack (3.1.13)
rack-session (2.1.0)
base64 (>= 0.1.0)
rack (>= 3.0.0)
Expand All @@ -103,30 +103,30 @@ GEM
rspec-mocks (~> 3.13.0)
rspec-core (3.13.3)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.3)
rspec-expectations (3.13.4)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.2)
rspec-mocks (3.13.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-support (3.13.2)
rubocop (1.74.0)
rspec-support (3.13.3)
rubocop (1.75.4)
json (~> 2.3)
language_server-protocol (~> 3.17.0.2)
lint_roller (~> 1.1.0)
parallel (~> 1.10)
parser (>= 3.3.0.2)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 2.9.3, < 3.0)
rubocop-ast (>= 1.38.0, < 2.0)
rubocop-ast (>= 1.44.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 4.0)
rubocop-ast (1.43.0)
rubocop-ast (1.44.1)
parser (>= 3.3.7.2)
prism (~> 1.4)
rubocop-performance (1.24.0)
rubocop-performance (1.25.0)
lint_roller (~> 1.1)
rubocop (>= 1.72.1, < 2.0)
rubocop (>= 1.75.0, < 2.0)
rubocop-ast (>= 1.38.0, < 2.0)
ruby-progressbar (1.13.0)
securerandom (0.4.1)
Expand Down
43 changes: 0 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -350,50 +350,7 @@ Here your OpenAPI schema defines endpoints starting with `/resource` but your ac
oad = OpenapiFirst.load('openapi.yaml') do |config|
config.path = ->(req) { request.path.delete_prefix('/api') }
end
# Add your custom middleware
use OpenapiFirst::Middlewares::RequestValidation, oad

# You can add ResponseValidation without any customization.
use OpenapiFirst::Middlewares::ResponseValidation, 'openapi.yaml'
```

In this case, you might want to serve APIs on `/api` while serving rendered pages on other paths which are not managed by OpenAPI schema in a single application.

You can add some lines to selectively validate only paths under `/api` while bypassing others:

```diff
env[OpenapiFirst::PATH] = request.path.to_s.sub(%r"^/api", "")

+ # Only validate paths under /api/
+ if request.path.start_with?('/api/')
super
+ else
+ @app.call(env)
+ end
end
```

And the final code is:

```ruby
class CustomOpenAPIValidation < OpenapiFirst::Middlewares::RequestValidation
def call(env)
request = Rack::Request.new(env)

# Strip the "/api" prefix for schema matching
env[OpenapiFirst::PATH] = request.path.to_s.sub(%r"^/api", "")

# Only validate paths under /api/
if request.path.start_with?('/api/')
super
else
@app.call(env)
end
end
end

use CustomOpenAPIValidation, 'openapi.yaml'
use OpenapiFirst::Middlewares::ResponseValidation, 'openapi.yaml'
```

## Development
Expand Down
8 changes: 4 additions & 4 deletions benchmarks/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
openapi_first (2.6.0)
openapi_first (2.7.0)
hana (~> 1.3)
json_schemer (>= 2.1, < 3.0)
openapi_parameters (>= 0.3.3, < 2.0)
Expand All @@ -16,7 +16,7 @@ GEM
benchmark-memory (0.2.0)
memory_profiler (~> 1)
bigdecimal (3.1.9)
committee (5.5.2)
committee (5.5.3)
json_schema (~> 0.14, >= 0.14.3)
openapi_parser (~> 2.0)
rack (>= 1.5, < 3.2)
Expand All @@ -34,14 +34,14 @@ GEM
nio4r (2.7.4)
openapi_parameters (0.5.0)
rack (>= 2.2)
openapi_parser (2.2.4)
openapi_parser (2.2.6)
optparse (0.6.0)
profile-viewer (0.0.4)
optparse
webrick
puma (6.6.0)
nio4r (~> 2.0)
rack (3.1.12)
rack (3.1.13)
rack-protection (4.1.1)
base64 (>= 0.1.0)
logger (>= 1.6.0)
Expand Down
54 changes: 29 additions & 25 deletions examples/rails_app/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ../..
specs:
openapi_first (2.2.3)
openapi_first (2.7.0)
hana (~> 1.3)
json_schemer (>= 2.1, < 3.0)
openapi_parameters (>= 0.3.3, < 2.0)
Expand Down Expand Up @@ -88,29 +88,30 @@ GEM
msgpack (~> 1.2)
builder (3.3.0)
concurrent-ruby (1.3.5)
connection_pool (2.5.0)
connection_pool (2.5.3)
crass (1.0.6)
date (3.4.1)
debug (1.9.2)
debug (1.10.0)
irb (~> 1.10)
reline (>= 0.3.8)
drb (2.2.1)
erubi (1.13.1)
globalid (1.2.1)
activesupport (>= 6.1)
hana (1.3.7)
i18n (1.14.6)
i18n (1.14.7)
concurrent-ruby (~> 1.0)
io-console (0.8.0)
irb (1.14.3)
irb (1.15.2)
pp (>= 0.6.0)
rdoc (>= 4.0.0)
reline (>= 0.4.2)
json_schemer (2.3.0)
json_schemer (2.4.0)
bigdecimal
hana (~> 1.3)
regexp_parser (~> 2.0)
simpleidn (~> 0.2)
logger (1.6.5)
logger (1.7.0)
loofah (2.24.0)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
Expand All @@ -122,40 +123,43 @@ GEM
marcel (1.0.4)
mini_mime (1.1.5)
mini_portile2 (2.8.8)
minitest (5.25.4)
msgpack (1.7.3)
net-imap (0.5.5)
minitest (5.25.5)
msgpack (1.8.0)
net-imap (0.5.8)
date
net-protocol
net-pop (0.1.2)
net-protocol
net-protocol (0.2.2)
timeout
net-smtp (0.5.0)
net-smtp (0.5.1)
net-protocol
nio4r (2.7.4)
nokogiri (1.18.1)
nokogiri (1.18.8)
mini_portile2 (~> 2.8.2)
racc (~> 1.4)
nokogiri (1.18.1-aarch64-linux-gnu)
nokogiri (1.18.8-aarch64-linux-gnu)
racc (~> 1.4)
nokogiri (1.18.1-arm-linux-gnu)
nokogiri (1.18.8-arm-linux-gnu)
racc (~> 1.4)
nokogiri (1.18.1-arm64-darwin)
nokogiri (1.18.8-arm64-darwin)
racc (~> 1.4)
nokogiri (1.18.1-x86_64-darwin)
nokogiri (1.18.8-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.18.1-x86_64-linux-gnu)
nokogiri (1.18.8-x86_64-linux-gnu)
racc (~> 1.4)
openapi_parameters (0.4.0)
openapi_parameters (0.5.0)
rack (>= 2.2)
psych (5.2.2)
pp (0.6.2)
prettyprint
prettyprint (0.2.0)
psych (5.2.4)
date
stringio
puma (6.4.3)
puma (6.6.0)
nio4r (~> 2.0)
racc (1.8.1)
rack (3.1.8)
rack (3.1.13)
rack-session (2.1.0)
base64 (>= 0.1.0)
rack (>= 3.0.0)
Expand Down Expand Up @@ -193,14 +197,14 @@ GEM
thor (~> 1.0, >= 1.2.2)
zeitwerk (~> 2.6)
rake (13.2.1)
rdoc (6.11.0)
rdoc (6.13.1)
psych (>= 4.0.0)
regexp_parser (2.10.0)
reline (0.6.0)
reline (0.6.1)
io-console (~> 0.5)
securerandom (0.4.1)
simpleidn (0.2.3)
stringio (3.1.2)
stringio (3.1.7)
thor (1.3.2)
timeout (0.4.3)
tzinfo (2.0.6)
Expand All @@ -210,7 +214,7 @@ GEM
base64
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
zeitwerk (2.7.1)
zeitwerk (2.7.2)

PLATFORMS
aarch64-linux
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
require 'test_helper'

class StationsApiTest < ActionDispatch::IntegrationTest
include OpenapiFirst::Test::Methods

test 'GET /attachments/{attachment_id}' do
get '/attachments/42abc', as: :json

assert_api_conform(status: 200, api: :attachments)
end
end
2 changes: 0 additions & 2 deletions examples/rails_app/test/integration/stations_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
require 'test_helper'

class StationsApiTest < ActionDispatch::IntegrationTest
include OpenapiFirst::Test::Methods

test 'GET /stations' do
get '/stations'

Expand Down
2 changes: 0 additions & 2 deletions examples/rails_app/test/integration/trips_api_test.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
require 'test_helper'

class TripsApiTest < ActionDispatch::IntegrationTest
include OpenapiFirst::Test::Methods

test 'GET /trips' do
get '/trips',
params: { origin: 'efdbb9d1-02c2-4bc3-afb7-6788d8782b1e', destination: 'b2e783e1-c824-4d63-b37a-d8d698862f1d',
Expand Down
25 changes: 8 additions & 17 deletions examples/rails_app/test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
require 'rails/test_help'

require 'openapi_first'
OpenapiFirst::Test.register(Rails.root.join('../../spec/data/train-travel-api/openapi.yaml'))
OpenapiFirst::Test.register(Rails.root.join('../../spec/data/attachments_openapi.yaml'), as: :attachments)

OpenapiFirst::Test::Coverage.register(
Rails.root.join('../../spec/data/train-travel-api/openapi.yaml'),
Rails.root.join('../../spec/data/attachments_openapi.yaml')
)
OpenapiFirst::Test::Coverage.start

Minitest.after_run do
OpenapiFirst::Test::Coverage.report
OpenapiFirst::Test.setup do |test|
test.register Rails.root.join('../../spec/data/train-travel-api/openapi.yaml')
test.register Rails.root.join('../../spec/data/attachments_openapi.yaml'), as: :attachments
test.coverage_formatter_options = { verbose: true }
end

require_relative '../config/environment'
require 'rails/test_help'

module ActiveSupport
class TestCase
# Run tests in parallel with specified workers
Expand All @@ -25,12 +19,9 @@ class TestCase
end
end

TEST_APP = OpenapiFirst::Test.app(TrainTravel::Application, spec: Rails.root.join('../../spec/data/attachments_openapi.yaml'))

module ActionDispatch
class IntegrationTest
def app
TEST_APP
end
include OpenapiFirst::Test::Methods[TrainTravel::Application]
end
end
Loading
Loading