diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f303528..ec498c0c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Gemfile.lock b/Gemfile.lock index dae8e339..9f95e130 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -54,7 +54,7 @@ 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) @@ -62,7 +62,7 @@ GEM 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) @@ -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) @@ -103,14 +103,14 @@ 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) @@ -118,15 +118,15 @@ GEM 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) diff --git a/README.md b/README.md index 7a0bef82..8684db6d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/benchmarks/Gemfile.lock b/benchmarks/Gemfile.lock index d677ad25..43b09ee6 100644 --- a/benchmarks/Gemfile.lock +++ b/benchmarks/Gemfile.lock @@ -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) @@ -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) @@ -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) diff --git a/examples/rails_app/Gemfile.lock b/examples/rails_app/Gemfile.lock index 9c13a335..038f61db 100644 --- a/examples/rails_app/Gemfile.lock +++ b/examples/rails_app/Gemfile.lock @@ -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) @@ -88,10 +88,10 @@ 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) @@ -99,18 +99,19 @@ GEM 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) @@ -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) @@ -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) @@ -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 diff --git a/examples/rails_app/test/integration/attachments_api_test.rb b/examples/rails_app/test/integration/attachments_api_test.rb index b2276d4d..f1e89e34 100644 --- a/examples/rails_app/test/integration/attachments_api_test.rb +++ b/examples/rails_app/test/integration/attachments_api_test.rb @@ -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 diff --git a/examples/rails_app/test/integration/stations_api_test.rb b/examples/rails_app/test/integration/stations_api_test.rb index f067bd0d..0ec8d23a 100644 --- a/examples/rails_app/test/integration/stations_api_test.rb +++ b/examples/rails_app/test/integration/stations_api_test.rb @@ -3,8 +3,6 @@ require 'test_helper' class StationsApiTest < ActionDispatch::IntegrationTest - include OpenapiFirst::Test::Methods - test 'GET /stations' do get '/stations' diff --git a/examples/rails_app/test/integration/trips_api_test.rb b/examples/rails_app/test/integration/trips_api_test.rb index d97dff91..54592cbc 100644 --- a/examples/rails_app/test/integration/trips_api_test.rb +++ b/examples/rails_app/test/integration/trips_api_test.rb @@ -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', diff --git a/examples/rails_app/test/test_helper.rb b/examples/rails_app/test/test_helper.rb index ca271d3d..1bce6720 100644 --- a/examples/rails_app/test/test_helper.rb +++ b/examples/rails_app/test/test_helper.rb @@ -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 @@ -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 diff --git a/lib/openapi_first/version.rb b/lib/openapi_first/version.rb index 0e64a6e1..e39f983b 100644 --- a/lib/openapi_first/version.rb +++ b/lib/openapi_first/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module OpenapiFirst - VERSION = '2.6.0' + VERSION = '2.7.0' end