Skip to content

Serialize UUID values instead of raising ValueError (#161)#226

Open
CedricConday wants to merge 1 commit into
XeroAPI:masterfrom
CedricConday:fix/161-serialize-uuid
Open

Serialize UUID values instead of raising ValueError (#161)#226
CedricConday wants to merge 1 commit into
XeroAPI:masterfrom
CedricConday:fix/161-serialize-uuid

Conversation

@CedricConday

@CedricConday CedricConday commented Jun 30, 2026

Copy link
Copy Markdown

Fixes #161

Calling e.g. accounting_api.get_invoice(...) can raise:

ValueError: Can't serialize value type 'UUID'

The singledispatch serializer in serializer.py has handlers for BaseModel and Enum but none for uuid.UUID, so any value that is a UUID (such as an invoice id) falls through to the default and raises.

Change

Register a UUID handler on serialize_model:

@serialize_model.register(UUID)
def serialize_uuid_model(model):
    return str(model)

I used str(model) (canonical 8-4-4-4-12 hyphenated form, e.g. 12345678-1234-5678-1234-567812345678) rather than the .hex form suggested in #93, because that is the GUID format the Xero API uses and accepts.

Test

Added test_serialize_uuid in tests/test_api_client/test_serializer.py. It fails on master (ValueError) and passes with the fix. Full test_api_client suite (151 tests) stays green.

Note: the related to_dict() path raised the same way for enums/UUIDs — fixed separately in #227.


AI-assisted, human-reviewed — I'm an AI engineer; I find, fix, and test with AI, then review and verify before opening.

The singledispatch serializer had no handler for uuid.UUID, so any model
or parameter holding a UUID (e.g. an invoice id) raised
'Can't serialize value type UUID'. Register a UUID handler that returns
the canonical 8-4-4-4-12 hyphenated string expected by the Xero API.

Closes XeroAPI#161, also addresses XeroAPI#93.
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

Successfully merging this pull request may close these issues.

Can't serialize value type 'UUID'

1 participant