-
Notifications
You must be signed in to change notification settings - Fork 3
CCM-12896: Generate Python Models #151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 19 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
091c55f
CCM-12896: Initial version of python type generation
gareth-allan 34d1332
CCM-12896: Remove venv
gareth-allan 35b960c
CCM-12896: Run pydantic-model-generator tests as part of build
gareth-allan 81318cc
CCM-12896: Move generated Python types to digital-letters-events package
gareth-allan 87679be
CCM-12896: Rename pydantic-model-generator to python-schema-generator
gareth-allan 94c5867
CCM-12896: Small tweaks to digital-letters-events package for Python
gareth-allan d8d4a8f
CCM-12896: Preprocess schemas to remove allOf
gareth-allan 4332daa
CCM-12896: Fix python-schema-generator lint command
gareth-allan 479f1be
CCM-12896: Small Sonar fixes
gareth-allan c2cf235
CCM-12896: Make python types build in the pipeline
gareth-allan cff2194
CCM-12896: Extract a schema-utils utility
gareth-allan 607d6a5
CCM-12896: Changes following self-review
gareth-allan 31a2600
CCM-12896: Generate Pydantic models programmatically
gareth-allan ac74963
CCM-12896: Temporarily skip build check
gareth-allan 19e584a
CCM-12896: Don't run npm install as part of python class generation
gareth-allan 20ad6c4
CCM-12896: Attempt to fix code coverage paths for python-schema-gener…
gareth-allan 69502d5
CCM-12896: Potential Sonar coverage fix
gareth-allan 50304a2
CCM-12896: Add python-schema-generator coverage.xml to Sonar config
gareth-allan ecd9c19
CCM-12896: Add a unit test for generate_models
gareth-allan 8efd464
CCM-12896: Tidy up README
gareth-allan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -69,6 +69,7 @@ Podman | |
| producedby | ||
| projectRoot | ||
| Protobuf | ||
| Pydantic | ||
| pylint | ||
| Python | ||
| quotingType | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| validators | ||
| types | ||
| models |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/digital-letters-events/digital_letters_events/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| """Digital Letters Events package. | ||
|
|
||
| This package contains automatically-generated Pydantic v2 models for NHS Notify | ||
| Digital Letters events. These models are generated from JSON schemas by the | ||
| python-schema-generator tool. | ||
|
|
||
| DO NOT EDIT: Files in this package are auto-generated. Any manual changes will | ||
| be overwritten when the models are regenerated. | ||
| """ | ||
|
|
||
| from .models import * | ||
|
|
||
| __all__ = ["models"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| pydantic>=2.0.0,<3.0.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| from setuptools import setup, find_packages | ||
|
|
||
| setup( | ||
| name="digital-letters-events", | ||
| version="0.1.0", | ||
| packages=find_packages(), | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| schemas |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| # Variables | ||
| SCHEMA_SRC_DIR := ../../schemas/digital-letters/2025-10-draft/events | ||
| OUTPUT_DIR := ../digital-letters-events/digital_letters_events/models | ||
| SRC_DIR := src | ||
| SCHEMAS_DIR := $(SRC_DIR)/schemas | ||
|
|
||
| # Default target | ||
| .PHONY: all clean generate install install-dev test coverage help | ||
|
|
||
| all: generate | ||
|
|
||
| # Install production dependencies | ||
| install: | ||
| @echo "Installing Python production dependencies..." | ||
| @pip install -r requirements.txt | ||
| @echo "Production dependencies installed!" | ||
|
|
||
| # Install development dependencies | ||
| install-dev: | ||
| @echo "Installing development dependencies..." | ||
| @pip install -r requirements-dev.txt | ||
| @echo "Development dependencies installed!" | ||
|
|
||
| # Generate Pydantic models from JSON schemas | ||
| generate: install | ||
| @echo "Generating Pydantic models from JSON schemas..." | ||
| @mkdir -p $(OUTPUT_DIR) | ||
| @npm run merge | ||
| @python $(SRC_DIR)/generate_models.py \ | ||
| --input-dir $(SCHEMAS_DIR) \ | ||
| --output-dir $(OUTPUT_DIR) | ||
| @echo "Pydantic models generated in $(OUTPUT_DIR)/" | ||
|
|
||
| # Run tests | ||
| test: install-dev | ||
| @echo "Running tests..." | ||
| @cd ../.. && PYTHONPATH=src/python-schema-generator/src:$$PYTHONPATH pytest src/python-schema-generator/tests/ | ||
|
|
||
| # Generate coverage report | ||
| coverage: install-dev | ||
| @echo "Generating coverage report..." | ||
| @cd ../.. && PYTHONPATH=src/python-schema-generator/src:$$PYTHONPATH pytest src/python-schema-generator/tests/ \ | ||
| --cov=src/python-schema-generator \ | ||
| --cov-config=src/python-schema-generator/pytest.ini \ | ||
| --cov-report=html:src/python-schema-generator/htmlcov \ | ||
| --cov-report=term-missing \ | ||
| --cov-report=xml:src/python-schema-generator/coverage.xml \ | ||
| --cov-branch | ||
| @echo "Coverage report generated in src/python-schema-generator/htmlcov/" | ||
|
|
||
| # Clean output directory and generated files | ||
| clean: | ||
| @echo "Cleaning generated models..." | ||
| @rm -rf $(OUTPUT_DIR) htmlcov .pytest_cache coverage.xml | ||
| @rm -rf $(SCHEMAS_DIR) | ||
| @echo "Clean complete!" | ||
|
|
||
| # Show help | ||
| help: | ||
| @echo "Available targets:" | ||
| @echo " all - Generate Pydantic models (default)" | ||
| @echo " install - Install production dependencies" | ||
| @echo " install-dev - Install development dependencies" | ||
| @echo " generate - Generate Pydantic models from JSON schemas" | ||
| @echo " test - Run tests" | ||
| @echo " coverage - Generate coverage report" | ||
| @echo " clean - Clean output directories" | ||
| @echo " help - Show this help message" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| <!-- vale Vale.Avoid = NO --> | ||
| <!-- vale Vale.Terms = NO --> | ||
| # python-schema-generator | ||
| <!-- vale Vale.Avoid = YES --> | ||
| <!-- vale Vale.Terms = YES --> | ||
|
|
||
| This package provides a tool that generates Pydantic v2 models for the events | ||
| that are defined in the JSON schemas that are built as part of this repository. | ||
| The generated Pydantic models can be used to validate and parse NHS Notify | ||
| Digital Letters events. | ||
|
|
||
| ## Setup | ||
|
|
||
| Use `make install-dev` to install the necessary dependencies for this application. | ||
|
|
||
| ## Generating Models | ||
|
|
||
| In order for this tool to function, you must first build the JSON schemas for | ||
| the Digital Letters events. The simplest way to build these schemas is to run | ||
| the `npm run generate-dependencies` command from the root of this repository. | ||
|
|
||
| ### Using the Generator | ||
|
|
||
| Once the JSON schemas have been built, Pydantic models can be generated by running: | ||
|
|
||
| ```bash | ||
| make generate | ||
| ``` | ||
|
|
||
| This will: | ||
|
|
||
| - Read all JSON event schemas from `schemas/digital-letters/2025-10-draft/events/` | ||
| - Generate Pydantic v2 models using `datamodel-code-generator` | ||
| - Output the models to `../digital-letters-events/models/` | ||
|
|
||
| ### Configuration | ||
|
|
||
| The generator can be configured through: | ||
|
|
||
| - Command-line arguments | ||
| - Environment variables | ||
| - Configuration file (if needed) | ||
|
|
||
| ### Output Structure | ||
|
|
||
| <!-- vale Vale.Terms = NO --> | ||
| <!-- vale Vale.Avoid = NO --> | ||
| Generated models are placed in the | ||
| [digital-letters-events](../digital-letters-events/) package: | ||
| <!-- vale Vale.Terms = YES --> | ||
| <!-- vale Vale.Avoid = YES --> | ||
|
|
||
| - `../digital-letters-events/models/` - Individual Pydantic model files | ||
| - `../digital-letters-events/models/__init__.py` - Combined index for easy imports | ||
|
|
||
| ## Testing | ||
|
|
||
| Run tests using: | ||
|
|
||
| ```bash | ||
| make test | ||
| ``` | ||
|
|
||
| For coverage reporting: | ||
|
|
||
| ```bash | ||
| make coverage | ||
| ``` | ||
|
|
||
| ## Development | ||
|
|
||
| ### Prerequisites | ||
|
|
||
| - Python 3.11+ | ||
| - pip or poetry for dependency management | ||
|
|
||
| ### Installing Development Dependencies | ||
|
|
||
| ```bash | ||
| make install-dev | ||
| ``` | ||
|
|
||
| ### Running Tests | ||
|
|
||
| ```bash | ||
| make test | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| Once generated, the models can be used in your code as follows: | ||
|
|
||
| ```python | ||
| from digital_letters_event_python import PrintLetterAvailable, CloudEventEnvelope | ||
|
|
||
| # Validate incoming event | ||
| event_data = CloudEventEnvelope(**raw_event) | ||
| if event_data.type == "uk.nhs.notify.digital.letters.letter.available.v1": | ||
| letter_available = PrintLetterAvailable(**raw_event) | ||
| # Process the validated event | ||
| ``` | ||
|
|
||
| ## Architecture | ||
|
|
||
| The generator consists of: | ||
|
|
||
| - `src/generate_models.py` - Main CLI script | ||
| - `src/schema_processor.py` - Schema loading and processing | ||
| - `src/model_generator.py` - Pydantic model generation logic | ||
| - `src/file_utils.py` - File system utilities | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.