-
Notifications
You must be signed in to change notification settings - Fork 0
feat(api): OpenAPI Pydantic codegen + CI drift check (PLAN 3.5) #10
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 all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e8565fd
feat(api): OpenAPI Pydantic codegen + CI drift check (PLAN 3.5)
mcalthrop f713408
fix(ci): Copilot review for OpenAPI codegen workflow
mcalthrop 6cd5ef1
ci: use [[ for bash test in OpenAPI codegen workflow
mcalthrop 3e8fcbc
ci: use GENERATED_PATH in OpenAPI codegen drift check
mcalthrop 0ef4973
ci: unify workflow, add openapi:validate, root pnpm scripts
mcalthrop 80b0f8d
feat(api): data_paths, venv-based scripts, move recipes.json
mcalthrop 890c4c6
ci: rely on packageManager for pnpm version; clarify OpenAPI codegen …
mcalthrop 7cd7757
fix(api): address Copilot review for validate script and recipes test
mcalthrop 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| name: CI | ||
|
|
||
| on: | ||
| push: | ||
| branches: [main] | ||
| pull_request: | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| validate: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v6.0.2 | ||
|
|
||
| - uses: actions/setup-python@v6.2.0 | ||
| with: | ||
| python-version: "3.12" | ||
| cache: pip | ||
| cache-dependency-path: apps/api/pyproject.toml | ||
|
|
||
| - uses: pnpm/action-setup@v5.0.0 | ||
|
|
||
| - uses: actions/setup-node@v6.3.0 | ||
| with: | ||
| node-version-file: ".nvmrc" | ||
| cache: pnpm | ||
|
|
||
| - name: Install dependencies | ||
| run: pnpm install --frozen-lockfile | ||
|
|
||
| - name: Install API dev dependencies | ||
| working-directory: apps/api | ||
| run: .venv/bin/python -m pip install -e ".[dev]" | ||
|
|
||
| - name: Lint | ||
| run: pnpm lint | ||
|
|
||
| - name: Test | ||
| run: pnpm test | ||
|
|
||
| - name: Generate OpenAPI code | ||
| run: pnpm openapi:generate | ||
|
|
||
| - name: Validate generated OpenAPI code matches the repo | ||
| run: pnpm openapi:validate | ||
This file was deleted.
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
File renamed without changes.
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 |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| """Paths to bundled recipe data (editable install vs wheel).""" | ||
|
|
||
| from pathlib import Path | ||
|
|
||
|
|
||
| def resolve_recipes_json_path() -> Path: | ||
| """Return ``apps/api/data/recipes.json`` in the repo; ``recipes_api/data/recipes.json`` in an installed wheel.""" | ||
| here = Path(__file__).resolve().parent | ||
| peer = here.parent / "data" / "recipes.json" | ||
| packaged = here / "data" / "recipes.json" | ||
| if peer.is_file(): | ||
| return peer | ||
| return packaged |
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 @@ | ||
| """Code-generated artefacts (do not edit by hand).""" |
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,44 @@ | ||
| # generated by datamodel-codegen: | ||
| # filename: openapi.yaml | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import List, Optional | ||
|
|
||
| from pydantic import AnyUrl, BaseModel, Field, conint | ||
|
|
||
|
|
||
| class ErrorMessage(BaseModel): | ||
| message: str = Field(..., description='Human-readable error description.') | ||
|
|
||
|
|
||
| class RecipeSummary(BaseModel): | ||
| id: str = Field( | ||
| ..., description='Unique recipe identifier (used in `/recipes/{recipe_id}`).' | ||
| ) | ||
| title: str = Field(..., examples=['Seeded sourdough']) | ||
| summary: str = Field(..., description='Short teaser for the recipe card.') | ||
| imageUrl: AnyUrl = Field(..., description='Thumbnail image for the list view.') | ||
|
|
||
|
|
||
| class RecipeDetail(BaseModel): | ||
| id: str | ||
| title: str | ||
| summary: str | ||
| imageUrl: AnyUrl = Field( | ||
| ..., | ||
| description='Same logical photo as the list; suitable for smaller placements.', | ||
| ) | ||
| imageUrlLarge: AnyUrl = Field( | ||
| ..., description='Larger version of the same photo for the recipe page.' | ||
| ) | ||
| ingredients: List[str] = Field( | ||
| ..., description='Ingredient lines as displayed to the baker.' | ||
| ) | ||
| steps: List[str] = Field(..., description='Ordered steps to make the bread.') | ||
| prepTimeMinutes: Optional[conint(ge=0)] = Field( | ||
| None, description='Optional active preparation time in minutes.' | ||
| ) | ||
| bakeTimeMinutes: Optional[conint(ge=0)] = Field( | ||
| None, description='Optional bake time in minutes.' | ||
| ) |
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,36 @@ | ||
| """Regenerate ``recipes_api/generated/openapi_models.py`` from ``packages/openapi/openapi.yaml``.""" | ||
|
|
||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| from datamodel_code_generator import ( | ||
| DataModelType, | ||
| InputFileType, | ||
| PythonVersion, | ||
| generate, | ||
| ) | ||
| from recipes_api.openapi_paths import resolve_openapi_spec_path | ||
|
|
||
| _RECIPES_PKG = Path(__file__).resolve().parent | ||
| OUTPUT = _RECIPES_PKG / "generated" / "openapi_models.py" | ||
|
|
||
|
|
||
| def main() -> None: | ||
| openapi_yaml = resolve_openapi_spec_path() | ||
| if not openapi_yaml.is_file(): | ||
| print(f"OpenAPI spec not found: {openapi_yaml}", file=sys.stderr) | ||
| sys.exit(1) | ||
| OUTPUT.parent.mkdir(parents=True, exist_ok=True) | ||
| generate( | ||
| openapi_yaml, | ||
| input_filename=openapi_yaml.name, | ||
| input_file_type=InputFileType.OpenAPI, | ||
| output=OUTPUT, | ||
| output_model_type=DataModelType.PydanticV2BaseModel, | ||
| disable_timestamp=True, | ||
| target_python_version=PythonVersion.PY_312, | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
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 |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| #!/usr/bin/env bash | ||
| # Fail if recipes_api/generated does not match the working tree (run from repo root via git). | ||
| set -euo pipefail | ||
|
|
||
| ROOT="$(git rev-parse --show-toplevel 2>/dev/null || :)" | ||
| if [[ -z "${ROOT}" ]]; then | ||
| echo "validate_openapi_generated.sh: not inside a git repository" >&2 | ||
| exit 1 | ||
| fi | ||
| cd "${ROOT}" | ||
|
|
||
| GENERATED_PATH="apps/api/recipes_api/generated" | ||
| if ! git diff --quiet -- "${GENERATED_PATH}"; then | ||
| echo "Generated OpenAPI files under ${GENERATED_PATH} are out of date." >&2 | ||
| echo "Please run 'pnpm openapi:generate' and commit the updated files." >&2 | ||
| git diff -- "${GENERATED_PATH}" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| PORCELAIN_CHANGES=$(git status --porcelain -- "${GENERATED_PATH}") || exit $? | ||
| if [[ -n "${PORCELAIN_CHANGES}" ]]; then | ||
| echo "Untracked or uncommitted changes under ${GENERATED_PATH}. Regenerate with 'pnpm openapi:generate' and commit." >&2 | ||
| printf '%s\n' "${PORCELAIN_CHANGES}" >&2 | ||
| exit 1 | ||
| fi |
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
Oops, something went wrong.
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.