File tree Expand file tree Collapse file tree 3 files changed +64
-1
lines changed
openapi_spec_validator/validation Expand file tree Collapse file tree 3 files changed +64
-1
lines changed Original file line number Diff line number Diff line change 1919 DuplicateOperationIDError ,
2020)
2121from openapi_spec_validator .validation .exceptions import ExtraParametersError
22+ from openapi_spec_validator .validation .exceptions import OpenAPIValidationError
2223from openapi_spec_validator .validation .exceptions import (
2324 ParameterDuplicateError ,
2425)
@@ -98,7 +99,14 @@ def __call__(
9899 self , schema : SchemaPath , require_properties : bool = True
99100 ) -> Iterator [ValidationError ]:
100101 schema_value = schema .read_value ()
101- if not hasattr (schema_value , "__getitem__" ):
102+
103+ try :
104+ self .default_validator .value_validator_cls .check_schema (schema_value )
105+ except Exception as err :
106+ yield OpenAPIValidationError .create_from (err )
107+ return
108+
109+ if isinstance (schema_value , bool ):
102110 return
103111
104112 assert self .schema_ids_registry is not None
Original file line number Diff line number Diff line change @@ -193,6 +193,34 @@ def test_schema_stdin(capsys):
193193 assert "stdin: OK\n " in out
194194
195195
196+ def test_malformed_schema_stdin (capsys ):
197+ """Malformed schema from STDIN reports validation error."""
198+ spec_io = StringIO (
199+ """
200+ openapi: 3.1.0
201+ info:
202+ version: "1"
203+ title: "Title"
204+ components:
205+ schemas:
206+ Component:
207+ type: object
208+ properties:
209+ name: string
210+ """
211+ )
212+
213+ testargs = ["--schema" , "3.1.0" , "-" ]
214+ with mock .patch ("openapi_spec_validator.__main__.sys.stdin" , spec_io ):
215+ with pytest .raises (SystemExit ):
216+ main (testargs )
217+
218+ out , err = capsys .readouterr ()
219+ assert not err
220+ assert "stdin: Validation Error:" in out
221+ assert "stdin: OK" not in out
222+
223+
196224def test_version (capsys ):
197225 """Test --version flag outputs correct version."""
198226 testargs = ["--version" ]
Original file line number Diff line number Diff line change 11from openapi_spec_validator import OpenAPIV2SpecValidator
22from openapi_spec_validator import OpenAPIV30SpecValidator
3+ from openapi_spec_validator import OpenAPIV31SpecValidator
34from openapi_spec_validator .validation .exceptions import (
45 DuplicateOperationIDError ,
56)
@@ -495,3 +496,29 @@ def validate(to_validate) -> bool:
495496 assert len (errors_list ) == 1
496497 assert errors_list [0 ].__class__ == OpenAPIValidationError
497498 assert errors_list [0 ].message == ("'invalid' is not a 'custom'" )
499+
500+ def test_malformed_property_schema (self ):
501+ spec = {
502+ "openapi" : "3.1.0" ,
503+ "info" : {
504+ "title" : "Test Api" ,
505+ "version" : "0.0.1" ,
506+ },
507+ "components" : {
508+ "schemas" : {
509+ "Component" : {
510+ "type" : "object" ,
511+ "properties" : {
512+ "name" : "string" ,
513+ },
514+ }
515+ },
516+ },
517+ }
518+
519+ errors = OpenAPIV31SpecValidator (spec ).iter_errors ()
520+
521+ errors_list = list (errors )
522+ assert len (errors_list ) == 1
523+ assert errors_list [0 ].__class__ == OpenAPIValidationError
524+ assert "'string' is not of type 'object'" in errors_list [0 ].message
You can’t perform that action at this time.
0 commit comments