xsdaata with pydantic works but using the classes manually aint a good idea

This commit is contained in:
Jonas Linter
2025-09-24 11:18:57 +02:00
parent d2bb8a3f54
commit a9116b0e0c
2 changed files with 91 additions and 77 deletions

View File

@@ -28,59 +28,8 @@ def validate_dataclass_patterns(obj) -> None:
validate_dataclass_patterns(item)
def main():
# Success - just use object() since it's defined as Optional[object]
success = object()
# Now we need to create the missing nested classes that aren't fully defined
# Let's create minimal implementations for the empty classes
# Create basic implementations for the empty nested classes
class RoomType(ab.BaseModel):
room_type_code: str = "A"
room_classification_code: str = "5"
room_type: str = "8"
class GuestCount(ab.BaseModel):
count: int = 1
class TimeSpanImpl(ab.BaseModel):
start: str
end: str
class PersonName(ab.BaseModel):
given_name: str
surname: str
class Customer(ab.BaseModel):
language: str = "de"
gender: str = "Male" # Use valid gender
person_name: PersonName
class Profile(ab.BaseModel):
customer: Customer
class ProfileInfo(ab.BaseModel):
profile: Profile
class Profiles(ab.BaseModel):
profile_info: ProfileInfo
class ResGuest(ab.BaseModel):
profiles: Profiles
class HotelReservationId(ab.BaseModel):
res_id_type: str
res_id_source_context: str
res_id_value: str
res_id_source: str
# Build the structure using the Pydantic models
person_name = PersonName(given_name="Otto", surname="Mustermann")
customer = Customer(person_name=person_name)
profile = Profile(customer=customer)
profile_info = ProfileInfo(profile=profile)
profiles = Profiles(profile_info=profile_info)
res_guest = ResGuest(profiles=profiles)
# Success - use None instead of object() for cleaner XML output
success = None
# UniqueID
unique_id = ab.OtaResRetrieveRs.ReservationsList.HotelReservation.UniqueId(
@@ -88,28 +37,70 @@ def main():
id="6b34fe24ac2ff811"
)
# TimeSpan - use the actual nested class
time_span = ab.OtaResRetrieveRs.ReservationsList.HotelReservation.RoomStays.RoomStay.TimeSpan()
# RoomStay with TimeSpan
room_stay = ab.OtaResRetrieveRs.ReservationsList.HotelReservation.RoomStays.RoomStay(
time_span=time_span
)
room_stays = ab.OtaResRetrieveRs.ReservationsList.HotelReservation.RoomStays(
room_stay=[room_stay]
)
profile = ab.OtaResRetrieveRs.ReservationsList.HotelReservation.ResGuests.ResGuest.Profiles.ProfileInfo.Profile(
customer=ab.OtaResRetrieveRs.ReservationsList.HotelReservation.ResGuests.ResGuest.Profiles.ProfileInfo.Profile.Customer(
person_name=ab.OtaResRetrieveRs.ReservationsList.HotelReservation.ResGuests.ResGuest.Profiles.ProfileInfo.Profile.Customer.PersonName(
given_name="John",
surname="Doe"
)
)
)
# Use the actual nested Profiles class
profile_info = ab.OtaResRetrieveRs.ReservationsList.HotelReservation.ResGuests.ResGuest.Profiles.ProfileInfo(
profile=profile)
profiles = ab.OtaResRetrieveRs.ReservationsList.HotelReservation.ResGuests.ResGuest.Profiles(profile_info=profile_info)
# ResGuest
res_guest = ab.OtaResRetrieveRs.ReservationsList.HotelReservation.ResGuests.ResGuest(
profiles=profiles
)
res_guests = ab.OtaResRetrieveRs.ReservationsList.HotelReservation.ResGuests(
res_guest=res_guest
)
# Use the actual nested HotelReservationIds class
hotel_res_ids = ab.OtaResRetrieveRs.ReservationsList.HotelReservation.ResGlobalInfo.HotelReservationIds(
hotel_reservation_id=[]
)
# Basic property info
basic_property_info = ab.OtaResRetrieveRs.ReservationsList.HotelReservation.ResGlobalInfo.BasicPropertyInfo(
hotel_code="123",
hotel_name="Frangart Inn"
)
# Build a minimal reservation structure
# ResGlobalInfo
res_global_info = ab.OtaResRetrieveRs.ReservationsList.HotelReservation.ResGlobalInfo(
hotel_reservation_ids=hotel_res_ids,
basic_property_info=basic_property_info
)
# Hotel Reservation
hotel_reservation = ab.OtaResRetrieveRs.ReservationsList.HotelReservation(
create_date_time=datetime.now(timezone.utc).isoformat(),
res_status=ab.HotelReservationResStatus.REQUESTED,
room_stay_reservation="true",
unique_id=unique_id
unique_id=unique_id,
room_stays=room_stays,
res_guests=res_guests,
res_global_info=res_global_info
)
# Add the basic property info to res_global_info
res_global_info = ab.OtaResRetrieveRs.ReservationsList.HotelReservation.ResGlobalInfo(
basic_property_info=basic_property_info
)
# Update the hotel reservation with all required fields
hotel_reservation.res_global_info = res_global_info
reservations_list = ab.OtaResRetrieveRs.ReservationsList(hotel_reservation=[hotel_reservation])
# Root element
@@ -135,7 +126,10 @@ def main():
)
serializer = XmlSerializer(config=config)
xml_string = serializer.render(ota_res_retrieve_rs)
# Use ns_map to control namespace prefixes - set default namespace
ns_map = {None: "http://www.opentravel.org/OTA/2003/05"}
xml_string = serializer.render(ota_res_retrieve_rs, ns_map=ns_map)
with open('output.xml', 'w', encoding='utf-8') as outfile:
outfile.write(xml_string)
@@ -156,12 +150,11 @@ def main():
parsed_result = parser.from_string(xml_content, ab.OtaResRetrieveRs)
print("✅ Round-trip validation successful!")
print(f"Parsed reservation status: {parsed_result.res_status}")
print(f"Parsed reservation status: {parsed_result.reservations_list.hotel_reservation[0].res_status}")
except Exception as e:
print(f"❌ Validation/Serialization failed: {e}")
if __name__ == "__main__":
main()

View File

@@ -1,12 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<ns0:OTA_ResRetrieveRS xmlns:ns0="http://www.opentravel.org/OTA/2003/05" Version="7.000">
<ns0:Success>&lt;object object at 0x7fd2fa5a0f40&gt;</ns0:Success>
<ns0:ReservationsList>
<ns0:HotelReservation CreateDateTime="2025-09-24T08:56:11.806672+00:00" ResStatus="Requested" RoomStayReservation="true">
<ns0:UniqueID Type="14" ID="6b34fe24ac2ff811"/>
<ns0:ResGlobalInfo>
<ns0:BasicPropertyInfo HotelCode="123" HotelName="Frangart Inn"/>
</ns0:ResGlobalInfo>
</ns0:HotelReservation>
</ns0:ReservationsList>
</ns0:OTA_ResRetrieveRS>
<OTA_ResRetrieveRS xmlns="http://www.opentravel.org/OTA/2003/05" Version="7.000">
<ReservationsList>
<HotelReservation CreateDateTime="2025-09-24T09:16:59.563720+00:00" ResStatus="Requested" RoomStayReservation="true">
<UniqueID Type="14" ID="6b34fe24ac2ff811"/>
<RoomStays>
<RoomStay>
<TimeSpan/>
</RoomStay>
</RoomStays>
<ResGuests>
<ResGuest>
<Profiles>
<ProfileInfo>
<Profile>
<Customer>
<PersonName>
<GivenName>John</GivenName>
<Surname>Doe</Surname>
</PersonName>
</Customer>
</Profile>
</ProfileInfo>
</Profiles>
</ResGuest>
</ResGuests>
<ResGlobalInfo>
<HotelReservationIDs/>
<BasicPropertyInfo HotelCode="123" HotelName="Frangart Inn"/>
</ResGlobalInfo>
</HotelReservation>
</ReservationsList>
</OTA_ResRetrieveRS>