Skip to content

Advertise route prefix via servers instead of on every path - #11

Merged
Doomblaster merged 1 commit into
devfrom
squad/servers-base-path
Sep 1, 2026
Merged

Doomblaster merged 1 commit into
devfrom
squad/servers-base-path

Conversation

@Doomblaster

Copy link
Copy Markdown
Owner

Summary

Moves the Functions route prefix (api by default) out of every generated path key and into the document's top-level servers array — the idiomatic OpenAPI representation of a shared base path.

  • Path keys are now relative: /items, not /api/items.
  • Effective operation URL = servers[].url + path key.
  • Valid in both OpenAPI 3.0 and 3.1.

Server URL resolution

  • Default: inferred from the incoming request — scheme, host, and path base, honouring X-Forwarded-Proto / X-Forwarded-Host. The advertised base therefore matches wherever the doc is served from (local, deployed, behind a proxy).
  • Fallback: relative base path (/api) when the host can't be resolved.
  • Override: new OpenApiOptions.Servers — entries are used verbatim and request inference is skipped (for fixed gateway/APIM URLs).

The per-request servers are applied to a shallow copy of the cached document (new OpenApiDocument(cached) { Servers = ... }), so the shared singleton is never mutated when different hosts hit the endpoint concurrently.

Changes

  • FunctionEndpointDiscovery — emits route-relative paths (routePrefix removed from Discover/TryDiscover/CombinePath).
  • OpenApiOptions — added Servers (IList<OpenApiServer>); refined RoutePrefix docs.
  • OpenApiHttpFunctions — per-request servers injection (BuildServers, WithServers, ResolveRequestBaseUrl).
  • Tests — updated Discovery + DocumentGeneration to relative paths; new ServerBasePathTests.cs (6 tests: inference, forwarded headers, relative fallback, empty prefix, explicit override, per-host isolation without cache mutation).
  • README + CHANGELOG updated.

Testing

dotnet test — 156/156 pass.

Note

This is a document-shape change: consumers relying on /api in the generated path keys must update. Pre-1.0, so acceptable — recommend releasing as 0.2.0.

Path keys are now relative to the API base (e.g. `/items`, not `/api/items`).
The Functions route prefix is a global base path, so it is advertised once via
the document's top-level `servers` array — the idiomatic OpenAPI representation
of a shared base URL (effective URL = servers[].url + path key).

By default the server URL is inferred from the incoming request (scheme, host,
path base, honouring X-Forwarded-Proto / X-Forwarded-Host), with a relative
fallback when the host is unresolved. The new OpenApiOptions.Servers lets callers
advertise fixed URLs (e.g. an API gateway) verbatim, skipping request inference.

The per-request servers are applied to a shallow copy of the cached document so
the shared singleton is never mutated across differing hosts.

BREAKING (pre-1.0): consumers relying on `/api` in the generated path keys must
update. Recommend releasing as 0.2.0.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@Doomblaster
Doomblaster merged commit 7c008dc into dev Sep 1, 2026
1 check passed
@Doomblaster
Doomblaster deleted the squad/servers-base-path branch September 1, 2026 20:17
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.

1 participant