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

Nested schema Element in appinfo Affects Namespace Resolution #1079

Open
drwicid opened this issue Sep 24, 2024 · 1 comment
Open

Nested schema Element in appinfo Affects Namespace Resolution #1079

drwicid opened this issue Sep 24, 2024 · 1 comment

Comments

@drwicid
Copy link

drwicid commented Sep 24, 2024

I'm encountering an obscure real-world scenario involving an XML schema that includes a schema element nested inside an appdata element. When this schema is parsed, the nested schema element seems to override the namespace information. While the generated dataclass includes the correct __NAMESPACE__ and Meta declarations from the primary schema, the metadata attributes for fields contain an empty string for the namespace.

Example Schema:

<xsd:schema targetNamespace="http://example.org/tst" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tst="http://example.org/tst" elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xsd:annotation>
    <xsd:appinfo>
      <schema>Test</schema> <!-- Schema element nested in appinfo -->
    </xsd:appinfo>
  </xsd:annotation>
  <xsd:element name="product" type="tst:ProductType"/>
  <xsd:complexType name="ProductType">
    <xsd:sequence>
      <xsd:element name="number" type="xsd:integer"/>
      <xsd:element name="size" type="tst:SizeType"/>
    </xsd:sequence>
    <xsd:attribute name="effDate" type="xsd:date"/>
  </xsd:complexType>
  <xsd:simpleType name="SizeType">
    <xsd:restriction base="xsd:integer">
      <xsd:minInclusive value="2"/>
      <xsd:maxInclusive value="18"/>
    </xsd:restriction>
  </xsd:simpleType>
</xsd:schema>

Generated Data Classes:

__NAMESPACE__ = "http://example.org/tst"      # <<< Correct namespace declared

@dataclass
class ProductType:
    number: Optional[int] = field(
        default=None,
        metadata={
            "type": "Element",
            "namespace": "",                  # <<< Empty String/Wrong Namespace
            "required": True,
        },
    )
    size: Optional[int] = field(
        default=None,
        metadata={
            "type": "Element",
            "namespace": "",                  # <<< Empty String/Wrong Namespace
            "required": True,
            "min_inclusive": 2,
            "max_inclusive": 18,
        },
    )
    eff_date: Optional[XmlDate] = field(
        default=None,
        metadata={
            "name": "effDate",
            "type": "Attribute",
        },
    )

@dataclass
class Product(ProductType):
    class Meta:
        name = "product"
        namespace = "http://example.org/tst"  # <<< Correct namespace declared

Issue:

A simple workaround is to remove the schema element from appdata element to generate valid classes. However, I wonder if this is expected behavior in xsdata, where it parses and processes elements within appinfo as if they were valid top-level schema elements. Should xsdata be handling this differently by ignoring the entire appdata block, and could this be considered a bug that needs to be fixed?

For reference, this is the actual full schema: https://cwe.mitre.org/data/xsd/cwe_schema_latest.xsd

@tefra
Copy link
Owner

tefra commented Dec 8, 2024

I am not sure I understand correctly, in the example schema the ProductType is a complex type, it's not supposed to have a namespace.

Care to provide an example xml?

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

No branches or pull requests

2 participants