-
-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
337 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,65 @@ | ||
from collections import OrderedDict | ||
|
||
from openapi_core.casting.schemas.casters import ArrayCaster | ||
from openapi_core.casting.schemas.casters import BooleanCaster | ||
from openapi_core.casting.schemas.casters import DummyCaster | ||
from openapi_core.casting.schemas.casters import IntegerCaster | ||
from openapi_core.casting.schemas.casters import NumberCaster | ||
from openapi_core.casting.schemas.casters import ObjectCaster | ||
from openapi_core.casting.schemas.casters import TypesCaster | ||
from openapi_core.casting.schemas.factories import SchemaCastersFactory | ||
from openapi_core.validation.schemas import ( | ||
oas30_read_schema_validators_factory, | ||
) | ||
from openapi_core.validation.schemas import ( | ||
oas30_write_schema_validators_factory, | ||
) | ||
from openapi_core.validation.schemas import oas31_schema_validators_factory | ||
|
||
__all__ = [ | ||
"oas30_write_schema_casters_factory", | ||
"oas30_read_schema_casters_factory", | ||
"oas31_schema_casters_factory", | ||
] | ||
|
||
oas30_casters_dict = OrderedDict( | ||
[ | ||
("object", ObjectCaster), | ||
("array", ArrayCaster), | ||
("boolean", BooleanCaster), | ||
("integer", IntegerCaster), | ||
("number", NumberCaster), | ||
("string", DummyCaster), | ||
] | ||
) | ||
oas31_casters_dict = oas30_casters_dict.copy() | ||
oas31_casters_dict.update( | ||
{ | ||
"null": DummyCaster, | ||
} | ||
) | ||
|
||
oas30_types_caster = TypesCaster( | ||
oas30_casters_dict, | ||
DummyCaster, | ||
) | ||
oas31_types_caster = TypesCaster( | ||
oas31_casters_dict, | ||
DummyCaster, | ||
multi=DummyCaster, | ||
) | ||
|
||
oas30_write_schema_casters_factory = SchemaCastersFactory( | ||
oas30_write_schema_validators_factory, | ||
oas30_types_caster, | ||
) | ||
|
||
__all__ = ["schema_casters_factory"] | ||
oas30_read_schema_casters_factory = SchemaCastersFactory( | ||
oas30_read_schema_validators_factory, | ||
oas30_types_caster, | ||
) | ||
|
||
schema_casters_factory = SchemaCastersFactory() | ||
oas31_schema_casters_factory = SchemaCastersFactory( | ||
oas31_schema_validators_factory, | ||
oas31_types_caster, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,35 @@ | ||
from typing import Dict | ||
from typing import Optional | ||
|
||
from jsonschema_path import SchemaPath | ||
|
||
from openapi_core.casting.schemas.casters import ArrayCaster | ||
from openapi_core.casting.schemas.casters import BaseSchemaCaster | ||
from openapi_core.casting.schemas.casters import CallableSchemaCaster | ||
from openapi_core.casting.schemas.casters import DummyCaster | ||
from openapi_core.casting.schemas.casters import SchemaCaster | ||
from openapi_core.casting.schemas.casters import TypesCaster | ||
from openapi_core.casting.schemas.datatypes import CasterCallable | ||
from openapi_core.util import forcebool | ||
from openapi_core.validation.schemas.datatypes import FormatValidatorsDict | ||
from openapi_core.validation.schemas.factories import SchemaValidatorsFactory | ||
|
||
|
||
class SchemaCastersFactory: | ||
DUMMY_CASTERS = [ | ||
"string", | ||
"object", | ||
"any", | ||
] | ||
PRIMITIVE_CASTERS: Dict[str, CasterCallable] = { | ||
"integer": int, | ||
"number": float, | ||
"boolean": forcebool, | ||
} | ||
COMPLEX_CASTERS = { | ||
"array": ArrayCaster, | ||
} | ||
|
||
def create(self, schema: SchemaPath) -> BaseSchemaCaster: | ||
schema_type = schema.getkey("type", "any") | ||
|
||
if schema_type in self.DUMMY_CASTERS: | ||
return DummyCaster(schema) | ||
|
||
if schema_type in self.PRIMITIVE_CASTERS: | ||
caster_callable = self.PRIMITIVE_CASTERS[schema_type] | ||
return CallableSchemaCaster(schema, caster_callable) | ||
|
||
return ArrayCaster(schema, self) | ||
def __init__( | ||
self, | ||
schema_validators_factory: SchemaValidatorsFactory, | ||
types_caster: TypesCaster, | ||
): | ||
self.schema_validators_factory = schema_validators_factory | ||
self.types_caster = types_caster | ||
|
||
def create( | ||
self, | ||
schema: SchemaPath, | ||
format_validators: Optional[FormatValidatorsDict] = None, | ||
extra_format_validators: Optional[FormatValidatorsDict] = None, | ||
) -> SchemaCaster: | ||
schema_validator = self.schema_validators_factory.create( | ||
schema, | ||
format_validators=format_validators, | ||
extra_format_validators=extra_format_validators, | ||
) | ||
|
||
return SchemaCaster(schema, schema_validator, self.types_caster) |
Oops, something went wrong.