mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-06 12:37:49 +00:00
Corrected remote document lookup behavior
When remote documents are requested and there is no way to know the base URL, the index will now try and determine the base path from the way the spec was loaded (if it comes in remote, we use the base URL of the spec)
This commit is contained in:
@@ -504,6 +504,35 @@ paths:
|
||||
|
||||
assert.Equal(t, d, strings.TrimSpace(string(rend)))
|
||||
}
|
||||
|
||||
func TestDocument_RemoteWithoutBaseURL(t *testing.T) {
|
||||
|
||||
// This test will push the index to do try and locate remote references that use relative references
|
||||
spec := `openapi: 3.0.2
|
||||
info:
|
||||
title: Test
|
||||
version: 1.0.0
|
||||
paths:
|
||||
/test:
|
||||
get:
|
||||
parameters:
|
||||
- $ref: "https://schemas.opengis.net/ogcapi/features/part2/1.0/openapi/ogcapi-features-2.yaml#/components/parameters/crs"`
|
||||
|
||||
config := datamodel.NewOpenDocumentConfiguration()
|
||||
|
||||
doc, err := NewDocumentWithConfiguration([]byte(spec), config)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
result, errs := doc.BuildV3Model()
|
||||
if len(errs) > 0 {
|
||||
panic(errs)
|
||||
}
|
||||
|
||||
assert.Equal(t, "crs", result.Model.Paths.PathItems["/test"].Get.Parameters[0].Name)
|
||||
}
|
||||
|
||||
func TestDocument_ExampleMap(t *testing.T) {
|
||||
var d = `openapi: "3.1"
|
||||
components:
|
||||
|
||||
@@ -325,8 +325,18 @@ func (index *SpecIndex) performExternalLookup(uri []string, componentId string,
|
||||
newBasePath = filepath.Dir(filepath.Join(index.config.BasePath, filepath.Dir(newUrl.Path)))
|
||||
}
|
||||
if bd != "" {
|
||||
if len(uri[0]) > 0 {
|
||||
// if there is no base url defined, but we can know we have been requested remotely,
|
||||
// set the base url to the remote url base path.
|
||||
if newUrl == nil || newUrl.String() != uri[0] {
|
||||
ur, _ := url.Parse(uri[0])
|
||||
newUrl, _ = url.Parse(fmt.Sprintf("%s://%s%s", ur.Scheme, ur.Host, filepath.Dir(ur.Path)))
|
||||
}
|
||||
newBasePath = filepath.Dir(filepath.Join(bd, uri[1]))
|
||||
} else {
|
||||
newBasePath = filepath.Dir(filepath.Join(bd, uri[0]))
|
||||
}
|
||||
}
|
||||
|
||||
if newUrl != nil || newBasePath != "" {
|
||||
newConfig := &SpecIndexConfig{
|
||||
|
||||
@@ -99,3 +99,30 @@ components:
|
||||
assert.Nil(t, thing)
|
||||
assert.Len(t, index.GetReferenceIndexErrors(), 1)
|
||||
}
|
||||
|
||||
func TestSpecIndex_LocateRemoteDocsWithNoBaseURLSupplied(t *testing.T) {
|
||||
// This test will push the index to do try and locate remote references that use relative references
|
||||
spec := `openapi: 3.0.2
|
||||
info:
|
||||
title: Test
|
||||
version: 1.0.0
|
||||
paths:
|
||||
/test:
|
||||
get:
|
||||
parameters:
|
||||
- $ref: "https://schemas.opengis.net/ogcapi/features/part2/1.0/openapi/ogcapi-features-2.yaml#/components/parameters/crs"`
|
||||
|
||||
var rootNode yaml.Node
|
||||
_ = yaml.Unmarshal([]byte(spec), &rootNode)
|
||||
|
||||
c := CreateOpenAPIIndexConfig()
|
||||
index := NewSpecIndexWithConfig(&rootNode, c)
|
||||
|
||||
// extract crs param from index
|
||||
crsParam := index.GetMappedReferences()["https://schemas.opengis.net/ogcapi/features/part2/1.0/openapi/ogcapi-features-2.yaml#/components/parameters/crs"]
|
||||
assert.NotNil(t, crsParam)
|
||||
assert.True(t, crsParam.IsRemote)
|
||||
assert.Equal(t, "crs", crsParam.Node.Content[1].Value)
|
||||
assert.Equal(t, "query", crsParam.Node.Content[3].Value)
|
||||
assert.Equal(t, "form", crsParam.Node.Content[9].Value)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user