11"""OpenAPI core schemas models module"""
22import attr
33import logging
4- from collections import defaultdict
54import re
65
76from jsonschema .exceptions import ValidationError
87
98from openapi_core .schema .schemas ._format import oas30_format_checker
109from openapi_core .schema .schemas .enums import SchemaType
11- from openapi_core .schema .schemas .exceptions import (
12- CastError , InvalidSchemaValue ,
13- )
10+ from openapi_core .schema .schemas .exceptions import InvalidSchemaValue
1411from openapi_core .schema .schemas .types import NoValue
15- from openapi_core .schema .schemas .util import forcebool
1612from openapi_core .schema .schemas .validators import OAS30Validator
1713from openapi_core .unmarshalling .schemas .exceptions import (
1814 UnmarshalValueError ,
@@ -30,12 +26,6 @@ class Format(object):
3026class Schema (object ):
3127 """Represents an OpenAPI Schema."""
3228
33- TYPE_CAST_CALLABLE_GETTER = {
34- SchemaType .INTEGER : int ,
35- SchemaType .NUMBER : float ,
36- SchemaType .BOOLEAN : forcebool ,
37- }
38-
3929 def __init__ (
4030 self , schema_type = None , properties = None , items = None ,
4131 schema_format = None , required = None , default = NoValue , nullable = False ,
@@ -109,30 +99,17 @@ def get_all_properties_names(self):
10999 all_properties = self .get_all_properties ()
110100 return set (all_properties .keys ())
111101
112- def get_cast_mapping (self ):
113- mapping = self .TYPE_CAST_CALLABLE_GETTER .copy ()
114- mapping .update ({
115- SchemaType .ARRAY : self ._cast_collection ,
116- })
117-
118- return defaultdict (lambda : lambda x : x , mapping )
119-
120102 def cast (self , value ):
121103 """Cast value from string to schema type"""
122- if value in (None , NoValue ):
123- return value
124-
125- cast_mapping = self .get_cast_mapping ()
126-
127- cast_callable = cast_mapping [self .type ]
104+ from openapi_core .casting .schemas .exceptions import CastError
105+ from openapi_core .casting .schemas .factories import SchemaCastersFactory
106+ casters_factory = SchemaCastersFactory ()
107+ caster = casters_factory .create (self )
128108 try :
129- return cast_callable (value )
130- except ValueError :
109+ return caster (value )
110+ except ( ValueError , TypeError ) :
131111 raise CastError (value , self .type )
132112
133- def _cast_collection (self , value ):
134- return list (map (self .items .cast , value ))
135-
136113 def get_validator (self , resolver = None ):
137114 return OAS30Validator (
138115 self .__dict__ ,
0 commit comments