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

Element is generated as List, while it is 0:1 #871

Closed
skinkie opened this issue Dec 6, 2023 · 2 comments
Closed

Element is generated as List, while it is 0:1 #871

skinkie opened this issue Dec 6, 2023 · 2 comments

Comments

@skinkie
Copy link
Contributor

skinkie commented Dec 6, 2023

Within the NeTEx schema the element TransportAdministrativeZone inherits a Description element. This Description element is rendered as List[MultilingualString]. It is unclear to me why this element is not just a single element.

@dataclass(unsafe_hash=True, kw_only=True)
class GroupOfEntitiesVersionStructure(DataManagedObjectStructure):
    """
    Type for a GROUP OF ENTITies.

    :ivar name: Name of GROUP OF ENTITies.
    :ivar short_name: Short Name of GROUP OF ENTITies.
    :ivar description: Further Description of a GROUP OF ENTITies.
    :ivar purpose_of_grouping_ref: Reference to a PURPOSE OF GROUPING.
    :ivar private_code:
    :ivar info_links: Hyperlinks associated with GROUP OF ENTITIES.
    """
    class Meta:
        name = "GroupOfEntities_VersionStructure"

    description: List[MultilingualString] = field(
        default_factory=list,
        metadata={
            "name": "Description",
            "type": "Element",
            "namespace": "http://www.netex.org.uk/netex",
        }
    )

NeTEx-CEN/NeTEx#562

@tefra
Copy link
Owner

tefra commented Dec 6, 2023

Mypy doesn't allow an override field to change from a single value to a list like this

@dataclass
class Foo:
    value: str

@dataclass
class Bar(Foo):
    value: list[str]

To compensate xsdata, finds these cases and converts the parent class to a list, the culprit for the change in GroupOfEntities_VersionStructure is the HeadwayJourneyGroup, which has two sequences that both include a description.

	<xsd:element name="HeadwayJourneyGroup" abstract="false" substitutionGroup="JourneyFrequencyGroup">
		<xsd:annotation>
			<xsd:documentation>A group of VEHICLE JOURNEYs following the same JOURNEY PATTERN and having the same headway interval between a specified start and end time (for example, ‘every 10 minutes’). This is especially useful for presenting passenger information.</xsd:documentation>
		</xsd:annotation>
		<xsd:complexType>
			<xsd:complexContent>
				<xsd:restriction base="HeadwayJourneyGroup_VersionStructure">
					<xsd:sequence>
						<xsd:sequence>
							<xsd:group ref="EntityInVersionGroup" minOccurs="0"/>
						</xsd:sequence>
						<xsd:sequence>
							<xsd:group ref="DataManagedObjectGroup"/>
						</xsd:sequence>
						<xsd:sequence>
							<xsd:group ref="GroupOfEntitiesGroup"/>  <!-- this has a description element -->
						</xsd:sequence>
						<xsd:group ref="JourneyFrequencyGroupGroup"/>
						<xsd:group ref="HeadwayJourneyGroupGroup"/><!-- this has a description element -->
					</xsd:sequence>
					<xsd:attribute name="id" type="HeadwayJourneyGroupIdType"/>
				</xsd:restriction>
			</xsd:complexContent>
		</xsd:complexType>

Here is the hack in the code
https://github.com/tefra/xsdata/blob/main/xsdata/codegen/handlers/validate_attributes_overrides.py#L53-L55

At the time I couldn't think of a better alternative, I am open to suggestions.

Note to Self: Add a warning there because you need to know all xsdata's hacks to debug this one.

@tefra
Copy link
Owner

tefra commented Dec 18, 2023

A new warning was added, if anyone comes up with an idea to address things properly please open a new issue

@tefra tefra closed this as completed Dec 18, 2023
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