Skip to content

Commit

Permalink
fix panic slice out of range error getkin#652
Browse files Browse the repository at this point in the history
Fixes panic from the issue getkin#652, that occurs when trying to load
specification from file that contains reference to the file insed of
parent directory.
  • Loading branch information
nk2ge5k committed Nov 5, 2022
1 parent 9ea22ae commit 97dd8ac
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
20 changes: 20 additions & 0 deletions openapi3/issue652_test.go
@@ -0,0 +1,20 @@
package openapi3_test

import (
"testing"

"github.com/getkin/kin-openapi/openapi3"
"github.com/stretchr/testify/require"
)

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() {
_, err := loader.LoadFromFile("testdata/issue652/nested/schema.yml")
require.NoError(t, err)
})
}
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 for 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 path.Dir(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
7 changes: 7 additions & 0 deletions openapi3/testdata/issue652/nested/schema.yml
@@ -0,0 +1,7 @@
components:
schemas:
TestObject:
type: object
properties:
reference_to_parent_directory:
$ref: "../definitions.yml#/components/schemas/TestSchema"

0 comments on commit 97dd8ac

Please sign in to comment.