11import string
22from typing import TYPE_CHECKING
33from typing import Any
4+ from typing import Sequence
45from typing import Iterator
56from typing import List
67from typing import Optional
@@ -78,7 +79,8 @@ def _collect_properties(self, schema: SchemaPath) -> set[str]:
7879 props : set [str ] = set ()
7980
8081 if "properties" in schema :
81- props .update ((schema / "properties" ).keys ())
82+ schema_props = (schema / "properties" ).keys ()
83+ props .update (cast (Sequence [str ], schema_props ))
8284
8385 for kw in ("allOf" , "anyOf" , "oneOf" ):
8486 if kw in schema :
@@ -96,11 +98,12 @@ def _collect_properties(self, schema: SchemaPath) -> set[str]:
9698 def __call__ (
9799 self , schema : SchemaPath , require_properties : bool = True
98100 ) -> Iterator [ValidationError ]:
99- if not hasattr (schema .content (), "__getitem__" ):
101+ schema_value = schema .read_value ()
102+ if not hasattr (schema_value , "__getitem__" ):
100103 return
101104
102105 assert self .schema_ids_registry is not None
103- schema_id = id (schema . content () )
106+ schema_id = id (schema_value )
104107 if schema_id in self .schema_ids_registry :
105108 return
106109 self .schema_ids_registry .append (schema_id )
@@ -151,8 +154,8 @@ def __call__(
151154 require_properties = False ,
152155 )
153156
154- required = schema . getkey ( "required" , [])
155- properties = schema . get ( "properties" , {} ).keys ()
157+ required = "required" in schema and ( schema / "required" ). read_value () or []
158+ properties = "properties" in schema and ( schema / "properties" ).keys () or []
156159 if "allOf" in schema :
157160 extra_properties = list (
158161 set (required ) - set (properties ) - set (nested_properties )
@@ -166,10 +169,12 @@ def __call__(
166169 )
167170
168171 if "default" in schema :
169- default = schema ["default" ]
170- nullable = schema .get ("nullable" , False )
171- if default is not None or nullable is not True :
172- yield from self .default_validator (schema , default )
172+ default_value = (schema / "default" ).read_value ()
173+ nullable_value = False
174+ if "nullable" in schema :
175+ nullable_value = (schema / "nullable" ).read_value ()
176+ if default_value is not None or nullable_value is not True :
177+ yield from self .default_validator (schema , default_value )
173178
174179
175180class SchemasValidator (KeywordValidator ):
@@ -203,9 +208,9 @@ def __call__(self, parameter: SchemaPath) -> Iterator[ValidationError]:
203208
204209 if "default" in parameter :
205210 # only possible in swagger 2.0
206- default = parameter . getkey ( "default" )
207- if default is not None :
208- yield from self .default_validator (parameter , default )
211+ if "default" in parameter :
212+ default_value = ( parameter / "default" ). read_value ()
213+ yield from self .default_validator (parameter , default_value )
209214
210215
211216class ParametersValidator (KeywordValidator ):
@@ -246,6 +251,7 @@ def media_type_validator(self) -> MediaTypeValidator:
246251
247252 def __call__ (self , content : SchemaPath ) -> Iterator [ValidationError ]:
248253 for mimetype , media_type in content .items ():
254+ assert isinstance (mimetype , str )
249255 yield from self .media_type_validator (mimetype , media_type )
250256
251257
@@ -291,6 +297,7 @@ def response_validator(self) -> ResponseValidator:
291297
292298 def __call__ (self , responses : SchemaPath ) -> Iterator [ValidationError ]:
293299 for response_code , response in responses .items ():
300+ assert isinstance (response_code , str )
294301 yield from self .response_validator (response_code , response )
295302
296303
@@ -317,15 +324,17 @@ def __call__(
317324 ) -> Iterator [ValidationError ]:
318325 assert self .operation_ids_registry is not None
319326
320- operation_id = operation .getkey ("operationId" )
321- if (
322- operation_id is not None
323- and operation_id in self .operation_ids_registry
324- ):
325- yield DuplicateOperationIDError (
326- f"Operation ID '{ operation_id } ' for '{ name } ' in '{ url } ' is not unique"
327- )
328- self .operation_ids_registry .append (operation_id )
327+ if "operationId" in operation :
328+ operation_id_value = (operation / "operationId" ).read_value ()
329+ if (
330+ operation_id_value is not None
331+ and operation_id_value in self .operation_ids_registry
332+ ):
333+ yield DuplicateOperationIDError (
334+ f"Operation ID '{ operation_id_value } ' for "
335+ f"'{ name } ' in '{ url } ' is not unique"
336+ )
337+ self .operation_ids_registry .append (operation_id_value )
329338
330339 if "responses" in operation :
331340 responses = operation / "responses"
@@ -392,6 +401,7 @@ def __call__(
392401 yield from self .parameters_validator (parameters )
393402
394403 for field_name , operation in path_item .items ():
404+ assert isinstance (field_name , str )
395405 if field_name not in self .OPERATIONS :
396406 continue
397407
@@ -407,6 +417,7 @@ def path_validator(self) -> PathValidator:
407417
408418 def __call__ (self , paths : SchemaPath ) -> Iterator [ValidationError ]:
409419 for url , path_item in paths .items ():
420+ assert isinstance (url , str )
410421 yield from self .path_validator (url , path_item )
411422
412423
@@ -416,8 +427,9 @@ def schemas_validator(self) -> SchemasValidator:
416427 return cast (SchemasValidator , self .registry ["schemas" ])
417428
418429 def __call__ (self , components : SchemaPath ) -> Iterator [ValidationError ]:
419- schemas = components .get ("schemas" , {})
420- yield from self .schemas_validator (schemas )
430+ if "schemas" in components :
431+ schemas = components / "schemas"
432+ yield from self .schemas_validator (schemas )
421433
422434
423435class RootValidator (KeywordValidator ):
0 commit comments