2
2
import warnings
3
3
from typing import Optional
4
4
5
- if sys .version_info >= (3 , 8 ):
6
- from functools import cached_property
7
- else :
8
- from backports .cached_property import cached_property
9
-
10
5
from 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
+ )
14
9
from openapi_core .unmarshalling .schemas .exceptions import (
15
10
FormatterNotFoundError ,
16
11
)
19
14
)
20
15
from openapi_core .unmarshalling .schemas .unmarshallers import SchemaUnmarshaller
21
16
from 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
22
19
from openapi_core .validation .schemas .factories import SchemaValidatorsFactory
23
20
24
21
@@ -27,55 +24,74 @@ def __init__(
27
24
self ,
28
25
schema_validators_factory : SchemaValidatorsFactory ,
29
26
types_unmarshaller : TypesUnmarshaller ,
30
- format_unmarshallers : Optional [UnmarshallersDict ] = None ,
27
+ format_unmarshallers : Optional [FormatUnmarshallersDict ] = None ,
31
28
custom_formatters : Optional [CustomFormattersDict ] = None ,
32
29
):
33
30
self .schema_validators_factory = schema_validators_factory
34
31
self .types_unmarshaller = types_unmarshaller
35
- if custom_formatters is None :
36
- custom_formatters = {}
37
32
if format_unmarshallers is None :
38
33
format_unmarshallers = {}
39
34
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
+ )
40
44
self .custom_formatters = custom_formatters
41
45
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 :
50
54
"""Create unmarshaller from the schema."""
51
55
if schema is None :
52
56
raise TypeError ("Invalid schema" )
53
57
54
58
if schema .getkey ("deprecated" , False ):
55
59
warnings .warn ("The schema is deprecated" , DeprecationWarning )
56
60
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
+ )
61
69
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 ,
63
73
)
64
74
65
75
schema_format = schema .getkey ("format" )
66
76
77
+ formats_unmarshaller = FormatsUnmarshaller (
78
+ format_unmarshallers or self .format_unmarshallers ,
79
+ extra_format_unmarshallers ,
80
+ self .custom_formatters ,
81
+ )
82
+
67
83
# FIXME: don;t raise exception on unknown format
84
+ # See https://github.com/p1c2u/openapi-core/issues/515
68
85
if (
69
86
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
73
89
):
74
90
raise FormatterNotFoundError (schema_format )
75
91
76
92
return SchemaUnmarshaller (
77
93
schema ,
78
94
schema_validator ,
79
95
self .types_unmarshaller ,
80
- self . formats_unmarshaller ,
96
+ formats_unmarshaller ,
81
97
)
0 commit comments