22import warnings
33from typing import Optional
44
5- if sys .version_info >= (3 , 8 ):
6- from functools import cached_property
7- else :
8- from backports .cached_property import cached_property
9-
105from openapi_core .spec import Spec
11- from openapi_core .unmarshalling .schemas .datatypes import CustomFormattersDict
12- from openapi_core . unmarshalling . schemas . datatypes import FormatUnmarshaller
13- from openapi_core . unmarshalling . schemas . datatypes import UnmarshallersDict
6+ from openapi_core .unmarshalling .schemas .datatypes import (
7+ FormatUnmarshallersDict ,
8+ )
149from openapi_core .unmarshalling .schemas .exceptions import (
1510 FormatterNotFoundError ,
1611)
1914)
2015from openapi_core .unmarshalling .schemas .unmarshallers import SchemaUnmarshaller
2116from openapi_core .unmarshalling .schemas .unmarshallers import TypesUnmarshaller
17+ from openapi_core .validation .schemas .datatypes import CustomFormattersDict
18+ from openapi_core .validation .schemas .datatypes import FormatValidatorsDict
2219from openapi_core .validation .schemas .factories import SchemaValidatorsFactory
2320
2421
@@ -27,55 +24,74 @@ def __init__(
2724 self ,
2825 schema_validators_factory : SchemaValidatorsFactory ,
2926 types_unmarshaller : TypesUnmarshaller ,
30- format_unmarshallers : Optional [UnmarshallersDict ] = None ,
27+ format_unmarshallers : Optional [FormatUnmarshallersDict ] = None ,
3128 custom_formatters : Optional [CustomFormattersDict ] = None ,
3229 ):
3330 self .schema_validators_factory = schema_validators_factory
3431 self .types_unmarshaller = types_unmarshaller
35- if custom_formatters is None :
36- custom_formatters = {}
3732 if format_unmarshallers is None :
3833 format_unmarshallers = {}
3934 self .format_unmarshallers = format_unmarshallers
35+ if custom_formatters is None :
36+ custom_formatters = {}
37+ else :
38+ warnings .warn (
39+ "custom_formatters is deprecated. "
40+ "Use extra_format_validators to validate custom formats "
41+ "and use extra_format_unmarshallers to unmarshal custom formats." ,
42+ DeprecationWarning ,
43+ )
4044 self .custom_formatters = custom_formatters
4145
42- @ cached_property
43- def formats_unmarshaller ( self ) -> FormatsUnmarshaller :
44- return FormatsUnmarshaller (
45- self . format_unmarshallers ,
46- self . custom_formatters ,
47- )
48-
49- def create ( self , schema : Spec ) -> SchemaUnmarshaller :
46+ def create (
47+ self ,
48+ schema : Spec ,
49+ format_validators : Optional [ FormatValidatorsDict ] = None ,
50+ format_unmarshallers : Optional [ FormatUnmarshallersDict ] = None ,
51+ extra_format_validators : Optional [ FormatValidatorsDict ] = None ,
52+ extra_format_unmarshallers : Optional [ FormatUnmarshallersDict ] = None ,
53+ ) -> SchemaUnmarshaller :
5054 """Create unmarshaller from the schema."""
5155 if schema is None :
5256 raise TypeError ("Invalid schema" )
5357
5458 if schema .getkey ("deprecated" , False ):
5559 warnings .warn ("The schema is deprecated" , DeprecationWarning )
5660
57- formatters_checks = {
58- name : formatter .validate
59- for name , formatter in self .custom_formatters .items ()
60- }
61+ if extra_format_validators is None :
62+ extra_format_validators = {}
63+ extra_format_validators .update (
64+ {
65+ name : formatter .validate
66+ for name , formatter in self .custom_formatters .items ()
67+ }
68+ )
6169 schema_validator = self .schema_validators_factory .create (
62- schema , ** formatters_checks
70+ schema ,
71+ format_validators = format_validators ,
72+ extra_format_validators = extra_format_validators ,
6373 )
6474
6575 schema_format = schema .getkey ("format" )
6676
77+ formats_unmarshaller = FormatsUnmarshaller (
78+ format_unmarshallers or self .format_unmarshallers ,
79+ extra_format_unmarshallers ,
80+ self .custom_formatters ,
81+ )
82+
6783 # FIXME: don;t raise exception on unknown format
84+ # See https://github.com/p1c2u/openapi-core/issues/515
6885 if (
6986 schema_format
70- and schema_format
71- not in self .schema_validators_factory .format_checker .checkers
72- and schema_format not in self .custom_formatters
87+ and schema_format not in schema_validator
88+ and schema_format not in formats_unmarshaller
7389 ):
7490 raise FormatterNotFoundError (schema_format )
7591
7692 return SchemaUnmarshaller (
7793 schema ,
7894 schema_validator ,
7995 self .types_unmarshaller ,
80- self . formats_unmarshaller ,
96+ formats_unmarshaller ,
8197 )
0 commit comments