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

Wrapping a list of element and primitive nodes #710

Merged
merged 12 commits into from Sep 28, 2022
Merged

Wrapping a list of element and primitive nodes #710

merged 12 commits into from Sep 28, 2022

Conversation

cercide
Copy link
Contributor

@cercide cercide commented Sep 14, 2022

📒 Description

XML data structures commonly wrap element and primitive collections. For instance, a library may have several books
and and other stuff as well. In terms of OpenAPI 3, these data
structures are wrapped. Hence, this PR introduces the field parameter wrapper, which wraps any element/primitive
collection into a custom xml element without the need of a dedicated wrapper class.

from dataclasses import dataclass, field
from typing import List
from xsdata.formats.dataclass.serializers import XmlSerializer
from xsdata.formats.dataclass.serializers.config import SerializerConfig

config = SerializerConfig(pretty_print=True, xml_declaration=False)
serializer = XmlSerializer(config=config)

@dataclass
class Library:
    books: List[str] = field(
        metadata={
            "wrapper": "Books",
            "name": "Title",
            "type": "Element",
        }
    )

obj = Library(
    books = [
        "python for beginners",
        "beautiful xml",
    ]
)

print(serializer.render(obj))
<Library>
  <Books>
    <Title>python for beginners</Title>
    <Title>beautiful xml</Title>
  </Books>
</Library>

Resolves #709

🔗 What I've Done

First, a new metadata attribute wrapper indicates that a list is wrapped by an xml object.

Next, when the serializer writes a list, the wrapper attribute triggers XmlWriterEvent.START end before the serializer writes the list. Afterwards, an XmlWriterEvent.END event follows vice versa .

Finally, the parser introduces the class WrapperNode as a subclass of XmlNode. The WrapperNode class is a wrapper for the parent element and lines up into the node queue.

💬 Comments

This is a great library to work in pythonik way with xml. Thank you for your efforts!

🛫 Checklist

@codecov
Copy link

codecov bot commented Sep 21, 2022

Codecov Report

Base: 100.00% // Head: 100.00% // No change to project coverage 👍

Coverage data is based on head (0e84311) compared to base (a93f186).
Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff            @@
##            master      #710   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          100       101    +1     
  Lines         8741      8776   +35     
  Branches      2212      2220    +8     
=========================================
+ Hits          8741      8776   +35     
Impacted Files Coverage Δ
xsdata/utils/testing.py 100.00% <ø> (ø)
xsdata/formats/dataclass/context.py 100.00% <100.00%> (ø)
xsdata/formats/dataclass/models/builders.py 100.00% <100.00%> (ø)
xsdata/formats/dataclass/models/elements.py 100.00% <100.00%> (ø)
xsdata/formats/dataclass/parsers/bases.py 100.00% <100.00%> (ø)
xsdata/formats/dataclass/parsers/nodes/__init__.py 100.00% <100.00%> (ø)
xsdata/formats/dataclass/parsers/nodes/wrapper.py 100.00% <100.00%> (ø)
xsdata/formats/dataclass/serializers/xml.py 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Owner

@tefra tefra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good @cercide let's also take care of the coverage please

@sonarcloud
Copy link

sonarcloud bot commented Sep 26, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@tefra
Copy link
Owner

tefra commented Sep 28, 2022

Thank you for the contribution @cercide this is something I wanted to tackle at some point!!!

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

Successfully merging this pull request may close these issues.

Support for a wrapped list
2 participants