Skip to content

Commit 387bc08

Browse files
fredbiclaude
andcommitted
fix(normalizer): anchor a $ref joined onto a base with no path
A base that carries a scheme but no path of its own ("a:", "smb://host") leaves the joined path relative. Rendering that gives "scheme://path", which promotes the path to an authority component: "a://some file.json" no longer parses, and normalizing such a $ref panics instead of resolving it. Anchor the joined path so that the rendered URI stays canonical. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
1 parent e592343 commit 387bc08

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

‎normalizer.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ func normalizeURI(refPath, base string) string {
5656
// copying fragment from ref to base
5757
baseURL.Fragment = refURL.Fragment
5858

59+
if baseURL.Scheme != "" && baseURL.Path != "" && !path.IsAbs(baseURL.Path) {
60+
// a base that carries no path of its own (e.g. "smb://host") leaves the join relative.
61+
// Anchor it, because a relative path under a scheme does not survive rendering: it is
62+
// either promoted to a host component, which is not even guaranteed to be a legal one
63+
// ("a://some file.json" no longer parses), or read back as absolute anyway.
64+
baseURL.Path = path.Join("/", baseURL.Path)
65+
}
66+
5967
return baseURL.String()
6068
}
6169

‎normalizer_test.go‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,39 @@ func TestNormalizer_NormalizeURI(t *testing.T) {
160160
base: "file:///base/path.json",
161161
expOutput: "file:///base/path.json",
162162
},
163+
{
164+
// a base with a scheme but no path leaves the join relative: it has to be anchored,
165+
// or "a://some file.json" renders the path as a host and no longer parses
166+
refPath: "some file.json#/definitions/X",
167+
base: "a:",
168+
expOutput: "a:///some%20file.json#/definitions/X",
169+
},
170+
{
171+
// same, with dot segments to clean away
172+
refPath: "..",
173+
base: "a:",
174+
expOutput: "a:///",
175+
},
176+
{
177+
// a base with an authority but no path
178+
refPath: "dir/file.json#/definitions/X",
179+
base: "smb://host",
180+
expOutput: "smb://host/dir/file.json#/definitions/X",
181+
},
182+
{
183+
// anchoring happens after the windows tolerance has slashed the path
184+
refPath: `sub\file.json#/definitions/X`,
185+
base: "a:",
186+
expOutput: "a:///sub/file.json#/definitions/X",
187+
windows: true,
188+
},
189+
{
190+
// on other platforms a backslash is an ordinary path character, and gets escaped
191+
refPath: `sub\file.json#/definitions/X`,
192+
base: "a:",
193+
expOutput: "a:///sub%5Cfile.json#/definitions/X",
194+
nonWindows: true,
195+
},
163196
{
164197
// file basePath, absolute refPath, no fragment
165198
refPath: `C:\another\base\path.json`,

0 commit comments

Comments
 (0)
⌂ 换网址