(fix): Remote references with no components now supported by index #37

If a remote reference is used that does not contain a component ID (http://something.com/somefile.json#/components/something). The index would throw an error. This is not the correct behavior as the raw file can be the root, thus no componentID is required.

The bulk of the code is expecting that componentID, so this fix will prepare any component-less remote reference, to be treated properly.

Signed-off-by: Dave Shanley <dave@quobix.com>
This commit is contained in:
Dave Shanley
2022-12-05 09:18:44 -05:00
parent 0a1ed17066
commit 9ccd3bc8b7
2 changed files with 27 additions and 2 deletions

View File

@@ -1573,7 +1573,17 @@ func (index *SpecIndex) FindComponent(componentId string, parent *yaml.Node) *Re
case HttpResolve:
uri := strings.Split(componentId, "#")
if len(uri) == 2 {
if len(uri) >= 2 {
return index.performExternalLookup(uri, componentId, remoteLookup, parent)
}
if len(uri) == 1 {
// if there is no reference, second segment is empty / has no name
// this means there is no component to look-up and the entire file should be pulled in.
// to stop all the other code from breaking (that is expecting a component), let's just post-pend
// a hash to the end of the componentId and ensure the uri slice is as expected.
// described in https://github.com/pb33f/libopenapi/issues/37
componentId = fmt.Sprintf("%s#", componentId)
uri = append(uri, "")
return index.performExternalLookup(uri, componentId, remoteLookup, parent)
}
@@ -1980,7 +1990,12 @@ func (index *SpecIndex) lookupRemoteReference(ref string) (*yaml.Node, *yaml.Nod
}
// lookup item from reference by using a path query.
query := fmt.Sprintf("$%s", strings.ReplaceAll(uri[1], "/", "."))
var query string
if len(uri) >= 2 {
query = fmt.Sprintf("$%s", strings.ReplaceAll(uri[1], "/", "."))
} else {
query = "$"
}
// remove any URL encoding
query = strings.Replace(query, "~1", "./", 1)