Skip to content

Commit

Permalink
cleanup after #583 (#585)
Browse files Browse the repository at this point in the history
  • Loading branch information
fenollp committed Aug 29, 2022
1 parent 2470727 commit efe7ae9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 26 deletions.
36 changes: 12 additions & 24 deletions openapi3/load_cicular_ref_with_external_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package openapi3_test

import (
"embed"
"encoding/json"
"net/url"
"testing"

Expand All @@ -24,38 +25,24 @@ func TestLoadCircularRefFromFile(t *testing.T) {
}

got, err := loader.LoadFromFile("testdata/circularRef/base.yml")
if err != nil {
t.Error(err)
}
require.NoError(t, err)

foo := &openapi3.SchemaRef{
Ref: "",
Value: &openapi3.Schema{
ExtensionProps: openapi3.ExtensionProps{Extensions: map[string]interface{}{}},
Properties: map[string]*openapi3.SchemaRef{
"foo2": { // reference to an external file
Ref: "other.yml#/components/schemas/Foo2",
"foo2": {
Ref: "other.yml#/components/schemas/Foo2", // reference to an external file
Value: &openapi3.Schema{
ExtensionProps: openapi3.ExtensionProps{Extensions: map[string]interface{}{}},
Properties: map[string]*openapi3.SchemaRef{
"id": {
Value: &openapi3.Schema{
Type: "string",
ExtensionProps: openapi3.ExtensionProps{Extensions: map[string]interface{}{}},
}},
Value: &openapi3.Schema{Type: "string"}},
},
},
},
},
},
}
bar := &openapi3.SchemaRef{
Ref: "",
Value: &openapi3.Schema{
ExtensionProps: openapi3.ExtensionProps{Extensions: map[string]interface{}{}},
Properties: map[string]*openapi3.SchemaRef{},
},
}
bar := &openapi3.SchemaRef{Value: &openapi3.Schema{Properties: make(map[string]*openapi3.SchemaRef)}}
// circular reference
bar.Value.Properties["foo"] = &openapi3.SchemaRef{Ref: "#/components/schemas/Foo", Value: foo.Value}
foo.Value.Properties["bar"] = &openapi3.SchemaRef{Ref: "#/components/schemas/Bar", Value: bar.Value}
Expand All @@ -65,18 +52,19 @@ func TestLoadCircularRefFromFile(t *testing.T) {
Info: &openapi3.Info{
Title: "Recursive cyclic refs example",
Version: "1.0",

ExtensionProps: openapi3.ExtensionProps{Extensions: map[string]interface{}{}},
},
Components: openapi3.Components{
ExtensionProps: openapi3.ExtensionProps{Extensions: map[string]interface{}{}},
Schemas: map[string]*openapi3.SchemaRef{
"Foo": foo,
"Bar": bar,
},
},
ExtensionProps: openapi3.ExtensionProps{Extensions: map[string]interface{}{}},
}

require.Equal(t, want, got)
jsoner := func(doc *openapi3.T) string {
data, err := json.Marshal(doc)
require.NoError(t, err)
return string(data)
}
require.JSONEq(t, jsoner(want), jsoner(got))
}
3 changes: 1 addition & 2 deletions openapi3/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -748,8 +748,7 @@ func (loader *Loader) resolveSchemaRef(doc *T, component *SchemaRef, documentPat
}

func (loader *Loader) getResolvedRefPath(ref string, resolved *SchemaRef, cur, found *url.URL) string {
referencedFilename := strings.Split(ref, "#")[0]
if referencedFilename == "" {
if referencedFilename := strings.Split(ref, "#")[0]; referencedFilename == "" {
if cur != nil {
return path.Base(cur.Path)
}
Expand Down

0 comments on commit efe7ae9

Please sign in to comment.