Skip to content

router: store Spec per route so dynamic dispatch carries it - #120

Merged
iainmcgin merged 2 commits into
mainfrom
iain/router-spec
May 19, 2026
Merged

iainmcgin merged 2 commits into
mainfrom
iain/router-spec

Conversation

@iainmcgin

Copy link
Copy Markdown
Collaborator

Why

The dynamic Router returned spec: None from lookup() because its registration API had nowhere to put a Spec. The codegen-emitted register(Router) has the static *_SPEC consts in scope (#112) but route() never accepted them — so a Router built through register() was metadata-blind: RequestContext::spec() was always None for handlers and interceptors, even though the monomorphic FooServiceServer<T> dispatcher for the same service supplies a Spec for every method. RequestContext::path() (#116) papers over the procedure name; stream_type and idempotency were just lost. An interceptor or span builder behaves differently depending on which dispatch path the host wired up — for the same proto.

What

Each registered route now carries an Option<Spec>, set after the fact by Router::with_spec(spec). The Spec keys the route by spec.procedure (leading slash stripped, matching the route_* methods' format!("{service}/{method}") keying), so the codegen chains .with_spec(SPEC_CONST) after each route_view* call without touching the route_* signatures. lookup() returns the stored Spec.

  • Router::with_spec is the one new public method. route_* signatures and behavior are unchanged — a registration without with_spec behaves exactly as before (spec: None).
  • Debug builds assert with_spec finds a registered route, and that the route's idempotent flag agrees with spec.idempotency_level — a mismatch would silently disable Connect GET for an idempotent method, which is a hard-to-find bug.
  • The generated register() now emits .with_spec(SPEC_CONST) per method. Checked-in generated code in benches/, examples/, and conformance/ regenerated.
  • merge_routers carries each route's Spec across the merge (the storage moves with the entry).

Confidence

  • 5 new router.rs tests: round-trip through lookup(), back-compat without with_spec, merge_routers preserves specs, debug-assert panics on unknown route and on idempotency mismatch.
  • 1 new service.rs test (spec_flows_to_handler_context): drives a request through handle_unary_request with a with_spec-attached Router and asserts the handler sees the Spec and that path() agrees with spec().procedure.
  • 2 streaming-test e2e tests updated to assert the Router path now surfaces the same Spec as the codegen dispatcher (they previously asserted the opposite — that's the behavior change this PR is for).
  • 335 lib + 37 codegen + 20 e2e tests pass. cargo fmt, cargo clippy --workspace --all-targets -- -D warnings, cargo doc all clean (no new warnings).
  • Regenerated all checked-in *.__connect.rs via buf generate with the workspace's protoc-gen-connect-rust build; only connect/ outputs changed (buffa/ outputs unchanged).

🤖 Generated with Claude Code

The dynamic Router returned spec: None from lookup() because its
registration API had nowhere to put a Spec. The codegen-emitted
register(Router) has the static *_SPEC consts in scope but route() never
accepted them, so a Router built through register() was metadata-blind:
RequestContext::spec() was always None for handlers and interceptors,
even though the generated FooServiceServer<T> dispatcher for the same
service supplied a Spec for every method. RequestContext::path() (#116)
papers over the procedure name; stream_type and idempotency were lost.

Each registered Method now carries an Option<Spec>, set after the fact by
Router::with_spec(spec) — the route is keyed by spec.procedure so the
codegen can chain it after the route_view* call without changing the
route_* signatures. The generated register() emits .with_spec(SPEC_CONST)
per method, so a Router built through codegen surfaces the same per-method
metadata as the monomorphic dispatcher. Hand-written route_* registrations
without a with_spec call behave exactly as before (spec: None).

Debug builds assert that with_spec finds a registered route at
spec.procedure, and that the route's idempotent flag agrees with
spec.idempotency_level — the latter would otherwise silently disable
Connect GET for idempotent methods.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@github-actions

github-actions Bot commented May 19, 2026 •

Copy link
Copy Markdown

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@iainmcgin
iainmcgin marked this pull request as ready for review May 19, 2026 22:32
@iainmcgin
iainmcgin requested a review from aknott-ant May 19, 2026 22:32
@iainmcgin
iainmcgin enabled auto-merge (squash) May 19, 2026 22:37
@iainmcgin
iainmcgin merged commit 8145aac into main May 19, 2026
12 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators May 19, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants