Skip to content

feat: add CommonGrants FastAPI API template - #1

Merged
SnowboardTechie merged 20 commits into
mainfrom
bryan/1183-fastapi-api-template
Oct 21, 2026
Merged

SnowboardTechie merged 20 commits into
mainfrom
bryan/1183-fastapi-api-template

Conversation

@SnowboardTechie

@SnowboardTechie SnowboardTechie commented Sep 24, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Implements Extract and refresh the FastAPI template into a standalone repository. This is a working CommonGrants API on FastAPI: three opportunity routes plus /health, backed by the same sample data as the Hono template. Requests and responses are validated with common-grants-sdk 0.8.1, and FastAPI serves the OpenAPI document at /openapi.json with Swagger UI at /docs. Connecting real data means writing one repository class; PORTING.md walks through it.

  • Time to review: 30–45 minutes

Changes proposed

  • create_app(repository) builds the app from one OpportunityRepository protocol. The routes own defaults, envelopes and pagination metadata.
  • SDK request models validate every request. SDK response models validate every success body through response_model. Errors are protocol-shaped 400, 404, 405 and 500.
  • The fixture repository implements the status, date-range and money-range filters, every sort key in both directions, and a deterministic tie-break.
  • CI has two jobs. verify runs checks, tests with coverage, and pip-audit --strict. sdk-range updates the SDK within ^0.8.1 and runs the tests.
  • The README, PORTING.md and contribution files follow the Hono section order. The license is CC0-1.0, and the README credits the monorepo origin.

Context for reviewers

Judgment calls worth a look:

  • Defaults match Core and the other templates, not the Python SDK. The default page size is Core's 100, where the SDK's pagination model says 10. A search that names sortBy without sortOrder sorts ascending, as Hono does. The routes subclass the SDK request models to set these defaults, so /docs documents them. The SDK accepts any sortOrder string, so an unknown one falls back to the default order and is reported in sortInfo.errors.
  • Items are dumped to plain data before FastAPI validates the envelope. FastAPI 0.141 accepts model instances without re-validating them, so a record built with model_copy(update=...) would otherwise reach the client.
  • PORTING.md §6 wraps with_custom_fields in a class statement. The website's plain assignment makes pyright reject every annotation that uses Opportunity. I checked the class form with a two-field extension: tests, make check-types, the typed OpenAPI schemas, and runtime validation all passed.
  • httpx2 is a dev dependency on purpose. Starlette 1.7's TestClient, which fastapi.testclient re-exports, requires it, and its httpx fallback is deprecated.
  • make audit audits pip freeze --exclude-editable. The project's own editable install is not on PyPI, and --strict fails on it even with --skip-editable.

Verified locally on Python 3.11.16 with Poetry 2.5.1:

  • make install, make ci and make audit all pass. The audit found no known vulnerabilities.
  • I ran poetry update common-grants-sdk and then make test. Both passed, and 0.8.1 is the newest release in range.
  • fastapi run served all five routes, and the 404 and 400 cases, as expected.

Hosted CI passes both jobs on 9974184.

Checker run. I ran make gen-openapi, then npx @common-grants/cli@0.4.0 check spec openapi.yaml. It printed Spec is valid and compliant with base spec and exited 0. This pass was expected and proves nothing on its own. It is the false pass on Pydantic output described in HHS/simpler-grants-protocol#735. Pydantic writes each optional field as an anyOf with a {type: null} branch, which covers 44 of the document's 46 anyOf nodes, and the checker collapses anyOf to its first member. Sorted by bucket:

  • Union handling, from the defect report above: no findings. This collapse is also why the next bucket reads zero.
  • HHS/simpler-grants-protocol#1192 nullability drift: no findings, but the drift is present in the document.
  • oneOf and type arrays, from the same report: not exercised, because the document contains neither.

Following PORTING §1 as written made the kept test suites fail at
collection with KeyError: 'DATABASE_URL', because they and the OpenAPI
export import api.py, where the wiring reads the variable. §1 now says to
set it wherever they run, CI included, and to connect on the first query
so the stub-repository suites never need a database. The fixture deletion
step names the export script, which still imported fixture_repository.
@SnowboardTechie
SnowboardTechie marked this pull request as ready for review October 19, 2026 17:35
A missing or inverted bound is unusable in the same way. Taken literally, an
inverted `between` would match nothing and `outside` every record.
"""
if value is None or low is None or high is None or low > high:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggestion: what do you think about treating a missing closeDateRange bound as open-ended? Then {"min": X} would mean “closes on or after X”.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Core's DateRangeFilter requires both min and max, so a one-sided range isn't valid there, and making it open-ended would be a Core change rather than a template one. Could be worth proposing for Core, though.

@karinamzalez karinamzalez left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Worth a look:

  • /docs loads JavaScript from jsdelivr into the API’s own origin, with no integrity check (SRI) and only a major-version pin (src/app.ts:61). It’s documented in PORTING §9, but the README endpoint table could mention it too, since that’s what most people read.
  • (minor) url: "/openapi.json" is an absolute path (src/app.ts:61). If the app is mounted under a prefix or behind a path-rewriting proxy, the page loads but can’t fetch the spec.

@SnowboardTechie

Copy link
Copy Markdown
Collaborator Author

FastAPI's /docs loads Swagger UI from jsDelivr, so I added a note to this README's /docs row (94c0648). The spec URL is already fine here, since FastAPI adds the mount prefix for you. Thanks for the careful look.

@SnowboardTechie
SnowboardTechie merged commit c708baf into main Oct 21, 2026
2 checks passed
@SnowboardTechie
SnowboardTechie deleted the bryan/1183-fastapi-api-template branch October 21, 2026 17:23
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.

[Template] Extract and refresh the FastAPI template into a standalone repository

2 participants