Skip to content

feat: strip router path patterns from the OpenAPI document - #1091

Open
stareezy-1 wants to merge 1 commit into
danielgtaylor:mainfrom
stareezy-1:fix/922-regex-openapi-path
Open

feat: strip router path patterns from the OpenAPI document#1091
stareezy-1 wants to merge 1 commit into
danielgtaylor:mainfrom
stareezy-1:fix/922-regex-openapi-path

Conversation

@stareezy-1

Copy link
Copy Markdown

Summary

Fixes #922

Problem

Chi supports regex in path parameters, e.g. /users/{id:[0-9]+}, and huma routes them fine — but the same path is used verbatim as the OpenAPI paths key, so the generated document contains an invalid path:

"/users/{id:[0-9]+}": { ... }

Reproduction: register any operation on a regex path with humachi and inspect api.OpenAPI().Paths.

Fix

Normalize the documented path in AddOperation{id:[0-9]+} becomes {id} — while leaving op.Path untouched so the router keeps the regex. The normalization uses a brace-aware scan, so quantifiers like {3} inside the pattern do not terminate the segment early ({id:[a-z]{3}}{id}).

Tests

TestRegexPathParamNormalizedInOpenAPI in huma_test.go:

Assertion Verifies
Paths contains /users/{id} regex stripped from the document
Paths does not contain /users/{id:[0-9]+} raw pattern not documented
GET /users/123 via the chi router routing still works end to end

All existing tests continue to pass (go test -race ./...).

Router-specific path parameter patterns such as chi's `{id:[0-9]+}` are
valid for routing but produce a broken OpenAPI path. Document the operation
under the normalized path (`{id}`) while leaving op.Path untouched for the
router, using a brace-aware scan so quantifiers like {3} inside the pattern
do not terminate the segment early.
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.

Regex in path params working - but breaking OpenAPI

1 participant