Giter Site home page Giter Site logo

tefra / xsdata Goto Github PK

View Code? Open in Web Editor NEW
289.0 8.0 55.0 6.09 MB

Naive XML & JSON Bindings for python

Home Page: https://xsdata.readthedocs.io

License: MIT License

Python 99.64% Jinja 0.36%
xml xsd schema dataclass binding python python-library parser serializer code-generator

xsdata's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

xsdata's Issues

XmlParser text elements are sometimes empty

From lxml

Note that the text, tail, and children of an Element are not necessarily present yet when receiving the start event. Only the end event guarantees that the Element has been parsed completely.

Partial parsing

Currently the parser fails on unknown elements that can't be mapped to any class field.

The xml parser should accept a config object with a flag to skip unknown elements

Restricting Enumerations

Restricting enumeration I know is dump but doesn't work instead all attributes are appended duplicating existing

More HL7v3 enum fun -- Unknown enumerations

This one's harder to pin down, but in the Connect types repo, if you try to run (in the NE2008 directory) xsdata --package hl7 multicacheschemas/ xsdata will crash with:

xsdata.exceptions.AnalyzerError: Unknown enumeration RoleClassPassive: @[email protected]

which I can't quite figure out how to fix internally. It may have to do with the recent enum changes. I don't quite see what it's trying to do with the @ prefixed class name

Serializer / Parser Config

Currently the implementation are too opinionated add config classes to control the output to fit various needs

Ideas:

  • trim zero decimals
  • fail on unknown fields
  • ignore additional fields

Recursive error: inner class extending outer class

In python inner classes are not allowed to extend outer classes

class Foo:
   pass

    Class Bar(Foo):
        pass

This is a sample that demonstrates the issue from w3c test suite msData/particles/particlesEb041.xsd

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="foo" 	xmlns="foo"> 
<xsd:complexType name="foo">
		<xsd:sequence>
			<xsd:element name="foo"/>
			<xsd:element name="bar" minOccurs="0">
				<xsd:complexType>
					<xsd:complexContent>
						<xsd:restriction base="foo">
							<xsd:sequence>
								<xsd:element name="foo"/>
							</xsd:sequence>
						</xsd:restriction>
					</xsd:complexContent>
				</xsd:complexType>
			</xsd:element>
		</xsd:sequence>
</xsd:complexType>

<xsd:element name="root" type="foo" />

</xsd:schema>

Possible solutions: https://stackoverflow.com/a/39077925

Restriction inheritance

The generator is fancy and small but skips over parent restrictions while iterating over elements to find the right ones for attributes

Reference issue with extended unions and fixed attributes

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:simpleType name="AddressPartType">
        <xs:union memberTypes="AdditionalLocator">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:enumeration value="DEL" />
                </xs:restriction>
            </xs:simpleType>
        </xs:union>
    </xs:simpleType>
    <xs:simpleType name="AdditionalLocator">
        <xs:restriction base="xs:string">
            <xs:enumeration value="ADL" />
        </xs:restriction>
    </xs:simpleType>

    <xs:complexType name="adxp.delimiter" mixed="true">
        <xs:attribute name="partType" type="AddressPartType" fixed="DEL"/>
	</xs:complexType>
</xs:schema>

This example schema fails to generate with:

xsdata.exceptions.AnalyzerError: Unknown enumeration AdditionalLocator: DEL

There's an additional issue, that "DEL" is considered a python stop word even if it actually wouldn't conflict, since the only cases that happens is when the casing matches too.

Simplified from the HL7v3 schemas.

xs:list inner classes doesn't work

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:simpleType name="AvailableSizesType">
    <xs:list>
      <xs:simpleType>
        <xs:restriction base="xs:token">
          <xs:enumeration value="small" />
          <xs:enumeration value="medium" />
          <xs:enumeration value="large" />
        </xs:restriction>
      </xs:simpleType>
    </xs:list>
  </xs:simpleType>
</xs:schema>

the attribute type defaults to str instead of the inner enum

Flatten extension simple conflict with complex

Flatten extension simple conflict with complex when common type has same name with a complex type or element.

xsdata common type is anything except elements and complexTypes

<xs:attribute name="fooType"/>
	<xs:complexType name="fooType">
		<xs:sequence>
			<xs:element name="myEle3" type="xs:NMTOKEN"/>
			<xs:element name="myEle4" type="xs:long"/>
		</xs:sequence>
		<xs:attribute ref="fooType"/>
	</xs:complexType>
	<xs:element name="root" type="fooType"/>

Recursion limit for complicated XSDs

For some truly cursed XSDs -- HL7v3 for example -- this tool tries to consolidate and flatten types recursively. However, the complexity is too much and python kills it for hitting the recursion limit or it simply segfaults/hits OOM.

This is the gauntlet of XSDs, no tool at all works besides XJC/JAXB and even then they don't do the unions correctly.

xs:pattern on non string types support

the w3c suite is full of tests applying patterns on decimals, integers, dates and boolean fields.

The xsd patterns is super tricky to handle and I am still debating how to tackle that problem but for now it might make sense to override those types with simple strings.

Integrate w3c / xsdtests

The defxmlschema book samples were really instrumental for the last couple releases, and I am quite happy with the stability of the generators and binding modules, it's about time to test again the official w3c xsd test suite

https://github.com/w3c/xsdtests

Unbounded sequences *headache

Ideas

  • Keep a list of sequences as class metadata
  • Consider unbounded sequences as attributes and generate inner classes

Integration tests

Most of the test cases are too isolated, I have added the xsdata-samples repo to test real life schemas and req/res xml files but as the code base grows I start forgetting stuff.

The project needs documented simplified tests cases with real schemas and outputs/inputs

Missing types should be ignored

Examples from w3c with valid instance documents

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xs:element name="good" type="xs:integer"/>
<xs:element name="bad" type="absent" substitutionGroup="missing"/>
</xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<xs:element name="good" type="xs:integer"/>

<xs:element name="bad" type="list"/>

<xs:simpleType name="list">
 <xs:list itemType="absent"/>
</xs:simpleType> 

</xs:schema>

xs:restriction doesn't add nested simpleType restirctions

	<element name="nillable2" nillable="true">
		<simpleType>
			<restriction>
				<simpleType>
					<list itemType="int" />
				</simpleType>
				<minLength value="2" />
			</restriction>
		</simpleType>
	</element>
    value: Optional[int] = field(
        default=None,
        metadata=dict(
            min_length=2.0
        )
    )

it should add min/max occurs from list

Filter missing type duplicates

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
		   xmlns:my="http://www.examples.org">

	<xs:element name="Example" type="xs:int" vc:typeAvailable="xs:int" />
	
	<xs:element name="Example" type="my:myInt" vc:typeAvailable="xs:int my:myInt" />
	
</xs:schema>
``

Wrong default value for enum fields

The default value should be TypeType.ARC instead of the string value "arc"

class TypeType(Enum):
    """
    :cvar ARC:
    :cvar EXTENDED:
    :cvar LOCATOR:
    :cvar RESOURCE:
    :cvar SIMPLE:
    :cvar TITLE:
    """
    ARC = "arc"
    EXTENDED = "extended"
    LOCATOR = "locator"
    RESOURCE = "resource"
    SIMPLE = "simple"
    TITLE = "title"


@dataclass
class ArcType:

    type: TypeType = field(
        init=False,
        default="arc",
        metadata=dict(
            type="Attribute",
            namespace="http://www.w3.org/1999/xlink",
            required=True
        )
    )

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.