Skip to content

Fix UUID json serialization#289

Merged
eric-eclecticiq merged 25 commits into
masterfrom
fix-uuid-json-encoding
Dec 8, 2025
Merged

Fix UUID json serialization#289
eric-eclecticiq merged 25 commits into
masterfrom
fix-uuid-json-encoding

Conversation

@eric-eclecticiq
Copy link
Copy Markdown
Contributor

@eric-eclecticiq eric-eclecticiq commented Dec 2, 2025

Fix #243

UUID json serialization

Solution

The problem is that by default the JSON encoder can not transform UUID class to JSON. I introduced UuidJSONEncoder that will invoke str on it when making the HTTP answer.

Testing issue

The tests did not catch the issue as it was mocked already using string UUID. I updated the mocked fixtures to be UUID as when coming from the DB.

Types

  1. The TAXII2 models (DB) were already correctly relying on UUID. However, opentaxii.taxii2.entities documented str types. Instead, I updated to UUID when the TAXII2 specs expect an identifier
  2. OpenTAXII2PersistenceAPI method interfaces also highlight when a UUID is expected
  3. Transform api_root_id and job_id from opentaxii.server.TAXII2Server URL path to UUID. It makes less divergence with UUID used elsewhere. Only the path parameter collection_id_or_alias can not be converted to UUID as it could also be an alias that is a string.

Technical improvements

Typing

  1. Add mypy and some stubs as dependency
  2. Add mypy check in the CI
  3. Enforce typing check on opentaxii/ and tests although some files are excluded as there were too many issues to fix

Test package

tests/ was not a package and used pytest magic to find root modules for tests. It started to create a conflict for mypy as there were 2 utils modules. I added a __init__.py where missing and updating utils and fixtures imports.

Copy link
Copy Markdown
Contributor

@saaj saaj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unifying to UUIDs is a good change.

Comment thread opentaxii/taxii2/http.py
* origin:
  Align isort check
  ignore blame to sort import
  Sort import on the codebase
  Add isort
  Ignore blame for black format applied to codebase
  Update python version for publishing
  Drop python 3.8 and 3.9
  Remove sqlalchemy 1.3
  Make it flake8 compatible
  Apply black format to the codebase
  Add black to the project and CI
@eric-eclecticiq eric-eclecticiq merged commit 9344f95 into master Dec 8, 2025
7 checks passed
@eric-eclecticiq eric-eclecticiq deleted the fix-uuid-json-encoding branch December 8, 2025 15:42
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.

UUID is not JSON serializable when running in taxii2 mode

3 participants