cleaned up path handling a little

Signed-off-by: quobix <dave@quobix.com>
This commit is contained in:
quobix
2024-01-03 14:13:23 -05:00
parent ee50403678
commit cbdaac7374
8 changed files with 105 additions and 80 deletions

View File

@@ -450,6 +450,7 @@ func (n *NodeBuilder) AddYAMLNode(parent *yaml.Node, entry *nodes.NodeEntry) *ya
} else {
// try an inline render if we can, otherwise there is no option but to default to the
// full render.
if _, ko := r.(RenderableInline); ko {
rawRender, _ = r.(RenderableInline).MarshalYAMLInline()
} else {

View File

@@ -840,7 +840,7 @@ func TestCreateDocument_YamlAnchor(t *testing.T) {
assert.NotNil(t, postParams)
assert.Equal(t, 1, len(getParams))
assert.Equal(t, 1, len(postParams))
assert.Equal(t, getParams, postParams)
assert.Equal(t, getParams[0].ValueNode, postParams[0].ValueNode)
// check post request body
responses := examplePath.GetValue().Get.GetValue().GetResponses().Value.(*Responses)

View File

@@ -529,8 +529,8 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
}
}
seenPath = append(seenPath, strings.ReplaceAll(n.Value, "/", "~1"))
//seenPath = append(seenPath, n.Value)
//seenPath = append(seenPath, strings.ReplaceAll(n.Value, "/", "~1"))
seenPath = append(seenPath, n.Value)
prev = n.Value
}

View File

@@ -971,7 +971,7 @@ func (index *SpecIndex) GetOperationCount() int {
Definition: m.Value,
Name: m.Value,
Node: method.Content[y+1],
Path: fmt.Sprintf("$.paths.%s.%s", p.Value, m.Value),
Path: fmt.Sprintf("$.paths['%s'].%s", p.Value, m.Value),
ParentNode: m,
}
if locatedPathRefs[p.Value] == nil {
@@ -1039,7 +1039,7 @@ func (index *SpecIndex) GetOperationsParameterCount() int {
Name: serverRef.Value,
Node: serverRef,
ParentNode: prop,
Path: fmt.Sprintf("$.paths.%s.servers[%d]", pathItemNode.Value, i),
Path: fmt.Sprintf("$.paths['%s'].servers[%d]", pathItemNode.Value, i),
}
serverRefs = append(serverRefs, ref)
}
@@ -1123,7 +1123,7 @@ func (index *SpecIndex) GetOperationsParameterCount() int {
Name: "servers",
Node: serverRef,
ParentNode: httpMethodProp,
Path: fmt.Sprintf("$.paths.%s.%s.servers[%d]", pathItemNode.Value, prop.Value, i),
Path: fmt.Sprintf("$.paths['%s'].%s.servers[%d]", pathItemNode.Value, prop.Value, i),
}
serverRefs = append(serverRefs, ref)
}

View File

@@ -1214,7 +1214,7 @@ components:
assert.Equal(t, "$.components.parameters.param2", params["/"]["get"]["#/components/parameters/param2"][0].Path)
}
if assert.Contains(t, params["/"]["get"], "test") {
assert.Equal(t, "$.paths./.get.parameters[2]", params["/"]["get"]["test"][0].Path)
assert.Equal(t, "$.paths['/'].get.parameters[2]", params["/"]["get"]["test"][0].Path)
}
}
}
@@ -1255,7 +1255,7 @@ paths:
opPath = ""
}
assert.Equal(t, fmt.Sprintf("$.paths.%s%s.servers[%d]", path, opPath, i), server.Path)
assert.Equal(t, fmt.Sprintf("$.paths['%s']%s.servers[%d]", path, opPath, i), server.Path)
}
}
}
@@ -1278,7 +1278,7 @@ func TestSpecIndex_schemaComponentsHaveParentsAndPaths(t *testing.T) {
for _, schema := range schemas {
assert.NotNil(t, schema.ParentNode)
assert.Equal(t, fmt.Sprintf("$.components.schemas.%s", schema.Name), schema.Path)
assert.Equal(t, fmt.Sprintf("$.components.schemas['%s']", schema.Name), schema.Path)
}
}
@@ -1497,12 +1497,12 @@ paths:
paths := idx.GetAllPaths()
assert.Equal(t, "$.paths./test.get", paths["/test"]["get"].Path)
assert.Equal(t, "$.paths['/test'].get", paths["/test"]["get"].Path)
assert.Equal(t, 9, paths["/test"]["get"].ParentNode.Line)
assert.Equal(t, "$.paths./test.post", paths["/test"]["post"].Path)
assert.Equal(t, "$.paths['/test'].post", paths["/test"]["post"].Path)
assert.Equal(t, 13, paths["/test"]["post"].ParentNode.Line)
assert.Equal(t, "$.paths./test2.delete", paths["/test2"]["delete"].Path)
assert.Equal(t, "$.paths['/test2'].delete", paths["/test2"]["delete"].Path)
assert.Equal(t, 18, paths["/test2"]["delete"].ParentNode.Line)
assert.Equal(t, "$.paths./test2.put", paths["/test2"]["put"].Path)
assert.Equal(t, "$.paths['/test2'].put", paths["/test2"]["put"].Path)
assert.Equal(t, 22, paths["/test2"]["put"].ParentNode.Line)
}

View File

@@ -30,7 +30,7 @@ func (index *SpecIndex) extractDefinitionsAndSchemas(schemasNode *yaml.Node, pat
Definition: def,
Name: name,
Node: schema,
Path: fmt.Sprintf("$.components.schemas.%s", name),
Path: fmt.Sprintf("$.components.schemas['%s']", name),
ParentNode: schemasNode,
RequiredRefProperties: extractDefinitionRequiredRefProperties(schemasNode, map[string][]string{}, fullDef, index),
}
@@ -384,9 +384,9 @@ func (index *SpecIndex) scanOperationParams(params []*yaml.Node, pathItemNode *y
// if this is a duplicate, add an error and ignore it
if index.paramOpRefs[pathItemNode.Value][method][paramRefName] != nil {
path := fmt.Sprintf("$.paths.%s.%s.parameters[%d]", pathItemNode.Value, method, i)
path := fmt.Sprintf("$.paths['%s'].%s.parameters[%d]", pathItemNode.Value, method, i)
if method == "top" {
path = fmt.Sprintf("$.paths.%s.parameters[%d]", pathItemNode.Value, i)
path = fmt.Sprintf("$.paths['%s'].parameters[%d]", pathItemNode.Value, i)
}
index.operationParamErrors = append(index.operationParamErrors, &IndexingError{
@@ -408,9 +408,9 @@ func (index *SpecIndex) scanOperationParams(params []*yaml.Node, pathItemNode *y
// param is inline.
_, vn := utils.FindKeyNode("name", param.Content)
path := fmt.Sprintf("$.paths.%s.%s.parameters[%d]", pathItemNode.Value, method, i)
path := fmt.Sprintf("$.paths['%s'].%s.parameters[%d]", pathItemNode.Value, method, i)
if method == "top" {
path = fmt.Sprintf("$.paths.%s.parameters[%d]", pathItemNode.Value, i)
path = fmt.Sprintf("$.paths['%s'].parameters[%d]", pathItemNode.Value, i)
}
if vn == nil {
@@ -452,9 +452,9 @@ func (index *SpecIndex) scanOperationParams(params []*yaml.Node, pathItemNode *y
if currentIn != nil && checkIn != nil && currentIn.Value == checkIn.Value {
path := fmt.Sprintf("$.paths.%s.%s.parameters[%d]", pathItemNode.Value, method, i)
path := fmt.Sprintf("$.paths['%s'].%s.parameters[%d]", pathItemNode.Value, method, i)
if method == "top" {
path = fmt.Sprintf("$.paths.%s.parameters[%d]", pathItemNode.Value, i)
path = fmt.Sprintf("$.paths['%s'].parameters[%d]", pathItemNode.Value, i)
}
index.operationParamErrors = append(index.operationParamErrors, &IndexingError{

View File

@@ -1,17 +1,18 @@
package utils
import (
"context"
"encoding/json"
"fmt"
"github.com/vmware-labs/yaml-jsonpath/pkg/yamlpath"
"gopkg.in/yaml.v3"
"net/http"
"net/url"
"regexp"
"sort"
"strconv"
"strings"
"github.com/vmware-labs/yaml-jsonpath/pkg/yamlpath"
"gopkg.in/yaml.v3"
"time"
)
type Case int8
@@ -106,11 +107,34 @@ func FindNodesWithoutDeserializing(node *yaml.Node, jsonPath string) ([]*yaml.No
jsonPath = FixContext(jsonPath)
path, err := yamlpath.NewPath(jsonPath)
if err != nil {
return nil, err
}
results, _ := path.Find(node)
// this can spin out, to lets gatekeep it.
done := make(chan bool)
eChan := make(chan error)
var results []*yaml.Node
timeout, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
go func(d chan bool, e chan error) {
var er error
results, er = path.Find(node)
if er != nil {
e <- er
}
done <- true
}(done, eChan)
select {
case <-done:
return results, nil
case er := <-eChan:
return nil, er
case <-timeout.Done():
return nil, fmt.Errorf("node lookup timeout exceeded")
}
}
// ConvertInterfaceIntoStringMap will convert an unknown input into a string map.