Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix panic slice out of range error #652 #654

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions openapi3/issue652_test.go
@@ -0,0 +1,29 @@
package openapi3_test

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/getkin/kin-openapi/openapi3"
)

func TestIssue652(t *testing.T) {
loader := openapi3.NewLoader()
loader.IsExternalRefsAllowed = true

// Test checks that no slice bounds out of range error occurs while loading
// from file that contains reference to file in the parent directory.
require.NotPanics(t, func() {
const schemaName = "ReferenceToParentDirectory"

spec, err := loader.LoadFromFile("testdata/issue652/nested/schema.yml")
require.NoError(t, err)
require.Contains(t, spec.Components.Schemas, schemaName)

schema := spec.Components.Schemas[schemaName]
assert.Equal(t, schema.Ref, "../definitions.yml#/components/schemas/TestSchema")
assert.Equal(t, schema.Value.Type, "string")
})
}
22 changes: 15 additions & 7 deletions openapi3/loader.go
Expand Up @@ -742,7 +742,10 @@ func (loader *Loader) resolveSchemaRef(doc *T, component *SchemaRef, documentPat
return err
}
component.Value = resolved.Value
foundPath := loader.getResolvedRefPath(ref, &resolved, documentPath, componentPath)
foundPath, rerr := loader.getResolvedRefPath(ref, &resolved, documentPath, componentPath)
if rerr != nil {
return fmt.Errorf("failed to resolve file from reference %q: %w", ref, rerr)
}
documentPath = loader.documentPathForRecursiveRef(documentPath, foundPath)
}
}
Expand Down Expand Up @@ -790,23 +793,28 @@ func (loader *Loader) resolveSchemaRef(doc *T, component *SchemaRef, documentPat
return nil
}

func (loader *Loader) getResolvedRefPath(ref string, resolved *SchemaRef, cur, found *url.URL) string {
func (loader *Loader) getResolvedRefPath(ref string, resolved *SchemaRef, cur, found *url.URL) (string, error) {
if referencedFilename := strings.Split(ref, "#")[0]; referencedFilename == "" {
if cur != nil {
if loader.rootDir != "" && strings.HasPrefix(cur.Path, loader.rootDir) {
return cur.Path[len(loader.rootDir)+1:]
return cur.Path[len(loader.rootDir)+1:], nil
}

return path.Base(cur.Path)
return path.Base(cur.Path), nil
}
return ""
return "", nil
}
// ref. to external file
if resolved.Ref != "" {
return resolved.Ref
return resolved.Ref, nil
}

if loader.rootDir == "" {
return found.Path, nil
}

// found dest spec. file
return path.Dir(found.Path)[len(loader.rootDir):]
return filepath.Rel(loader.rootDir, found.Path)
}

func (loader *Loader) resolveSecuritySchemeRef(doc *T, component *SecuritySchemeRef, documentPath *url.URL) (err error) {
Expand Down
4 changes: 4 additions & 0 deletions openapi3/testdata/issue652/definitions.yml
@@ -0,0 +1,4 @@
components:
schemas:
TestSchema:
type: string
4 changes: 4 additions & 0 deletions openapi3/testdata/issue652/nested/schema.yml
@@ -0,0 +1,4 @@
components:
schemas:
ReferenceToParentDirectory:
$ref: "../definitions.yml#/components/schemas/TestSchema"