Skip to content

CCM-12896: Generate Python Models#151

Merged
gareth-allan merged 20 commits intomainfrom
feature/CCM-12896_generate_python_models
Dec 17, 2025
Merged

CCM-12896: Generate Python Models#151
gareth-allan merged 20 commits intomainfrom
feature/CCM-12896_generate_python_models

Conversation

@gareth-allan
Copy link
Copy Markdown
Contributor

@gareth-allan gareth-allan commented Dec 15, 2025

Description

This PR adds a new application in src/python-schema-generator that takes the JSON event schemas built from the docs/src/cloudevents directories and produces Pydantic V2 model classes for each event (using datamodel-code-generator).

The Python model classes are generated in the existing src/digital-letters-events package, so that using them from Python uses the same package name as when using the Typescript types. Note that in order to be able to generate Pydantic classes from our schemas I have add to add a pre-generation step which uses the json-schema-merge-allof package to merge the allOf entries in the schemas, as datamodel-code-generator was otherwise producing empty classes to represent string fields that used an allOf property.

Context

This change means that we will now always have Pydantic model classes for each event that we have a schema defined for in the digital letters domain, and that any changes to the event schemas will automatically result in updated code.

Validation

Made some example changes to convert the mesh-poll lambda to Python and have it validate incoming data using the generated models: feature/CCM-12896_generate_python_models...test/CCM-12896_demo_pydantic_usage#diff-8a1058fb005c5dc6646f1c7f79d30ef5e549d0122909d209b88c0a375ddfc6f1
Screenshot 2025-12-16 at 10 15 46

My nhs-notify-digital-letters directory before running make config:
Screenshot 2025-12-16 at 10 19 45

My nhs-notify-digital-letters directory after running make config:
Screenshot 2025-12-16 at 10 21 29

Compared the runtime of the workflow for this branch:
Screenshot 2025-12-16 at 14 10 15

Against a branch taken from main:
Screenshot 2025-12-16 at 13 28 29

We can see that the build times are largely unchanged.

Dev Container

To verify the changes work in the dev container.

Ran make clean outside of a container and verified there were no generated files in src/digital-letters-events/digital_letters_events:
Screenshot 2025-12-16 at 10 23 04

Then reopened the workspace in a new Notify Loaded 1.0.17 dev container and verified the files had been generated (make config is run as part of the post-create command for the dev container:
Screenshot 2025-12-16 at 10 39 34

Type of changes

  • Refactoring (non-breaking change)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would change existing functionality)
  • Bug fix (non-breaking change which fixes an issue)

Checklist

  • I am familiar with the contributing guidelines
  • I have followed the code style of the project
  • I have added tests to cover my changes
  • I have updated the documentation accordingly
  • This PR is a result of pair or mob programming

Sensitive Information Declaration

To ensure the utmost confidentiality and protect your and others privacy, we kindly ask you to NOT including PII (Personal Identifiable Information) / PID (Personal Identifiable Data) or any other sensitive data in this PR (Pull Request) and the codebase changes. We will remove any PR that do contain any sensitive information. We really appreciate your cooperation in this matter.

  • I confirm that neither PII/PID nor sensitive data are included in this PR and the codebase changes.

Comment thread .github/workflows/stage-2-test.yaml
@gareth-allan gareth-allan marked this pull request as ready for review December 16, 2025 08:26
@gareth-allan gareth-allan requested review from a team as code owners December 16, 2025 08:26
Comment thread src/python-schema-generator/README.md Outdated
@gareth-allan gareth-allan merged commit 69f1c82 into main Dec 17, 2025
36 checks passed
@gareth-allan gareth-allan deleted the feature/CCM-12896_generate_python_models branch December 17, 2025 13:18
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.

3 participants