diff --git a/datamodel/low/3.0/create_document_test.go b/datamodel/low/3.0/create_document_test.go index 18f60d4..a9f4a4f 100644 --- a/datamodel/low/3.0/create_document_test.go +++ b/datamodel/low/3.0/create_document_test.go @@ -316,9 +316,9 @@ func TestCreateDocument_Components_Schemas(t *testing.T) { assert.NotNil(t, components) assert.Len(t, components.Schemas.Value, 6) - burger := components.FindSchema("Burger") - assert.NotNil(t, burger.Value) - assert.Equal(t, "The tastiest food on the planet you would love to eat everyday", burger.Value.Schema().Description.Value) + burger := components.FindSchema("Burger").Value + assert.NotNil(t, burger) + assert.Equal(t, "The tastiest food on the planet you would love to eat everyday", burger.Schema().Description.Value) er := components.FindSchema("Error") assert.NotNil(t, er.Value) diff --git a/datamodel/low/extraction_functions.go b/datamodel/low/extraction_functions.go index a2819d1..02d4440 100644 --- a/datamodel/low/extraction_functions.go +++ b/datamodel/low/extraction_functions.go @@ -11,7 +11,6 @@ import ( "gopkg.in/yaml.v3" "strconv" "strings" - "sync" ) func FindItemInMap[T any](item string, collection map[KeyReference[string]]ValueReference[T]) *ValueReference[T] { @@ -23,8 +22,6 @@ func FindItemInMap[T any](item string, collection map[KeyReference[string]]Value return nil } -var mapLock sync.Mutex - func LocateRefNode(root *yaml.Node, idx *index.SpecIndex) *yaml.Node { if rf, _, rv := utils.IsNodeRefValue(root); rf { // run through everything and return as soon as we find a match. @@ -80,7 +77,8 @@ func LocateRefNode(root *yaml.Node, idx *index.SpecIndex) *yaml.Node { cleaned := strings.ReplaceAll(rv, "#/paths/", "") cleaned = strings.ReplaceAll(cleaned, "/", ".") cleaned = strings.ReplaceAll(cleaned, "~1", "/") - path, err := yamlpath.NewPath(fmt.Sprintf("$.paths.%s", cleaned)) + yamlPath := fmt.Sprintf("$.paths.%s", cleaned) + path, err := yamlpath.NewPath(yamlPath) if err == nil { nodes, fErr := path.Find(idx.GetRootNode()) if fErr == nil { diff --git a/datamodel/low/extraction_functions_test.go b/datamodel/low/extraction_functions_test.go new file mode 100644 index 0000000..d5b8510 --- /dev/null +++ b/datamodel/low/extraction_functions_test.go @@ -0,0 +1,74 @@ +// Copyright 2022 Princess B33f Heavy Industries / Dave Shanley +// SPDX-License-Identifier: MIT + +package low + +import ( + "github.com/pb33f/libopenapi/index" + "github.com/stretchr/testify/assert" + "gopkg.in/yaml.v3" + "testing" +) + +func TestFindItemInMap(t *testing.T) { + v := make(map[KeyReference[string]]ValueReference[string]) + v[KeyReference[string]{ + Value: "pizza", + }] = ValueReference[string]{ + Value: "pie", + } + assert.Equal(t, "pie", FindItemInMap("pizza", v).Value) +} + +func TestFindItemInMap_Error(t *testing.T) { + v := make(map[KeyReference[string]]ValueReference[string]) + v[KeyReference[string]{ + Value: "pizza", + }] = ValueReference[string]{ + Value: "pie", + } + assert.Nil(t, FindItemInMap("nuggets", v)) +} + +func TestLocateRefNode(t *testing.T) { + + yml := `components: + schemas: + cake: + description: hello` + + var idxNode yaml.Node + mErr := yaml.Unmarshal([]byte(yml), &idxNode) + assert.NoError(t, mErr) + idx := index.NewSpecIndex(&idxNode) + + yml = `$ref: '#/components/schemas/cake'` + + var cNode yaml.Node + _ = yaml.Unmarshal([]byte(yml), &cNode) + + located := LocateRefNode(cNode.Content[0], idx) + assert.NotNil(t, located) + +} + +func TestLocateRefNode_Path(t *testing.T) { + + yml := `paths: + /burger/time: + description: hello` + + var idxNode yaml.Node + mErr := yaml.Unmarshal([]byte(yml), &idxNode) + assert.NoError(t, mErr) + idx := index.NewSpecIndex(&idxNode) + + yml = `$ref: '#/paths/~1burger~1time'` + + var cNode yaml.Node + _ = yaml.Unmarshal([]byte(yml), &cNode) + + located := LocateRefNode(cNode.Content[0], idx) + assert.NotNil(t, located) + +}