Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
couling opened this issue Jan 30, 2024 · 7 comments · Fixed by #982
Closed
Labels
enhancement New feature or request

Comments

@couling
Copy link

couling commented Jan 30, 2024

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 couling changed the title Add option to infer wrapped lists when generating python code from XSD Feature Request: Add option to infer wrapped lists when generating python code from XSD Jan 30, 2024
@tefra tefra added the enhancement New feature or request label Jan 30, 2024
@tefra
Copy link
Owner

tefra commented Jan 30, 2024

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

@couling
Copy link
Author

couling commented Jan 31, 2024

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 couling mentioned this issue Jan 31, 2024
4 tasks
@couling
Copy link
Author

couling commented Feb 17, 2024

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

@tefra
Copy link
Owner

tefra commented Mar 10, 2024

This is on my immediate todo list @couling

@tefra
Copy link
Owner

tefra commented Mar 12, 2024

@couling are you working with a specific schema?

@couling
Copy link
Author

couling commented Mar 12, 2024

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

@tefra
Copy link
Owner

tefra commented Mar 21, 2024

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

This is what it does on the w3c-test suite.
tefra/xsdata-w3c-tests@65c0342

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants