Skip to content

Commit 2b05067

Browse files
committed
Fix object oneOf/anyOf unmarshaling and model creation
1 parent b1ec9d8 commit 2b05067

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

openapi_core/unmarshalling/schemas/unmarshallers.py

+32-21
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,16 @@ def model_factory(self):
172172
return ModelFactory()
173173

174174
def unmarshal(self, value):
175+
properties = self.unmarshal_raw(value)
176+
177+
if 'x-model' in self.schema:
178+
name = self.schema['x-model']
179+
model = self.model_factory.create(properties, name=name)
180+
return model
181+
182+
return properties
183+
184+
def unmarshal_raw(self, value):
175185
try:
176186
value = self.formatter.unmarshal(value)
177187
except ValueError as exc:
@@ -180,48 +190,49 @@ def unmarshal(self, value):
180190
else:
181191
return self._unmarshal_object(value)
182192

193+
def _clone(self, schema):
194+
return ObjectUnmarshaller(
195+
schema, self.formatter, self.validator, self.unmarshallers_factory,
196+
self.context)
197+
183198
def _unmarshal_object(self, value):
199+
properties = {}
200+
184201
if 'oneOf' in self.schema:
185-
properties = None
202+
one_of_properties = None
186203
for one_of_schema in self.schema / 'oneOf':
187204
try:
188-
unmarshalled = self._unmarshal_properties(value)
205+
unmarshalled = self._clone(one_of_schema).unmarshal_raw(
206+
value)
189207
except (UnmarshalError, ValueError):
190208
pass
191209
else:
192-
if properties is not None:
210+
if one_of_properties is not None:
193211
log.warning("multiple valid oneOf schemas found")
194212
continue
195-
properties = unmarshalled
213+
one_of_properties = unmarshalled
196214

197-
if properties is None:
215+
if one_of_properties is None:
198216
log.warning("valid oneOf schema not found")
217+
else:
218+
properties.update(one_of_properties)
199219

200220
elif 'anyOf' in self.schema:
201-
properties = None
221+
any_of_properties = None
202222
for any_of_schema in self.schema / 'anyOf':
203223
try:
204-
unmarshalled = self._unmarshal_properties(value)
224+
unmarshalled = self._clone(any_of_schema).unmarshal_raw(
225+
value)
205226
except (UnmarshalError, ValueError):
206227
pass
207228
else:
208-
properties = unmarshalled
229+
any_of_properties = unmarshalled
209230
break
210231

211-
if properties is None:
232+
if any_of_properties is None:
212233
log.warning("valid anyOf schema not found")
213-
214-
else:
215-
properties = self._unmarshal_properties(value)
216-
217-
if 'x-model' in self.schema:
218-
name = self.schema['x-model']
219-
return self.model_factory.create(properties, name=name)
220-
221-
return properties
222-
223-
def _unmarshal_properties(self, value):
224-
properties = {}
234+
else:
235+
properties.update(any_of_properties)
225236

226237
for prop_name, prop in get_all_properties(self.schema).items():
227238
read_only = prop.getkey('readOnly', False)

0 commit comments

Comments
 (0)