Conversation
| assert 'extra_nested' in actual_obj.model_extra | ||
|
|
||
|
|
||
| def test_extra_save(): |
There was a problem hiding this comment.
The version I have now only works on attributes, not on child elements.
| @classmethod | ||
| def _keep_extra(cls, element: XmlElementReader) -> Dict: | ||
| result = {} | ||
| for path, attr, value in element.get_unbound(): | ||
| if path: | ||
| first_tag = path[0].tag | ||
| # result_section = result.setdefault(first_tag, []) | ||
| # result_section.append(path) | ||
| result[first_tag] = [] | ||
| elif attr: | ||
| result[attr] = value | ||
|
|
||
| return result | ||
|
|
There was a problem hiding this comment.
I dislike this flow a little. The data is first crawled over through the model. Then, a bit later, we go over the leftovers to store them elsewhere. Is this the most sensible approach?
|
Actually, I did just add another commit that also handles extra subelements. But I don't know if this is nice enough. |
|
@RobertoRoos Hi, extra="allow" feature is much more complicated to implement for xml than json since xml format respects element order so that extra elements order must be kept during serialization. For example: class TestModel(BaseXmlModel, tag='model', extra='allow', search_mode='ordered'):
element2: str = element()
xml = '''
<model>
<element1>1</element1>
<element2>2</element2>
<element3>3</element3>
</model>
'''elements must be serialized back in the same order not as <model>
<element2>2</element2>
<element1>1</element1>
<element3>3</element3>
</model> |
|
Hm, good point. We'll need to store some ordering information too then. My biggest doubt is in this comment: #305 (comment) - is there some way we can avoid parsing the input twice? |
Should resolve #304
pydantic-xmldoesn't really honorextra="allow", it only doesextra="ignore'.I would love to see behavior like in
pydanticitself, see for example this test: https://github.com/pydantic/pydantic/blob/26da044ff35a7d9b7b26c798b8d3c44457468005/tests/test_main.py#L267So in
pydanticyou can do something like this:Here for XML we could do the same, where we store attributes and extra child elements also under
.model_extra. Attributes we could all load as string, the elements we could store as raw XML.