fix: optimize OAS path ordering by only adding synthetic entries for conflicting paths#7966
Conversation
|
This PR introduces a significant optimization to the OpenAPI Specification (OAS) path ordering and compilation logic, addressing both a performance issue and a prefix matching bug. The key change is moving from a brute-force approach—where synthetic "shield" entries were created for all static paths—to a more intelligent, conflict-based system. Now, the gateway first compiles a list of regular expressions for only the parameterized paths that have middleware enabled. It then iterates through the static paths and creates a disabled shield entry only if a static path matches one of the parameterized regexes. This prevents the internal regex list from growing unnecessarily large for APIs with many non-conflicting static paths, improving performance. Additionally, a new custom sorting function,
These changes are validated with an expanded benchmark test that now includes 100 static paths and a new integration test ( Files Changed Analysis
Architecture & Impact Assessment
Optimized Path Compilation Flowgraph TD
A[Start OAS Path Compilation] --> B[Sort Paths using new custom logic];
B --> C{First Pass: Collect Regexes<br>for Parameterized Paths w/ Middleware};
C --> D{Second Pass: Iterate All Paths};
D --|For Each Path|--> E{Is Path Static?};
E --|"No (Parameterized)"|--> F[Process middleware as configured];
E --|"Yes (Static)"|--> G{Does it conflict with a<br>parameterized regex?};
G --|No|--> H[Ignore for this middleware];
G --|Yes|--> I[Create synthetic disabled "shield" entry];
F --> J[Add to URL Spec];
I --> J;
H --> K[End Loop];
J --> K;
Scope Discovery & Context Expansion
Metadata
Powered by Visor from Probelabs Last updated: 2026-04-06T07:29:03.691Z | Triggered by: pr_updated | Commit: f3ca8f9 💡 TIP: You can chat with Visor using |
Security Issues (2)
Architecture Issues (1)
Performance Issues (2)
Quality Issues (1)
Powered by Visor from Probelabs Last updated: 2026-04-06T07:28:30.499Z | Triggered by: pr_updated | Commit: f3ca8f9 💡 TIP: You can chat with Visor using |
|
API Changes no api changes detected |
🚨 Jira Linter FailedCommit: The Jira linter failed to validate your PR. Please check the error details below: 🔍 Click to view error detailsNext Steps
This comment will be automatically deleted once the linter passes. |
Problem / Task
The original fix for OAS path ordering (PR 7964) added synthetic disabled entries for EVERY static path that didn't have middleware enabled. This caused the regex list to grow linearly with the number of static paths, which could impact performance for APIs with many static paths.
Additionally, using
oasutil.SortByPathLengthcaused a prefix matching flaw where parameterized paths with more fragments were evaluated before static paths with fewer fragments. Ifenable_suffix: false(prefix matching enabled), a request to/employees/static/abc/defwould incorrectly hit the parameterized path instead of the static prefix.Changes
/users/adminconflicts with/users/{id}, but/healthdoes not).sortPathsForOASMiddleware) specifically for OAS middleware compilation to enforce strict precedence:/employees/static/{id}before/employees/{id}/static).compileOASValidateRequestPathSpecandcompileOASMockResponsePathSpecto use this new sorting function.Testing
TestOASValidateRequestPrefixMatchingto verify prefix matching behavior (/employees/staticvs/employees/{id}/abc/def).go test ./gateway -run=TestOASandgo test ./gateway -run=TestMockResponseto ensure no regressions.