xsdata icon indicating copy to clipboard operation
xsdata copied to clipboard

Feature Request: Add option to infer wrapped lists when generating python code from XSD

Open couling opened this issue 1 year ago • 3 comments

Wrapped lists are really useful for simplifying python data structures. Looking through the documentation, I don't see any way to make this work with xsdata generate <xsd file>.

It would be really useful to have an option to infer wrapped lists where one data class will contain another dataclass, itself only containing a list. This may be something that can be post-processed between schema transformation and python code writer.


I don't see any documentation on the "Pluggable code writer for custom output formats". So this customisation isn't something that can be easily bolted on from the side.

I certainly don't want to manually modify python code after parsing a sizeable XSD where the XSD is subject to change.


It could be optional which name is kept for the python name, but I'd recommend it default to the wrapper name, not the element name for these reasons:

  • XML schema don't commonly mix and match between wrapped lists and unwrapped lists. If they have wrapper elements at all then all lists are likely to be wrapped lists.
  • Wrappers are most commonly name the list, inner elements name the type of thing in the list.

Eg:

<person><children><child>Bob</child><children></person>

most naturally converts to

Person(children=["Bob"])

It makes much less sense to convert this to Person(child=["bob"])

couling avatar Jan 30 '24 09:01 couling

It should be part of the code generator, I agree.

tefra avatar Jan 30 '24 16:01 tefra

If I was to look at the possible code for this would it be better to process this in the transform step (which would need a wrapper attribute adding somewhere to the model), or would it be better to add as a feature of the output writer?

couling avatar Jan 31 '24 12:01 couling

I've not forgotten this. Just ran out of free time. I'll be back ;-)

couling avatar Feb 17 '24 21:02 couling

This is on my immediate todo list @couling

tefra avatar Mar 10 '24 10:03 tefra

@couling are you working with a specific schema?

tefra avatar Mar 12 '24 15:03 tefra

I am. But sadly it's proprietary and I don't have authorisation to share it.

couling avatar Mar 12 '24 15:03 couling

I added a new cli config option --wrapper-fields

This is what it does on the w3c-test suite. https://github.com/tefra/xsdata-w3c-tests/commit/65c03423c6679393c9bf7a142e80ac5c03f109ff

Give it a try and let me know how it works for your use case @couling

tefra avatar Mar 21 '24 14:03 tefra