Repository navigation
Description
Follow-up to #248, item 1.
What happens today
citry check warns about a link to a page the app does not serve
(citry.template.unknown-url). For a FastAPI or Starlette app it asks
Starlette whether a route matches the link's path
(route.matches(scope) in _match_routes, packages/py/citry/citry/contrib/fastapi.py
around line 222).
Starlette reads only the path in the route. A route written as
/projects/{project_id} matches any text in that place, and the int in
the route function's signature (def project(project_id: int)) plays no
part. So this link passes the check:
<a href="/projects/alpha">Project</a>The browser then gets FastAPI's 422 validation error. This is case m61 of
the error-prevention benchmark ("Link passes a route parameter of the
wrong type", benchmarks/errors), and Citry reports it only at runtime.
Proposal
For a literal link that matches a FastAPI route, check each path
parameter's text the way FastAPI will when the request arrives, and report
a failure as a warning, so /projects/alpha is caught before the app runs
and /projects/1 stays quiet.
FastAPI exposes the typed path parameters as route.dependant.path_params
(dependant is FastAPI's name for what it learned from the route
function's signature; the list holds one field per parameter, with a
name and, in field_info.annotation, the declared type). On FastAPI
0.141.1 with Starlette 1.3.1, for /projects/{project_id} and
project_id: int it lists project_id with int. The text that the link puts in each parameter is in the
path_params of the child_scope that route.matches(scope) already
returns.
Use each field's own validation rather than a check on the type name. The
declared type alone gives wrong answers both ways: with
n: Annotated[int, Path(ge=1)] the annotation still says int, yet /ann/0
is a 422; and FastAPI accepts "1.0" and " 1" for an int, so a
plain int(text) would warn about /ann/1.0, a link that works. On FastAPI
0.141.1, field.validate(text, {}, loc=("path", name)) returns
(value, errors) and agrees with the real responses for 1, 0, 1.0
and " 1", and reports alpha as invalid.
Risk: undocumented FastAPI attribute
FastAPI's docs do not describe dependant.path_params, so a FastAPI
release could rename or move it. The check should:
- read it, and the field's
validatemethod, defensively and, when either
is missing or has an unexpected shape, skip the type check, so the link is
checked exactly as it is today andcitry checknever fails because of a
FastAPI upgrade; - have a test that pins the attribute against the FastAPI version in the
lockfile, so an upgrade that moves it fails a test instead of quietly
turning the check off.
Scope and error modes
- Links built from an expression stay unchecked, as they are for
citry.template.unknown-url. - A parameter whose type Citry cannot check (a type it cannot turn into a
validation, such as a custom class): skipped, with no finding. - A route without typed parameters, or a plain Starlette route: no new
finding. - A path parameter that a
Depends(...)function declares is not in the
route's owndependant.path_params; it is in
route.dependant.dependencies[i].path_params. Decide whether to walk the
dependencies; without it those parameters stay unchecked. - Writing
{project_id:int}in the route already works: Starlette then
does not match/projects/alpha, and the existing rule warns. That
stays the advice for apps that want the check today. - Open question: whether to report this through
citry.template.unknown-url
or through a new rule with its ownrule_*setting. The new rule matches
the versioning policy under Release; under the existing rule, the only
way to turn the new finding off would be to turn off the whole rule
(rule_unknown_url).
Done when
- A test with the benchmark's shape:
/projects/alphaagainst
/projects/{project_id}withproject_id: intgives a warning, and
/projects/1gives none. - The tests for a missing or unexpected
dependantpass. - The benchmark's Citry notes and the docs page ("The mistake Citry
reports only at runtime" on the Benchmarks page) change once a release
with the check is measured.
Release
Suggested for 1.4.0, the minor release after 1.3.0. A new warning can make
citry check fail in a user's CI.
Breaking changes
lists "a new citry check rule" among the changes a minor release may
include, as long as the changelog names the rule and the setting that
turns it off.
Activity