Fix API docs index canonical URLs#347
Conversation
Code Review — PR #347: Fix API docs index canonical URLsOverviewThis PR ensures that API docs index/directory URLs always end with a trailing slash in canonical What's Good
Issues & Suggestions1. Bug: relative URLs with query strings produce malformed outputvar path = normalized;
var queryIndex = path.IndexOfAny(new[] { '?', '#' });
if (queryIndex >= 0)
path = path[..queryIndex]; // path = "/api"
if (Uri.TryCreate(normalized, UriKind.Absolute, out var absolute))
path = absolute.AbsolutePath;
return string.IsNullOrWhiteSpace(Path.GetExtension(path))
? normalized + "/" // ⚠️ still appends to full normalized, not stripped path
: normalized;For a relative URL like
The fix is to append to the stripped path or insert the slash before the query: return string.IsNullOrWhiteSpace(Path.GetExtension(path))
? (queryIndex >= 0 ? normalized[..queryIndex] + "/" + normalized[queryIndex..] : normalized + "/")
: normalized;In practice 2. Dead check:
|
| Input | Expected |
|---|---|
null / "" |
"/" |
/api |
/api/ |
/api/ |
/api/ (no double slash) |
https://example.test/api |
https://example.test/api/ |
https://example.test/api/ |
https://example.test/api/ |
/api/index.html |
/api/index.html (no slash) |
/api?v=2 |
/api/?v=2 (currently produces /api?v=2/) |
Summary
The core intent is correct and the change is safe for the project's current URL patterns. The most actionable fix before merging is item #1 (malformed URL for query-string inputs), even if it's a low-probability scenario today. Items #2–#4 are polish. Adding a small unit test table for the new function would make future refactors safer.
Summary
Validation