tefra / xsdata

Naive XML & JSON Bindings for python
https://xsdata.readthedocs.io
MIT License
331 stars 60 forks source link

choice results in list without elementnames (or reference to the full type) #775

Closed skinkie closed 1 year ago

skinkie commented 1 year ago

Considering schema: https://github.com/VDVde/OJP/tree/changes_for_v1.1

Generating this schema with compound-fields.

Having XML below. The query: ojp.ojprequest.service_request.ojpfare_request[0].trip_fare_request.trip.leg[2].timed_leg.service.choice_1 leads to the following:

image

Given that this element upstream is a choice (that is not very elegant in my humble option...) the way xsData handles that in deserialisation looses track what element was actually in use here. Hence I would have no clue what this InternationalText comes from. Ideally I would like to see that this choice would become a dictionary or list of tuples but at least the element name.

Suggestion 1: Could choice_1 be renamed to the actual choice? For example in the case below I would rather have it named datedjourneygroup and privateservicegroup.

                        <xs:choice>
                                <xs:group ref="DatedJourneyGroup"/>
                                <xs:group ref="PrivateServiceGroup"/>
                        </xs:choice>  

Bug 1: I think the group under a choice is wrongly interpreted as generating a list of elements being a choice. This is not the case. The group as a whole is 'the choice'. My suggestion here is introduce the group as 'artifical container' which is now choice_1 (or choice_2). Skipping the compound fields

<?xml version="1.0" encoding="UTF-8"?>
<OJP xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.siri.org.uk/siri" xmlns:ojp="http://www.vdv.de/ojp" version="1.1-dev" xsi:schemaLocation="http://www.siri.org.uk/siri ../../../OJP.xsd">
    <OJPRequest>
        <ServiceRequest>
            <RequestTimestamp>2020-01-19T12:00:00Z</RequestTimestamp>
            <RequestorRef>MyClient</RequestorRef>
            <ojp:OJPFareRequest>
                <RequestTimestamp>2020-01-19T12:00:00Z</RequestTimestamp>
                <MessageIdentifier>231231-231</MessageIdentifier>
                <ojp:TripFareRequest>
                    <ojp:Trip>
                        <ojp:Id>192391231</ojp:Id>
                        <ojp:Duration>PT55M</ojp:Duration>
                        <ojp:StartTime>2020-01-19T12:00:02Z</ojp:StartTime>
                        <ojp:EndTime>2020-01-19T12:57:00Z</ojp:EndTime>
                        <ojp:Transfers>0</ojp:Transfers>
                        <ojp:Leg>
                            <ojp:Id>1</ojp:Id>
                            <ojp:ContinuousLeg>
                                <ojp:LegStart>
                                    <ojp:GeoPosition>
                                        <Longitude>5.1</Longitude>
                                        <Latitude>20.1</Latitude>
                                    </ojp:GeoPosition>
                                    <ojp:Name>
                                        <ojp:Text>Origin</ojp:Text>
                                    </ojp:Name>
                                </ojp:LegStart>
                                <ojp:LegEnd>
                                    <ojp:GeoPosition>
                                        <Longitude>5.2</Longitude>
                                        <Latitude>20.2</Latitude>
                                    </ojp:GeoPosition>
                                    <ojp:Name>
                                        <ojp:Text>Destination</ojp:Text>
                                    </ojp:Name>
                                </ojp:LegEnd>
                                <ojp:Service>
                                    <ojp:ContinuousMode>demandResponsive</ojp:ContinuousMode>
                                    <ojp:OperatingDayRef>2023-12-24</ojp:OperatingDayRef>
                                    <VehicleRef>ch:1:vehicle:31231:12311</VehicleRef>
                                    <ojp:JourneyRef>ch:1:jyid:12931231</ojp:JourneyRef>
                                    <!-- mandatory and I don't like this -->
                                    <LineRef>ch:1:slnid:1231912</LineRef>
                                    <DirectionRef>ch:1:direction:H</DirectionRef>
                                    <ojp:Mode>
                                        <ojp:PtMode>bus</ojp:PtMode>
                                        <BusSubmode>demandAndResponseBus</BusSubmode>
                                    </ojp:Mode>
                                    <ojp:PublishedServiceName>
                                        <ojp:Text>Mybuxi</ojp:Text>
                                    </ojp:PublishedServiceName>
                                    <ojp:BookingArrangements>
                                        <ojp:BookingArrangement>
                                            <ojp:BookingUrl>
                                                <ojp:Label>
                                                    <ojp:Text>MyBuxi</ojp:Text>
                                                </ojp:Label>
                                                <ojp:Url>https://mybuxi.ch/booking</ojp:Url>
                                            </ojp:BookingUrl>
                                        </ojp:BookingArrangement>
                                    </ojp:BookingArrangements>
                                </ojp:Service>
                                <ojp:Duration>PT15M</ojp:Duration>
                            </ojp:ContinuousLeg>
                            <ojp:EmissionCO2>
                                <ojp:KilogramPerPersonKm>0.1</ojp:KilogramPerPersonKm>
                            </ojp:EmissionCO2>
                        </ojp:Leg>
                        <ojp:Leg>
                            <ojp:Id>2</ojp:Id>
                            <ojp:TransferLeg>
                                <ojp:TransferMode>walk</ojp:TransferMode>
                                <ojp:LegStart>
                                    <ojp:GeoPosition>
                                        <Longitude>5.2</Longitude>
                                        <Latitude>20.2</Latitude>
                                    </ojp:GeoPosition>
                                    <ojp:Name>
                                        <ojp:Text>Destination</ojp:Text>
                                    </ojp:Name>
                                </ojp:LegStart>
                                <ojp:LegEnd>
                                    <StopPointRef>ch:1:sloid:3000:7</StopPointRef>
                                    <ojp:Name>
                                        <ojp:Text>Bern, Gleis 7</ojp:Text>
                                    </ojp:Name>
                                </ojp:LegEnd>
                                <ojp:Duration>PT2M</ojp:Duration>
                            </ojp:TransferLeg>
                        </ojp:Leg>
                        <ojp:Leg>
                            <ojp:Id>1231123</ojp:Id>
                            <ojp:TimedLeg>
                                <ojp:LegBoard>
                                    <StopPointRef>ch:1:sloid:3000:7</StopPointRef>
                                    <ojp:StopPointName>
                                        <ojp:Text>Bern, Gleis 7</ojp:Text>
                                    </ojp:StopPointName>
                                    <ojp:ServiceDeparture>
                                        <ojp:TimetabledTime>2020-01-19T13:02:00Z</ojp:TimetabledTime>
                                    </ojp:ServiceDeparture>
                                </ojp:LegBoard>
                                <ojp:LegAlight>
                                    <StopPointRef>ch:1:sloid:7000:33</StopPointRef>
                                    <ojp:StopPointName>
                                        <ojp:Text>Zürich HB, Gleis 33</ojp:Text>
                                    </ojp:StopPointName>
                                    <ojp:ServiceArrival>
                                        <ojp:TimetabledTime>2020-01-19T13:57:00Z</ojp:TimetabledTime>
                                    </ojp:ServiceArrival>
                                </ojp:LegAlight>
                                <ojp:Service>
                                    <ojp:OperatingDayRef>2023-01-24</ojp:OperatingDayRef>
                                    <ojp:JourneyRef>ch:1:sjyid:11:182391231</ojp:JourneyRef>
                                    <LineRef>ch:1:slnid:11:8</LineRef>
                                    <ojp:Mode>
                                        <ojp:PtMode>rail</ojp:PtMode>
                                        <RailSubmode>highSpeedRailService</RailSubmode>
                                    </ojp:Mode>
                                    <ojp:PublishedServiceName>
                                        <ojp:Text>IC8</ojp:Text>
                                    </ojp:PublishedServiceName>
                                    <ojp:DestinationText>
                                        <ojp:Text>Romanshorn</ojp:Text>
                                    </ojp:DestinationText>
                                </ojp:Service>
                            </ojp:TimedLeg>
                        </ojp:Leg>
                    </ojp:Trip>
                </ojp:TripFareRequest>
                <ojp:Params>
                    <ojp:FareAuthorityFilter>ch:1:NOVA</ojp:FareAuthorityFilter>
                    <ojp:PassengerCategory>Adult</ojp:PassengerCategory>
                    <ojp:TravelClass>second</ojp:TravelClass>
                    <ojp:Traveller>
                        <ojp:Age>25</ojp:Age>
                    </ojp:Traveller>
                </ojp:Params>
            </ojp:OJPFareRequest>
        </ServiceRequest>
    </OJPRequest>
</OJP>
tefra commented 1 year ago

I am working (for some time now) on massive refactor for compound fields, to resolve this kind of issues with nested choices/sequences and groups.

skinkie commented 1 year ago

I am working (for some time now) on massive refactor for compound fields, to resolve this kind of issues with nested choices/sequences and groups.

Consider this bug a 'how to reproduce' not to pressure you into releasing anything that is not production ready.

tefra commented 1 year ago

Hey @skinkie, the refactor I mentioned is now on master, please take a look.

The bug part where occurs where wrongly calculated should now work as expected :crossed_fingers:

I am closing this one for now, if you still have an issue let's open a new one, with the new generated classes.

I am trying to cleanup the issues.