File tree Expand file tree Collapse file tree 5 files changed +90
-0
lines changed
lib/openapi_parser/schema_validator Expand file tree Collapse file tree 5 files changed +90
-0
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,10 @@ class AllOfValidator < Base
5
5
# @param [Object] value
6
6
# @param [OpenAPIParser::Schemas::Schema] schema
7
7
def coerce_and_validate ( value , schema , **keyword_args )
8
+ if value . nil? && schema . nullable
9
+ return [ value , nil ]
10
+ end
11
+
8
12
# if any schema return error, it's not valida all of value
9
13
remaining_keys = value . kind_of? ( Hash ) ? value . keys : [ ]
10
14
nested_additional_properties = false
Original file line number Diff line number Diff line change @@ -3,6 +3,9 @@ class AnyOfValidator < Base
3
3
# @param [Object] value
4
4
# @param [OpenAPIParser::Schemas::Schema] schema
5
5
def coerce_and_validate ( value , schema , **_keyword_args )
6
+ if value . nil? && schema . nullable
7
+ return [ value , nil ]
8
+ end
6
9
if schema . discriminator
7
10
return validate_discriminator_schema ( schema . discriminator , value )
8
11
end
Original file line number Diff line number Diff line change @@ -3,6 +3,9 @@ class OneOfValidator < Base
3
3
# @param [Object] value
4
4
# @param [OpenAPIParser::Schemas::Schema] schema
5
5
def coerce_and_validate ( value , schema , **_keyword_args )
6
+ if value . nil? && schema . nullable
7
+ return [ value , nil ]
8
+ end
6
9
if schema . discriminator
7
10
return validate_discriminator_schema ( schema . discriminator , value )
8
11
end
Original file line number Diff line number Diff line change @@ -245,6 +245,10 @@ paths:
245
245
id :
246
246
type : integer
247
247
format : int64
248
+ all_of_with_nullable :
249
+ nullable : true
250
+ allOf :
251
+ - type : integer
248
252
one_of_data :
249
253
oneOf :
250
254
- $ref : ' #/components/schemas/one_of_object1'
@@ -258,6 +262,10 @@ paths:
258
262
mapping :
259
263
obj1 : ' #/components/schemas/one_of_object1'
260
264
obj2 : ' #/components/schemas/one_of_object2'
265
+ one_of_with_nullable :
266
+ nullable : true
267
+ oneOf :
268
+ - type : integer
261
269
object_1 :
262
270
type : object
263
271
properties :
@@ -314,6 +322,10 @@ paths:
314
322
anyOf :
315
323
- type : string
316
324
- type : boolean
325
+ any_of_with_nullable :
326
+ nullable : true
327
+ anyOf :
328
+ - type : integer
317
329
unspecified_type : {}
318
330
enum_string :
319
331
type : string
Original file line number Diff line number Diff line change 209
209
end
210
210
end
211
211
212
+ context 'anyOf with nullable' do
213
+ subject { request_operation . validate_request_body ( content_type , { 'any_of_with_nullable' => params } ) }
214
+
215
+ context 'integer' do
216
+ let ( :params ) { 1 }
217
+
218
+ it { expect ( subject ) . to eq ( { 'any_of_with_nullable' => 1 } ) }
219
+ end
220
+
221
+ context 'null' do
222
+ let ( :params ) { nil }
223
+
224
+ it { expect ( subject ) . to eq ( { 'any_of_with_nullable' => nil } ) }
225
+ end
226
+
227
+ context 'invalid' do
228
+ let ( :params ) { 'foo' }
229
+
230
+ it { expect { subject } . to raise_error ( OpenAPIParser ::NotAnyOf ) }
231
+ end
232
+ end
233
+
212
234
context 'unspecified_type' do
213
235
it do
214
236
expect ( request_operation . validate_request_body ( content_type , { 'unspecified_type' => "foo" } ) ) .
369
391
end
370
392
end
371
393
394
+ describe 'allOf with nullable' do
395
+ context 'with nullable' do
396
+ subject { request_operation . validate_request_body ( content_type , { 'all_of_with_nullable' => params } ) }
397
+
398
+ context 'integer' do
399
+ let ( :params ) { 1 }
400
+
401
+ it { expect ( subject ) . to eq ( { 'all_of_with_nullable' => 1 } ) }
402
+ end
403
+
404
+ context 'null' do
405
+ let ( :params ) { nil }
406
+
407
+ it { expect ( subject ) . to eq ( { 'all_of_with_nullable' => nil } ) }
408
+ end
409
+
410
+ context 'invalid' do
411
+ let ( :params ) { 'foo' }
412
+
413
+ it { expect { subject } . to raise_error ( ::OpenAPIParser ::ValidateError ) }
414
+ end
415
+ end
416
+ end
417
+
372
418
describe 'one_of' do
373
419
context 'normal' do
374
420
subject { request_operation . validate_request_body ( content_type , { 'one_of_data' => params } ) }
430
476
431
477
it { expect ( subject ) . not_to eq nil }
432
478
end
479
+
480
+ context 'with nullable' do
481
+ subject { request_operation . validate_request_body ( content_type , { 'one_of_with_nullable' => params } ) }
482
+
483
+ context 'integer' do
484
+ let ( :params ) { 1 }
485
+
486
+ it { expect ( subject ) . to eq ( { 'one_of_with_nullable' => 1 } ) }
487
+ end
488
+
489
+ context 'null' do
490
+ let ( :params ) { nil }
491
+
492
+ it { expect ( subject ) . to eq ( { 'one_of_with_nullable' => nil } ) }
493
+ end
494
+
495
+ context 'invalid' do
496
+ let ( :params ) { 'foo' }
497
+
498
+ it { expect { subject } . to raise_error ( OpenAPIParser ::NotOneOf ) }
499
+ end
500
+ end
433
501
end
434
502
435
503
it 'unknown param' do
You can’t perform that action at this time.
0 commit comments