Skip to content

Commit 7a01970

Browse files
authored
Merge pull request #358 from ahx/2.7.0
2.7.0 - Allow to override path for schema matching with `config.path = ->(request) { '/prefix' + request.path } ` (#349) - Support passing in a Definition instance when registering an OAD for tests (#353) - Fix registering multiple APIs for testing (#352)
2 parents 4c08314 + ed14ae4 commit 7a01970

10 files changed

Lines changed: 63 additions & 112 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
## Unreleased
44

5-
- Allow to override path for schema matching with `env[OpenapiFirst::PATH]` (https://github.com/ahx/openapi_first/pull/345)
5+
## 2.7.0
6+
7+
- Allow to override path for schema matching with `config.path = ->(request) { '/prefix' + request.path } ` (https://github.com/ahx/openapi_first/issues/349)
8+
- Support passing in a Definition instance when registering an OAD for tests (https://github.com/ahx/openapi_first/issues/353)
9+
- Fix registering multiple APIs for testing (https://github.com/ahx/openapi_first/issues/352)
610

711
## 2.6.0
812

Gemfile.lock

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
openapi_first (2.6.0)
4+
openapi_first (2.7.0)
55
hana (~> 1.3)
66
json_schemer (>= 2.1, < 3.0)
77
openapi_parameters (>= 0.3.3, < 2.0)
@@ -45,7 +45,7 @@ GEM
4545
bigdecimal (3.1.9)
4646
builder (3.3.0)
4747
concurrent-ruby (1.3.5)
48-
connection_pool (2.5.0)
48+
connection_pool (2.5.3)
4949
crass (1.0.6)
5050
diff-lcs (1.6.1)
5151
docile (1.4.1)
@@ -54,15 +54,15 @@ GEM
5454
hana (1.3.7)
5555
i18n (1.14.7)
5656
concurrent-ruby (~> 1.0)
57-
json (2.10.2)
57+
json (2.11.3)
5858
json_schemer (2.4.0)
5959
bigdecimal
6060
hana (~> 1.3)
6161
regexp_parser (~> 2.0)
6262
simpleidn (~> 0.2)
6363
language_server-protocol (3.17.0.4)
6464
lint_roller (1.1.0)
65-
logger (1.6.6)
65+
logger (1.7.0)
6666
loofah (2.24.0)
6767
crass (~> 1.0.2)
6868
nokogiri (>= 1.12.0)
@@ -73,13 +73,13 @@ GEM
7373
racc (~> 1.4)
7474
openapi_parameters (0.5.0)
7575
rack (>= 2.2)
76-
parallel (1.26.3)
77-
parser (3.3.7.2)
76+
parallel (1.27.0)
77+
parser (3.3.8.0)
7878
ast (~> 2.4.1)
7979
racc
8080
prism (1.4.0)
8181
racc (1.8.1)
82-
rack (3.1.12)
82+
rack (3.1.13)
8383
rack-session (2.1.0)
8484
base64 (>= 0.1.0)
8585
rack (>= 3.0.0)
@@ -103,30 +103,30 @@ GEM
103103
rspec-mocks (~> 3.13.0)
104104
rspec-core (3.13.3)
105105
rspec-support (~> 3.13.0)
106-
rspec-expectations (3.13.3)
106+
rspec-expectations (3.13.4)
107107
diff-lcs (>= 1.2.0, < 2.0)
108108
rspec-support (~> 3.13.0)
109-
rspec-mocks (3.13.2)
109+
rspec-mocks (3.13.3)
110110
diff-lcs (>= 1.2.0, < 2.0)
111111
rspec-support (~> 3.13.0)
112-
rspec-support (3.13.2)
113-
rubocop (1.74.0)
112+
rspec-support (3.13.3)
113+
rubocop (1.75.4)
114114
json (~> 2.3)
115115
language_server-protocol (~> 3.17.0.2)
116116
lint_roller (~> 1.1.0)
117117
parallel (~> 1.10)
118118
parser (>= 3.3.0.2)
119119
rainbow (>= 2.2.2, < 4.0)
120120
regexp_parser (>= 2.9.3, < 3.0)
121-
rubocop-ast (>= 1.38.0, < 2.0)
121+
rubocop-ast (>= 1.44.0, < 2.0)
122122
ruby-progressbar (~> 1.7)
123123
unicode-display_width (>= 2.4.0, < 4.0)
124-
rubocop-ast (1.43.0)
124+
rubocop-ast (1.44.1)
125125
parser (>= 3.3.7.2)
126126
prism (~> 1.4)
127-
rubocop-performance (1.24.0)
127+
rubocop-performance (1.25.0)
128128
lint_roller (~> 1.1)
129-
rubocop (>= 1.72.1, < 2.0)
129+
rubocop (>= 1.75.0, < 2.0)
130130
rubocop-ast (>= 1.38.0, < 2.0)
131131
ruby-progressbar (1.13.0)
132132
securerandom (0.4.1)

README.md

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -350,50 +350,7 @@ Here your OpenAPI schema defines endpoints starting with `/resource` but your ac
350350
oad = OpenapiFirst.load('openapi.yaml') do |config|
351351
config.path = ->(req) { request.path.delete_prefix('/api') }
352352
end
353-
# Add your custom middleware
354353
use OpenapiFirst::Middlewares::RequestValidation, oad
355-
356-
# You can add ResponseValidation without any customization.
357-
use OpenapiFirst::Middlewares::ResponseValidation, 'openapi.yaml'
358-
```
359-
360-
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.
361-
362-
You can add some lines to selectively validate only paths under `/api` while bypassing others:
363-
364-
```diff
365-
env[OpenapiFirst::PATH] = request.path.to_s.sub(%r"^/api", "")
366-
367-
+ # Only validate paths under /api/
368-
+ if request.path.start_with?('/api/')
369-
super
370-
+ else
371-
+ @app.call(env)
372-
+ end
373-
end
374-
```
375-
376-
And the final code is:
377-
378-
```ruby
379-
class CustomOpenAPIValidation < OpenapiFirst::Middlewares::RequestValidation
380-
def call(env)
381-
request = Rack::Request.new(env)
382-
383-
# Strip the "/api" prefix for schema matching
384-
env[OpenapiFirst::PATH] = request.path.to_s.sub(%r"^/api", "")
385-
386-
# Only validate paths under /api/
387-
if request.path.start_with?('/api/')
388-
super
389-
else
390-
@app.call(env)
391-
end
392-
end
393-
end
394-
395-
use CustomOpenAPIValidation, 'openapi.yaml'
396-
use OpenapiFirst::Middlewares::ResponseValidation, 'openapi.yaml'
397354
```
398355

399356
## Development

benchmarks/Gemfile.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: ..
33
specs:
4-
openapi_first (2.6.0)
4+
openapi_first (2.7.0)
55
hana (~> 1.3)
66
json_schemer (>= 2.1, < 3.0)
77
openapi_parameters (>= 0.3.3, < 2.0)
@@ -16,7 +16,7 @@ GEM
1616
benchmark-memory (0.2.0)
1717
memory_profiler (~> 1)
1818
bigdecimal (3.1.9)
19-
committee (5.5.2)
19+
committee (5.5.3)
2020
json_schema (~> 0.14, >= 0.14.3)
2121
openapi_parser (~> 2.0)
2222
rack (>= 1.5, < 3.2)
@@ -34,14 +34,14 @@ GEM
3434
nio4r (2.7.4)
3535
openapi_parameters (0.5.0)
3636
rack (>= 2.2)
37-
openapi_parser (2.2.4)
37+
openapi_parser (2.2.6)
3838
optparse (0.6.0)
3939
profile-viewer (0.0.4)
4040
optparse
4141
webrick
4242
puma (6.6.0)
4343
nio4r (~> 2.0)
44-
rack (3.1.12)
44+
rack (3.1.13)
4545
rack-protection (4.1.1)
4646
base64 (>= 0.1.0)
4747
logger (>= 1.6.0)

examples/rails_app/Gemfile.lock

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: ../..
33
specs:
4-
openapi_first (2.2.3)
4+
openapi_first (2.7.0)
55
hana (~> 1.3)
66
json_schemer (>= 2.1, < 3.0)
77
openapi_parameters (>= 0.3.3, < 2.0)
@@ -88,29 +88,30 @@ GEM
8888
msgpack (~> 1.2)
8989
builder (3.3.0)
9090
concurrent-ruby (1.3.5)
91-
connection_pool (2.5.0)
91+
connection_pool (2.5.3)
9292
crass (1.0.6)
9393
date (3.4.1)
94-
debug (1.9.2)
94+
debug (1.10.0)
9595
irb (~> 1.10)
9696
reline (>= 0.3.8)
9797
drb (2.2.1)
9898
erubi (1.13.1)
9999
globalid (1.2.1)
100100
activesupport (>= 6.1)
101101
hana (1.3.7)
102-
i18n (1.14.6)
102+
i18n (1.14.7)
103103
concurrent-ruby (~> 1.0)
104104
io-console (0.8.0)
105-
irb (1.14.3)
105+
irb (1.15.2)
106+
pp (>= 0.6.0)
106107
rdoc (>= 4.0.0)
107108
reline (>= 0.4.2)
108-
json_schemer (2.3.0)
109+
json_schemer (2.4.0)
109110
bigdecimal
110111
hana (~> 1.3)
111112
regexp_parser (~> 2.0)
112113
simpleidn (~> 0.2)
113-
logger (1.6.5)
114+
logger (1.7.0)
114115
loofah (2.24.0)
115116
crass (~> 1.0.2)
116117
nokogiri (>= 1.12.0)
@@ -122,40 +123,43 @@ GEM
122123
marcel (1.0.4)
123124
mini_mime (1.1.5)
124125
mini_portile2 (2.8.8)
125-
minitest (5.25.4)
126-
msgpack (1.7.3)
127-
net-imap (0.5.5)
126+
minitest (5.25.5)
127+
msgpack (1.8.0)
128+
net-imap (0.5.8)
128129
date
129130
net-protocol
130131
net-pop (0.1.2)
131132
net-protocol
132133
net-protocol (0.2.2)
133134
timeout
134-
net-smtp (0.5.0)
135+
net-smtp (0.5.1)
135136
net-protocol
136137
nio4r (2.7.4)
137-
nokogiri (1.18.1)
138+
nokogiri (1.18.8)
138139
mini_portile2 (~> 2.8.2)
139140
racc (~> 1.4)
140-
nokogiri (1.18.1-aarch64-linux-gnu)
141+
nokogiri (1.18.8-aarch64-linux-gnu)
141142
racc (~> 1.4)
142-
nokogiri (1.18.1-arm-linux-gnu)
143+
nokogiri (1.18.8-arm-linux-gnu)
143144
racc (~> 1.4)
144-
nokogiri (1.18.1-arm64-darwin)
145+
nokogiri (1.18.8-arm64-darwin)
145146
racc (~> 1.4)
146-
nokogiri (1.18.1-x86_64-darwin)
147+
nokogiri (1.18.8-x86_64-darwin)
147148
racc (~> 1.4)
148-
nokogiri (1.18.1-x86_64-linux-gnu)
149+
nokogiri (1.18.8-x86_64-linux-gnu)
149150
racc (~> 1.4)
150-
openapi_parameters (0.4.0)
151+
openapi_parameters (0.5.0)
151152
rack (>= 2.2)
152-
psych (5.2.2)
153+
pp (0.6.2)
154+
prettyprint
155+
prettyprint (0.2.0)
156+
psych (5.2.4)
153157
date
154158
stringio
155-
puma (6.4.3)
159+
puma (6.6.0)
156160
nio4r (~> 2.0)
157161
racc (1.8.1)
158-
rack (3.1.8)
162+
rack (3.1.13)
159163
rack-session (2.1.0)
160164
base64 (>= 0.1.0)
161165
rack (>= 3.0.0)
@@ -193,14 +197,14 @@ GEM
193197
thor (~> 1.0, >= 1.2.2)
194198
zeitwerk (~> 2.6)
195199
rake (13.2.1)
196-
rdoc (6.11.0)
200+
rdoc (6.13.1)
197201
psych (>= 4.0.0)
198202
regexp_parser (2.10.0)
199-
reline (0.6.0)
203+
reline (0.6.1)
200204
io-console (~> 0.5)
201205
securerandom (0.4.1)
202206
simpleidn (0.2.3)
203-
stringio (3.1.2)
207+
stringio (3.1.7)
204208
thor (1.3.2)
205209
timeout (0.4.3)
206210
tzinfo (2.0.6)
@@ -210,7 +214,7 @@ GEM
210214
base64
211215
websocket-extensions (>= 0.1.0)
212216
websocket-extensions (0.1.5)
213-
zeitwerk (2.7.1)
217+
zeitwerk (2.7.2)
214218

215219
PLATFORMS
216220
aarch64-linux

examples/rails_app/test/integration/attachments_api_test.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
require 'test_helper'
44

55
class StationsApiTest < ActionDispatch::IntegrationTest
6-
include OpenapiFirst::Test::Methods
7-
86
test 'GET /attachments/{attachment_id}' do
97
get '/attachments/42abc', as: :json
8+
109
assert_api_conform(status: 200, api: :attachments)
1110
end
1211
end

examples/rails_app/test/integration/stations_api_test.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
require 'test_helper'
44

55
class StationsApiTest < ActionDispatch::IntegrationTest
6-
include OpenapiFirst::Test::Methods
7-
86
test 'GET /stations' do
97
get '/stations'
108

examples/rails_app/test/integration/trips_api_test.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
require 'test_helper'
22

33
class TripsApiTest < ActionDispatch::IntegrationTest
4-
include OpenapiFirst::Test::Methods
5-
64
test 'GET /trips' do
75
get '/trips',
86
params: { origin: 'efdbb9d1-02c2-4bc3-afb7-6788d8782b1e', destination: 'b2e783e1-c824-4d63-b37a-d8d698862f1d',
Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
ENV['RAILS_ENV'] ||= 'test'
2-
require_relative '../config/environment'
3-
require 'rails/test_help'
42

53
require 'openapi_first'
6-
OpenapiFirst::Test.register(Rails.root.join('../../spec/data/train-travel-api/openapi.yaml'))
7-
OpenapiFirst::Test.register(Rails.root.join('../../spec/data/attachments_openapi.yaml'), as: :attachments)
8-
9-
OpenapiFirst::Test::Coverage.register(
10-
Rails.root.join('../../spec/data/train-travel-api/openapi.yaml'),
11-
Rails.root.join('../../spec/data/attachments_openapi.yaml')
12-
)
13-
OpenapiFirst::Test::Coverage.start
14-
15-
Minitest.after_run do
16-
OpenapiFirst::Test::Coverage.report
4+
OpenapiFirst::Test.setup do |test|
5+
test.register Rails.root.join('../../spec/data/train-travel-api/openapi.yaml')
6+
test.register Rails.root.join('../../spec/data/attachments_openapi.yaml'), as: :attachments
7+
test.coverage_formatter_options = { verbose: true }
178
end
189

10+
require_relative '../config/environment'
11+
require 'rails/test_help'
12+
1913
module ActiveSupport
2014
class TestCase
2115
# Run tests in parallel with specified workers
@@ -25,12 +19,9 @@ class TestCase
2519
end
2620
end
2721

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

3023
module ActionDispatch
3124
class IntegrationTest
32-
def app
33-
TEST_APP
34-
end
25+
include OpenapiFirst::Test::Methods[TrainTravel::Application]
3526
end
3627
end

0 commit comments

Comments
 (0)