Skip to content

Commit 1c38387

Browse files
committed
Fix using middlwares with registered OAD
1 parent 49e863b commit 1c38387

4 files changed

Lines changed: 62 additions & 2 deletions

File tree

lib/openapi_first/middlewares/request_validation.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def initialize(app, spec = nil, options = {})
2222
@app = app
2323
if spec.is_a?(Hash)
2424
options = spec
25-
spec = options.fetch(:spec)
25+
spec = options[:spec]
2626
end
2727
@raise = options.fetch(:raise_error, OpenapiFirst.configuration.request_validation_raise_error)
2828
@error_response_class = error_response_option(options[:error_response])

lib/openapi_first/middlewares/response_validation.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def initialize(app, spec = nil, options = {})
1717
@app = app
1818
if spec.is_a?(Hash)
1919
options = spec
20-
spec = options.fetch(:spec)
20+
spec = options[:spec]
2121
end
2222
@raise = options.fetch(:raise_error, OpenapiFirst.configuration.response_validation_raise_error)
2323

spec/middlewares/request_validation_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,25 @@
141141
end
142142
end
143143

144+
context 'with option and registered OAD' do
145+
before do
146+
OpenapiFirst.register('./spec/data/request-body-validation.yaml')
147+
end
148+
149+
let(:app) do
150+
Rack::Builder.app do
151+
use OpenapiFirst::Middlewares::RequestValidation, raise_error: true
152+
run ->(_) {}
153+
end
154+
end
155+
156+
it 'returns 400' do
157+
expect do
158+
post '/pets'
159+
end.to raise_error OpenapiFirst::RequestInvalidError
160+
end
161+
end
162+
144163
context 'with error_response: MyCustomClass' do
145164
let(:app) do
146165
custom_class = Class.new do

spec/middlewares/response_validation_spec.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,26 @@
4747
end
4848
end
4949

50+
context 'without spec argument and option' do
51+
let(:response_body) { '2' }
52+
53+
let(:app) do
54+
res = response
55+
definition = spec
56+
Rack::Builder.app do
57+
use Rack::Lint
58+
use OpenapiFirst::Middlewares::ResponseValidation, spec: definition
59+
run ->(_env) { res.finish }
60+
end
61+
end
62+
63+
it 'fails if request is inavlid' do
64+
expect do
65+
get '/pets'
66+
end.to raise_error OpenapiFirst::ResponseInvalidError
67+
end
68+
end
69+
5070
context 'without content-type header' do
5171
let(:headers) do
5272
{ 'X-HEAD' => '/api/next-page' }
@@ -286,6 +306,27 @@
286306
expect(last_response.status).to eq 200
287307
end
288308
end
309+
310+
context 'with option and registered OAD' do
311+
before do
312+
OpenapiFirst.register('./spec/data/petstore.yaml')
313+
end
314+
315+
let(:app) do
316+
Rack::Builder.new.tap do |builder|
317+
builder.use(described_class, raise_error: false)
318+
builder.run lambda { |_env|
319+
Rack::Response.new('{"foo": "bar"}', 200, { 'Content-Type' => 'application/json' }).finish
320+
}
321+
end.to_app
322+
end
323+
324+
it 'does not raise an error' do
325+
get '/pets'
326+
327+
expect(last_response.status).to eq 200
328+
end
329+
end
289330
end
290331

291332
context 'when response is not valid JSON' do

0 commit comments

Comments
 (0)