Skip to content

Template wildcard route segments in exported OpenAPI - #29

Open
VSN2015 wants to merge 1 commit into
masterfrom
fix/openapi-glob-routes
Open

Template wildcard route segments in exported OpenAPI#29
VSN2015 wants to merge 1 commit into
masterfrom
fix/openapi-glob-routes

Conversation

@VSN2015

@VSN2015 VSN2015 commented Sep 4, 2026

Copy link
Copy Markdown
Owner

A one-line bug fix, found in the same sweep as #28.

The bug

OpenAPI.rails_routes templated the :id form of a Rails path parameter but not the *rest wildcard. So an ordinary route:

get "files/*path", to: "files#show"

exported as:

"paths": { "/files/*path": { "get": { ... } } }

/files/*path is not a valid OpenAPI path template, and an invalid path makes the whole document fail validation — so one glob route silently breaks the export for the entire app.

The fix

-.gsub(/:(\w+)/) { "{#{Regexp.last_match(1)}}" }
+.gsub(/[:*](\w+)/) { "{#{Regexp.last_match(1)}}" }
Route Before After
files/*path /files/*path /files/{path}
files/:bucket/*path /files/{bucket}/*path /files/{bucket}/{path}

Verification

  • 200 examples, 0 failures (1 new, written before the fix, covering a plain wildcard and one mixed with a named segment)
  • The existing .rails_routes spec is unchanged and still passes, so :id templating and the skipping of internal/verbless routes are unaffected
  • RuboCop clean

@VSN2015 VSN2015 left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Review of PR #29: Concise fix for Rails route wildcards. Translating *path to {path} generates valid OpenAPI route templates for catch-all endpoints.

next if requirements[:controller].nil? || requirements[:action].nil? || verb.empty?

path = route.path.spec.to_s.sub("(.:format)", "").gsub(/:(\w+)/) { "{#{Regexp.last_match(1)}}" }
path = route.path.spec.to_s.sub("(.:format)", "").gsub(/[:*](\w+)/) { "{#{Regexp.last_match(1)}}" }

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Updating the substitution regex to /[:*](\w+)/ cleanly handles both :id parameter segments and *path wildcard segments (such as /files/:bucket/*path).

rails_routes templated the :id form of a Rails path parameter but not
the *rest wildcard, so an ordinary route

  get "files/*path", to: "files#show"

exported the path /files/*path — which is not a valid OpenAPI path
template. An invalid path makes the whole document fail validation, so
one glob route silently broke the export for the entire app.

Both forms are now templated, including a route mixing them:
/files/:bucket/*path becomes /files/{bucket}/{path}.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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