1
1
import pytest
2
2
from jsonschema import ValidationError
3
3
4
+ from openapi_schema_validator import OAS30ReadValidator
5
+ from openapi_schema_validator import OAS30WriteValidator
4
6
from openapi_schema_validator import OAS30Validator
5
7
from openapi_schema_validator import OAS31Validator
6
8
from openapi_schema_validator import oas30_format_checker
@@ -258,16 +260,18 @@ def test_read_only(self):
258
260
"properties" : {"some_prop" : {"type" : "string" , "readOnly" : True }},
259
261
}
260
262
261
- validator = OAS30Validator (
262
- schema , format_checker = oas30_format_checker , write = True
263
- )
263
+ with pytest .warns (DeprecationWarning ):
264
+ validator = OAS30Validator (
265
+ schema , format_checker = oas30_format_checker , write = True
266
+ )
264
267
with pytest .raises (
265
268
ValidationError , match = "Tried to write read-only property with hello"
266
269
):
267
270
validator .validate ({"some_prop" : "hello" })
268
- validator = OAS30Validator (
269
- schema , format_checker = oas30_format_checker , read = True
270
- )
271
+ with pytest .warns (DeprecationWarning ):
272
+ validator = OAS30Validator (
273
+ schema , format_checker = oas30_format_checker , read = True
274
+ )
271
275
assert validator .validate ({"some_prop" : "hello" }) is None
272
276
273
277
def test_write_only (self ):
@@ -276,16 +280,18 @@ def test_write_only(self):
276
280
"properties" : {"some_prop" : {"type" : "string" , "writeOnly" : True }},
277
281
}
278
282
279
- validator = OAS30Validator (
280
- schema , format_checker = oas30_format_checker , read = True
281
- )
283
+ with pytest .warns (DeprecationWarning ):
284
+ validator = OAS30Validator (
285
+ schema , format_checker = oas30_format_checker , read = True
286
+ )
282
287
with pytest .raises (
283
288
ValidationError , match = "Tried to read write-only property with hello"
284
289
):
285
290
validator .validate ({"some_prop" : "hello" })
286
- validator = OAS30Validator (
287
- schema , format_checker = oas30_format_checker , write = True
288
- )
291
+ with pytest .warns (DeprecationWarning ):
292
+ validator = OAS30Validator (
293
+ schema , format_checker = oas30_format_checker , write = True
294
+ )
289
295
assert validator .validate ({"some_prop" : "hello" }) is None
290
296
291
297
def test_required_read_only (self ):
@@ -295,16 +301,18 @@ def test_required_read_only(self):
295
301
"required" : ["some_prop" ],
296
302
}
297
303
298
- validator = OAS30Validator (
299
- schema , format_checker = oas30_format_checker , read = True
300
- )
304
+ with pytest .warns (DeprecationWarning ):
305
+ validator = OAS30Validator (
306
+ schema , format_checker = oas30_format_checker , read = True
307
+ )
301
308
with pytest .raises (
302
309
ValidationError , match = "'some_prop' is a required property"
303
310
):
304
311
validator .validate ({"another_prop" : "hello" })
305
- validator = OAS30Validator (
306
- schema , format_checker = oas30_format_checker , write = True
307
- )
312
+ with pytest .warns (DeprecationWarning ):
313
+ validator = OAS30Validator (
314
+ schema , format_checker = oas30_format_checker , write = True
315
+ )
308
316
assert validator .validate ({"another_prop" : "hello" }) is None
309
317
310
318
def test_required_write_only (self ):
@@ -314,16 +322,18 @@ def test_required_write_only(self):
314
322
"required" : ["some_prop" ],
315
323
}
316
324
317
- validator = OAS30Validator (
318
- schema , format_checker = oas30_format_checker , write = True
319
- )
325
+ with pytest .warns (DeprecationWarning ):
326
+ validator = OAS30Validator (
327
+ schema , format_checker = oas30_format_checker , write = True
328
+ )
320
329
with pytest .raises (
321
330
ValidationError , match = "'some_prop' is a required property"
322
331
):
323
332
validator .validate ({"another_prop" : "hello" })
324
- validator = OAS30Validator (
325
- schema , format_checker = oas30_format_checker , read = True
326
- )
333
+ with pytest .warns (DeprecationWarning ):
334
+ validator = OAS30Validator (
335
+ schema , format_checker = oas30_format_checker , read = True
336
+ )
327
337
assert validator .validate ({"another_prop" : "hello" }) is None
328
338
329
339
def test_oneof_required (self ):
@@ -567,6 +577,84 @@ def test_nullable_schema_combos(self, is_nullable, schema_type, not_nullable_reg
567
577
validator .validate ({"testfield" : None })
568
578
assert False
569
579
580
+
581
+ class TestOAS30ReadWriteValidatorValidate :
582
+
583
+ def test_read_only (self ):
584
+ schema = {
585
+ "type" : "object" ,
586
+ "properties" : {"some_prop" : {"type" : "string" , "readOnly" : True }},
587
+ }
588
+
589
+ validator = OAS30WriteValidator (
590
+ schema , format_checker = oas30_format_checker ,
591
+ )
592
+ with pytest .raises (
593
+ ValidationError , match = "Tried to write read-only property with hello"
594
+ ):
595
+ validator .validate ({"some_prop" : "hello" })
596
+ validator = OAS30ReadValidator (
597
+ schema , format_checker = oas30_format_checker ,
598
+ )
599
+ assert validator .validate ({"some_prop" : "hello" }) is None
600
+
601
+ def test_write_only (self ):
602
+ schema = {
603
+ "type" : "object" ,
604
+ "properties" : {"some_prop" : {"type" : "string" , "writeOnly" : True }},
605
+ }
606
+
607
+ validator = OAS30ReadValidator (
608
+ schema , format_checker = oas30_format_checker ,
609
+ )
610
+ with pytest .raises (
611
+ ValidationError , match = "Tried to read write-only property with hello"
612
+ ):
613
+ validator .validate ({"some_prop" : "hello" })
614
+ validator = OAS30WriteValidator (
615
+ schema , format_checker = oas30_format_checker ,
616
+ )
617
+ assert validator .validate ({"some_prop" : "hello" }) is None
618
+
619
+ def test_required_read_only (self ):
620
+ schema = {
621
+ "type" : "object" ,
622
+ "properties" : {"some_prop" : {"type" : "string" , "readOnly" : True }},
623
+ "required" : ["some_prop" ],
624
+ }
625
+
626
+ validator = OAS30ReadValidator (
627
+ schema , format_checker = oas30_format_checker ,
628
+ )
629
+ with pytest .raises (
630
+ ValidationError , match = "'some_prop' is a required property"
631
+ ):
632
+ validator .validate ({"another_prop" : "hello" })
633
+ validator = OAS30WriteValidator (
634
+ schema , format_checker = oas30_format_checker ,
635
+ )
636
+ assert validator .validate ({"another_prop" : "hello" }) is None
637
+
638
+ def test_required_write_only (self ):
639
+ schema = {
640
+ "type" : "object" ,
641
+ "properties" : {"some_prop" : {"type" : "string" , "writeOnly" : True }},
642
+ "required" : ["some_prop" ],
643
+ }
644
+
645
+ validator = OAS30WriteValidator (
646
+ schema , format_checker = oas30_format_checker ,
647
+ )
648
+ with pytest .raises (
649
+ ValidationError , match = "'some_prop' is a required property"
650
+ ):
651
+ validator .validate ({"another_prop" : "hello" })
652
+ validator = OAS30ReadValidator (
653
+ schema , format_checker = oas30_format_checker ,
654
+ )
655
+ assert validator .validate ({"another_prop" : "hello" }) is None
656
+
657
+
570
658
class TestOAS31ValidatorValidate :
571
659
@pytest .mark .parametrize (
572
660
"schema_type" ,
0 commit comments