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

[BUG] Deserialization does not work for XmlArraySerializationType.FLAT where XML Name is customised #89

Closed
madpah opened this issue Apr 4, 2024 · 3 comments · Fixed by #90
Assignees
Labels
bug Something isn't working

Comments

@madpah
Copy link
Owner

madpah commented Apr 4, 2024

Discovered whilst working CycloneDX/cyclonedx-python-lib#576.

Steps to reproduce:

@property
    @serializable.view(SchemaVersion1Dot6)
    @serializable.xml_array(serializable.XmlArraySerializationType.FLAT, child_name='omniborId')
    @serializable.xml_sequence(16)
    def omnibor_ids(self) -> 'SortedSet[OmniborId]':
        """
        Specifies the OmniBOR Artifact ID. The OmniBOR, if specified, MUST be valid and conform to the specification
        defined at: https://www.iana.org/assignments/uri-schemes/prov/gitoid

        Returns:
            `Iterable[str]` or `None`
        """

        return self._omnibor_ids

produces XML (as expected):
<omniborId>gitoid:blob:sha1:261eeb9e9f8b2b4b0d119366dda99c6fd7d35c64</omniborId>

however this fails deserialization:
ValueError: omnibor_id is not a known Property for cyclonedx.model.component.Component

@madpah madpah added the bug Something isn't working label Apr 4, 2024
@madpah madpah self-assigned this Apr 4, 2024
madpah added a commit that referenced this issue Apr 4, 2024
Signed-off-by: Paul Horton <paul.horton@owasp.org>
@madpah
Copy link
Owner Author

madpah commented Apr 4, 2024

Upon investigation, this issue is caused by a difference in approach to handling custom field names and the CurrentFormatter.

Example below are with and the default formatter CamelCasePropertyNameFormatter.

Note: This only affects XML (de-)serialization.

The expectation is for the serialized version to use the custom name stockId.

Example 1

    @property
    @serializable.view(SchemaVersion4)
    @serializable.xml_array(XmlArraySerializationType.FLAT, 'stockId')
    @serializable.xml_sequence(21)
    def stock_ids(self) -> Set[StockId]:
        return self._stock_ids

❌ Serialization produces: <stockId>stock-id-1</stockId> as desired, but deserialization fails with:

ValueError: stock_id is not a known Property for tests.model.Book

Example 2

    @property
    @serializable.view(SchemaVersion4)
    @serializable.xml_array(XmlArraySerializationType.FLAT, 'stock_id')
    @serializable.xml_sequence(21)
    def stock_ids(self) -> Set[StockId]:
        return self._stock_ids

❌ Serialization produces: <stock_id>stock-id-1</stock_id>, but this DOES deserialize successfully.

Example 3
Attempting to force custom XML name

    @property
    @serializable.view(SchemaVersion4)
    @serializable.xml_array(XmlArraySerializationType.FLAT, 'stock_id')
    @serializable.xml_name('stockId')
    @serializable.xml_sequence(21)
    def stock_ids(self) -> Set[StockId]:
        return self._stock_ids

❌ Custom name is not used during serialization - output is <stock_id>..., but this DOES deserialize successfully (as per Example 2)

@madpah
Copy link
Owner Author

madpah commented Apr 4, 2024

Expectation is that Example 1 should work here, with Examples 2 and 3 being curious attempts only.

@jkowalleck
Copy link
Collaborator

jkowalleck commented Apr 4, 2024

stumbled upon this (or a similar) issue, when working with the lib downstream.
my solution was, to do write the (de)serializers manually by implementing BaseHelper.{xml,json}_{de,}serialize() functions

@madpah madpah closed this as completed in #90 Apr 4, 2024
madpah added a commit that referenced this issue Apr 4, 2024
…es not conform to current formatter #89 (#90)

Signed-off-by: Paul Horton <paul.horton@owasp.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants