From 5d14ef226b7df582acbae46ff19595a46f40f92e Mon Sep 17 00:00:00 2001 From: quobix Date: Wed, 22 Nov 2023 14:10:18 -0500 Subject: [PATCH] Adding more tests to bump coverage Signed-off-by: quobix --- datamodel/low/base/schema_proxy_test.go | 65 +++++++++++++++++++++++++ index/index_model.go | 10 ++++ index/map_index_nodes.go | 7 ++- index/spec_index_test.go | 4 ++ 4 files changed, 84 insertions(+), 2 deletions(-) diff --git a/datamodel/low/base/schema_proxy_test.go b/datamodel/low/base/schema_proxy_test.go index 964308e..feb0232 100644 --- a/datamodel/low/base/schema_proxy_test.go +++ b/datamodel/low/base/schema_proxy_test.go @@ -6,6 +6,7 @@ package base import ( "context" "github.com/pb33f/libopenapi/datamodel/low" + "github.com/pb33f/libopenapi/index" "github.com/stretchr/testify/assert" "gopkg.in/yaml.v3" "testing" @@ -96,3 +97,67 @@ x-common-definitions: assert.Equal(t, "The type of life cycle", sch.Schema().Description.Value) } + +func TestSchemaProxy_GetSchemaReferenceLocation(t *testing.T) { + + yml := `type: object +properties: + name: + type: string + description: thing` + + var idxNodeA yaml.Node + e := yaml.Unmarshal([]byte(yml), &idxNodeA) + assert.NoError(t, e) + + yml = ` +type: object +properties: + name: + type: string + description: thang` + + var schA SchemaProxy + var schB SchemaProxy + var schC SchemaProxy + var idxNodeB yaml.Node + _ = yaml.Unmarshal([]byte(yml), &idxNodeB) + + c := index.CreateOpenAPIIndexConfig() + rolo := index.NewRolodex(c) + rolo.SetRootNode(&idxNodeA) + _ = rolo.IndexTheRolodex() + + err := schA.Build(context.Background(), nil, idxNodeA.Content[0], rolo.GetRootIndex()) + assert.NoError(t, err) + err = schB.Build(context.Background(), nil, idxNodeB.Content[0].Content[3].Content[1], rolo.GetRootIndex()) + assert.NoError(t, err) + + rolo.GetRootIndex().SetAbsolutePath("/rooty/rootster") + origin := schA.GetSchemaReferenceLocation() + assert.NotNil(t, origin) + assert.Equal(t, "/rooty/rootster", origin.AbsoluteLocation) + + // mess things up so it cannot be found + schA.vn = schB.vn + origin = schA.GetSchemaReferenceLocation() + assert.Nil(t, origin) + + // create a new index + idx := index.NewSpecIndexWithConfig(&idxNodeB, c) + idx.SetAbsolutePath("/boaty/mcboatface") + + // add the index to the rolodex + rolo.AddIndex(idx) + + // can now find the origin + origin = schA.GetSchemaReferenceLocation() + assert.NotNil(t, origin) + assert.Equal(t, "/boaty/mcboatface", origin.AbsoluteLocation) + + // do it again, but with no index + err = schC.Build(context.Background(), nil, idxNodeA.Content[0], nil) + origin = schC.GetSchemaReferenceLocation() + assert.Nil(t, origin) + +} diff --git a/index/index_model.go b/index/index_model.go index c10cad8..3c8c582 100644 --- a/index/index_model.go +++ b/index/index_model.go @@ -296,6 +296,16 @@ func (index *SpecIndex) GetCache() *syncmap.Map { return index.cache } +// SetAbsolutePath sets the absolute path to the spec file for the index. Will be absolute, either as a http link or a file. +func (index *SpecIndex) SetAbsolutePath(absolutePath string) { + index.specAbsolutePath = absolutePath +} + +// GetAbsolutePath returns the absolute path to the spec file for the index. Will be absolute, either as a http link or a file. +func (index *SpecIndex) GetAbsolutePath() string { + return index.specAbsolutePath +} + // ExternalLookupFunction is for lookup functions that take a JSONSchema reference and tries to find that node in the // URI based document. Decides if the reference is local, remote or in a file. type ExternalLookupFunction func(id string) (foundNode *yaml.Node, rootNode *yaml.Node, lookupError error) diff --git a/index/map_index_nodes.go b/index/map_index_nodes.go index dda0656..02711d0 100644 --- a/index/map_index_nodes.go +++ b/index/map_index_nodes.go @@ -70,13 +70,16 @@ func (index *SpecIndex) MapNodes(rootNode *yaml.Node) { close(index.nodeMapCompleted) } +// FindNodeOrigin searches this index for a matching node. If the node is found, a NodeOrigin +// is returned, otherwise nil is returned. func (index *SpecIndex) FindNodeOrigin(node *yaml.Node) *NodeOrigin { - - // local search, then throw up to rolodex for a full search if node != nil { if index.nodeMap[node.Line] != nil { if index.nodeMap[node.Line][node.Column] != nil { foundNode := index.nodeMap[node.Line][node.Column] + if foundNode.Kind == yaml.DocumentNode { + foundNode = foundNode.Content[0] + } match := true if foundNode.Value != node.Value || foundNode.Kind != node.Kind || foundNode.Tag != node.Tag { match = false diff --git a/index/spec_index_test.go b/index/spec_index_test.go index a5c1e06..68d047c 100644 --- a/index/spec_index_test.go +++ b/index/spec_index_test.go @@ -519,6 +519,10 @@ func TestSpecIndex_PetstoreV3(t *testing.T) { assert.Equal(t, 19, index.GetAllSummariesCount()) assert.Len(t, index.GetAllDescriptions(), 90) assert.Len(t, index.GetAllSummaries(), 19) + + index.SetAbsolutePath("/rooty/rootster") + assert.Equal(t, "/rooty/rootster", index.GetSpecAbsolutePath()) + } var mappedRefs = 15