Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit97775cf

Browse files
committed
multi types schema format unmarshal fix
1 parente666357 commit97775cf

File tree

5 files changed

+69
-52
lines changed

5 files changed

+69
-52
lines changed

‎openapi_core/unmarshalling/schemas/exceptions.py‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,3 @@ class FormatterNotFoundError(UnmarshallerError):
1919

2020
def__str__(self)->str:
2121
returnf"Formatter not found for{self.type_format} format"
22-
23-
24-
@dataclass
25-
classFormatUnmarshalError(UnmarshallerError):
26-
"""Unable to unmarshal value for format"""
27-
28-
value:str
29-
type:str
30-
original_exception:Exception
31-
32-
def__str__(self)->str:
33-
return (
34-
"Unable to unmarshal value {value} for format {type}: {exception}"
35-
).format(
36-
value=self.value,
37-
type=self.type,
38-
exception=self.original_exception,
39-
)

‎openapi_core/unmarshalling/schemas/unmarshallers.py‎

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
fromopenapi_core.unmarshalling.schemas.datatypesimport (
1616
FormatUnmarshallersDict,
1717
)
18-
fromopenapi_core.unmarshalling.schemas.exceptionsimportFormatUnmarshalError
19-
fromopenapi_core.unmarshalling.schemas.exceptionsimportUnmarshallerError
2018
fromopenapi_core.validation.schemas.validatorsimportSchemaValidator
2119

2220
log=logging.getLogger(__name__)
@@ -138,34 +136,15 @@ def _unmarshal_properties(
138136

139137
classMultiTypeUnmarshaller(PrimitiveUnmarshaller):
140138
def__call__(self,value:Any)->Any:
141-
unmarshaller=self._get_best_unmarshaller(value)
139+
primitive_type=self.schema_validator.get_primitive_type(value)
140+
unmarshaller=self.schema_unmarshaller.get_type_unmarshaller(
141+
primitive_type
142+
)
142143
returnunmarshaller(value)
143144

144-
@property
145-
deftype(self)->List[str]:
146-
types=self.schema.getkey("type", ["any"])
147-
assertisinstance(types,list)
148-
returntypes
149-
150-
def_get_best_unmarshaller(self,value:Any)->"PrimitiveUnmarshaller":
151-
forschema_typeinself.type:
152-
result=self.schema_validator.type_validator(
153-
value,type_override=schema_type
154-
)
155-
ifnotresult:
156-
continue
157-
result=self.schema_validator.format_validator(value)
158-
ifnotresult:
159-
continue
160-
returnself.schema_unmarshaller.get_type_unmarshaller(schema_type)
161-
162-
raiseUnmarshallerError("Unmarshaller not found for type(s)")
163-
164145

165146
classAnyUnmarshaller(MultiTypeUnmarshaller):
166-
@property
167-
deftype(self)->List[str]:
168-
returnself.schema_unmarshaller.types_unmarshaller.get_types()
147+
pass
169148

170149

171150
classTypesUnmarshaller:
@@ -185,7 +164,7 @@ def __init__(
185164
defget_types(self)->List[str]:
186165
returnlist(self.unmarshallers.keys())
187166

188-
defget_unmarshaller(
167+
defget_unmarshaller_cls(
189168
self,
190169
schema_type:Optional[Union[Iterable[str],str]],
191170
)->Type["PrimitiveUnmarshaller"]:
@@ -220,8 +199,8 @@ def unmarshal(self, schema_format: str, value: Any) -> Any:
220199
returnvalue
221200
try:
222201
returnformat_unmarshaller(value)
223-
except (ValueError,TypeError)asexc:
224-
raiseFormatUnmarshalError(value,schema_format,exc)
202+
except (AttributeError,ValueError,TypeError):
203+
returnvalue
225204

226205
defget_unmarshaller(
227206
self,schema_format:str
@@ -279,19 +258,32 @@ def unmarshal(self, value: Any) -> Any:
279258
(isinstance(value,bytes)andschema_formatin ["binary","byte"])
280259
):
281260
returntyped
282-
returnself.formats_unmarshaller.unmarshal(schema_format,typed)
261+
262+
format_unmarshaller=self.get_format_unmarshaller(schema_format)
263+
ifformat_unmarshallerisNone:
264+
returntyped
265+
try:
266+
returnformat_unmarshaller(typed)
267+
except (AttributeError,ValueError,TypeError):
268+
returntyped
283269

284270
defget_type_unmarshaller(
285271
self,
286272
schema_type:Optional[Union[Iterable[str],str]],
287273
)->PrimitiveUnmarshaller:
288-
klass=self.types_unmarshaller.get_unmarshaller(schema_type)
274+
klass=self.types_unmarshaller.get_unmarshaller_cls(schema_type)
289275
returnklass(
290276
self.schema,
291277
self.schema_validator,
292278
self,
293279
)
294280

281+
defget_format_unmarshaller(
282+
self,
283+
schema_format:str,
284+
)->Optional[FormatUnmarshaller]:
285+
returnself.formats_unmarshaller.get_unmarshaller(schema_format)
286+
295287
defevolve(self,schema:SchemaPath)->"SchemaUnmarshaller":
296288
cls=self.__class__
297289

@@ -304,6 +296,10 @@ def evolve(self, schema: SchemaPath) -> "SchemaUnmarshaller":
304296

305297
deffind_format(self,value:Any)->Optional[str]:
306298
forschemainself.schema_validator.iter_valid_schemas(value):
299+
schema_validator=self.schema_validator.evolve(schema)
300+
primitive_type=schema_validator.get_primitive_type(value)
301+
ifprimitive_type!="string":
302+
continue
307303
if"format"inschema:
308304
returnstr(schema.getkey("format"))
309305
returnNone

‎openapi_core/validation/schemas/validators.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,24 @@ def format_validator_callable(self) -> FormatValidator:
7878

7979
returnlambdax:True
8080

81+
defget_primitive_type(self,value:Any)->Optional[str]:
82+
schema_types=self.schema.getkey("type")
83+
ifisinstance(schema_types,str):
84+
returnschema_types
85+
ifschema_typesisNone:
86+
schema_types=sorted(self.validator.TYPE_CHECKER._type_checkers)
87+
assertisinstance(schema_types,list)
88+
forschema_typeinschema_types:
89+
result=self.type_validator(value,type_override=schema_type)
90+
ifnotresult:
91+
continue
92+
result=self.format_validator(value)
93+
ifnotresult:
94+
continue
95+
assertisinstance(schema_type, (str,type(None)))
96+
returnschema_type
97+
returnNone
98+
8199
defiter_valid_schemas(self,value:Any)->Iterator[SchemaPath]:
82100
yieldself.schema
83101

‎tests/integration/unmarshalling/test_unmarshallers.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2057,6 +2057,27 @@ def test_nultiple_types_invalid(self, unmarshallers_factory, types, value):
20572057
assertlen(exc_info.value.schema_errors)==1
20582058
assert"is not of type"inexc_info.value.schema_errors[0].message
20592059

2060+
@pytest.mark.parametrize(
2061+
"types,format,value,expected",
2062+
[
2063+
(["string","null"],"date",None,None),
2064+
(["string","null"],"date","2018-12-13",date(2018,12,13)),
2065+
],
2066+
)
2067+
deftest_multiple_types_format_valid_or_ignored(
2068+
self,unmarshallers_factory,types,format,value,expected
2069+
):
2070+
schema= {
2071+
"type":types,
2072+
"format":format,
2073+
}
2074+
spec=SchemaPath.from_dict(schema)
2075+
unmarshaller=unmarshallers_factory.create(spec)
2076+
2077+
result=unmarshaller.unmarshal(value)
2078+
2079+
assertresult==expected
2080+
20602081
deftest_any_null(self,unmarshallers_factory):
20612082
schema= {}
20622083
spec=SchemaPath.from_dict(schema)

‎tests/unit/unmarshalling/test_schema_unmarshallers.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
fromopenapi_core.unmarshalling.schemas.exceptionsimport (
99
FormatterNotFoundError,
1010
)
11-
fromopenapi_core.unmarshalling.schemas.exceptionsimportFormatUnmarshalError
1211
fromopenapi_core.unmarshalling.schemas.factoriesimport (
1312
SchemaUnmarshallersFactory,
1413
)
@@ -102,8 +101,9 @@ def custom_format_unmarshaller(value):
102101
extra_format_unmarshallers=extra_format_unmarshallers,
103102
)
104103

105-
withpytest.raises(FormatUnmarshalError):
106-
unmarshaller.unmarshal(value)
104+
result=unmarshaller.unmarshal(value)
105+
106+
assertresult==value
107107

108108
deftest_schema_extra_format_unmarshaller_format_custom(
109109
self,schema_unmarshaller_factory

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp