added parent node to model for references.

Signed-off-by: quobix <dave@quobix.com>
This commit is contained in:
quobix
2024-02-07 17:38:52 -05:00
parent 7afb1e9d11
commit 0eea21b150
5 changed files with 50 additions and 17 deletions

View File

@@ -1,10 +1,14 @@
package index package index
import "strings" import (
"gopkg.in/yaml.v3"
"strings"
)
// CircularReferenceResult contains a circular reference found when traversing the graph. // CircularReferenceResult contains a circular reference found when traversing the graph.
type CircularReferenceResult struct { type CircularReferenceResult struct {
Journey []*Reference Journey []*Reference
ParentNode *yaml.Node
Start *Reference Start *Reference
LoopIndex int LoopIndex int
LoopPoint *Reference LoopPoint *Reference

View File

@@ -57,6 +57,7 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
_, jsonPath = utils.ConvertComponentIdIntoFriendlyPathSearch(definitionPath) _, jsonPath = utils.ConvertComponentIdIntoFriendlyPathSearch(definitionPath)
} }
ref := &Reference{ ref := &Reference{
ParentNode: parent,
FullDefinition: fullDefinitionPath, FullDefinition: fullDefinitionPath,
Definition: definitionPath, Definition: definitionPath,
Node: node.Content[i+1], Node: node.Content[i+1],
@@ -132,6 +133,7 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
_, jsonPath = utils.ConvertComponentIdIntoFriendlyPathSearch(definitionPath) _, jsonPath = utils.ConvertComponentIdIntoFriendlyPathSearch(definitionPath)
} }
ref := &Reference{ ref := &Reference{
ParentNode: parent,
FullDefinition: fullDefinitionPath, FullDefinition: fullDefinitionPath,
Definition: definitionPath, Definition: definitionPath,
Node: prop, Node: prop,
@@ -178,6 +180,7 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
} }
ref := &Reference{ ref := &Reference{
ParentNode: parent,
FullDefinition: fullDefinitionPath, FullDefinition: fullDefinitionPath,
Definition: definitionPath, Definition: definitionPath,
Node: element, Node: element,
@@ -337,6 +340,7 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
_, p := utils.ConvertComponentIdIntoFriendlyPathSearch(componentName) _, p := utils.ConvertComponentIdIntoFriendlyPathSearch(componentName)
ref := &Reference{ ref := &Reference{
ParentNode: parent,
FullDefinition: fullDefinitionPath, FullDefinition: fullDefinitionPath,
Definition: componentName, Definition: componentName,
Name: name, Name: name,
@@ -364,6 +368,7 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
if len(node.Content) > 2 { if len(node.Content) > 2 {
copiedNode := *node copiedNode := *node
copied := Reference{ copied := Reference{
ParentNode: parent,
FullDefinition: fullDefinitionPath, FullDefinition: fullDefinitionPath,
Definition: ref.Definition, Definition: ref.Definition,
Name: ref.Name, Name: ref.Name,
@@ -434,6 +439,7 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
} }
ref := &DescriptionReference{ ref := &DescriptionReference{
ParentNode: parent,
Content: node.Content[i+1].Value, Content: node.Content[i+1].Value,
Path: jsonPath, Path: jsonPath,
Node: node.Content[i+1], Node: node.Content[i+1],
@@ -455,6 +461,7 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
b = node.Content[i+1] b = node.Content[i+1]
} }
ref := &DescriptionReference{ ref := &DescriptionReference{
ParentNode: parent,
Content: b.Value, Content: b.Value,
Path: jsonPath, Path: jsonPath,
Node: b, Node: b,
@@ -528,11 +535,11 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
if enumKeyValueNode != nil { if enumKeyValueNode != nil {
ref := &EnumReference{ ref := &EnumReference{
ParentNode: parent,
Path: jsonPath, Path: jsonPath,
Node: node.Content[i+1], Node: node.Content[i+1],
Type: enumKeyValueNode, Type: enumKeyValueNode,
SchemaNode: node, SchemaNode: node,
ParentNode: parent,
} }
index.allEnums = append(index.allEnums, ref) index.allEnums = append(index.allEnums, ref)

View File

@@ -67,6 +67,16 @@ func FindComponent(root *yaml.Node, componentId, absoluteFilePath string, index
resNode := res[0] resNode := res[0]
fullDef := fmt.Sprintf("%s%s", absoluteFilePath, componentId) fullDef := fmt.Sprintf("%s%s", absoluteFilePath, componentId)
// extract properties // extract properties
// check if we have already seen this reference and there is a parent, use it
var parentNode *yaml.Node
if index.allRefs[componentId] != nil {
parentNode = index.allRefs[componentId].ParentNode
}
if index.allRefs[fullDef] != nil {
parentNode = index.allRefs[fullDef].ParentNode
}
ref := &Reference{ ref := &Reference{
FullDefinition: fullDef, FullDefinition: fullDef,
Definition: componentId, Definition: componentId,
@@ -74,6 +84,7 @@ func FindComponent(root *yaml.Node, componentId, absoluteFilePath string, index
Node: resNode, Node: resNode,
Path: friendlySearch, Path: friendlySearch,
RemoteLocation: absoluteFilePath, RemoteLocation: absoluteFilePath,
ParentNode: parentNode,
Index: index, Index: index,
RequiredRefProperties: extractDefinitionRequiredRefProperties(resNode, map[string][]string{}, fullDef, index), RequiredRefProperties: extractDefinitionRequiredRefProperties(resNode, map[string][]string{}, fullDef, index),
} }
@@ -166,7 +177,13 @@ func (index *SpecIndex) lookupRolodex(uri []string) *Reference {
parsedDocument = parsedDocument.Content[0] parsedDocument = parsedDocument.Content[0]
} }
var parentNode *yaml.Node
if index.allRefs[absoluteFileLocation] != nil {
parentNode = index.allRefs[absoluteFileLocation].ParentNode
}
foundRef = &Reference{ foundRef = &Reference{
ParentNode: parentNode,
FullDefinition: absoluteFileLocation, FullDefinition: absoluteFileLocation,
Definition: fileName, Definition: fileName,
Name: fileName, Name: fileName,

View File

@@ -333,6 +333,7 @@ type DescriptionReference struct {
Content string Content string
Path string Path string
Node *yaml.Node Node *yaml.Node
ParentNode *yaml.Node
IsSummary bool IsSummary bool
} }

View File

@@ -201,7 +201,7 @@ func (resolver *Resolver) Resolve() []*ResolvingError {
if !resolver.circChecked { if !resolver.circChecked {
resolver.resolvingErrors = append(resolver.resolvingErrors, &ResolvingError{ resolver.resolvingErrors = append(resolver.resolvingErrors, &ResolvingError{
ErrorRef: fmt.Errorf("infinite circular reference detected: %s", circRef.Start.Definition), ErrorRef: fmt.Errorf("infinite circular reference detected: %s", circRef.Start.Definition),
Node: circRef.LoopPoint.Node, Node: circRef.ParentNode,
Path: circRef.GenerateJourneyPath(), Path: circRef.GenerateJourneyPath(),
}) })
} }
@@ -221,7 +221,7 @@ func (resolver *Resolver) CheckForCircularReferences() []*ResolvingError {
if !resolver.circChecked { if !resolver.circChecked {
resolver.resolvingErrors = append(resolver.resolvingErrors, &ResolvingError{ resolver.resolvingErrors = append(resolver.resolvingErrors, &ResolvingError{
ErrorRef: fmt.Errorf("infinite circular reference detected: %s", circRef.Start.Name), ErrorRef: fmt.Errorf("infinite circular reference detected: %s", circRef.Start.Name),
Node: circRef.LoopPoint.Node, Node: circRef.ParentNode,
Path: circRef.GenerateJourneyPath(), Path: circRef.GenerateJourneyPath(),
CircularReference: circRef, CircularReference: circRef,
}) })
@@ -361,6 +361,7 @@ func (resolver *Resolver) VisitReference(ref *Reference, seen map[string]bool, j
isArray = true isArray = true
} }
circRef = &CircularReferenceResult{ circRef = &CircularReferenceResult{
ParentNode: foundDup.ParentNode,
Journey: loop, Journey: loop,
Start: foundDup, Start: foundDup,
LoopIndex: i, LoopIndex: i,
@@ -641,7 +642,7 @@ func (resolver *Resolver) extractRelatives(ref *Reference, node, parent *yaml.No
// if this is a polymorphic link, we want to follow it and see if it becomes circular // if this is a polymorphic link, we want to follow it and see if it becomes circular
if i+1 <= len(node.Content) && utils.IsNodeMap(node.Content[i+1]) { // check for nested items if i+1 <= len(node.Content) && utils.IsNodeMap(node.Content[i+1]) { // check for nested items
// check if items is present, to indicate an array // check if items is present, to indicate an array
if _, v := utils.FindKeyNodeTop("items", node.Content[i+1].Content); v != nil { if k, v := utils.FindKeyNodeTop("items", node.Content[i+1].Content); v != nil {
if utils.IsNodeMap(v) { if utils.IsNodeMap(v) {
if d, _, l := utils.IsNodeRefValue(v); d { if d, _, l := utils.IsNodeRefValue(v); d {
@@ -662,6 +663,7 @@ func (resolver *Resolver) extractRelatives(ref *Reference, node, parent *yaml.No
} else { } else {
loop := append(journey, mappedRefs) loop := append(journey, mappedRefs)
circRef := &CircularReferenceResult{ circRef := &CircularReferenceResult{
ParentNode: k,
Journey: loop, Journey: loop,
Start: mappedRefs, Start: mappedRefs,
LoopIndex: i, LoopIndex: i,
@@ -706,6 +708,7 @@ func (resolver *Resolver) extractRelatives(ref *Reference, node, parent *yaml.No
} else { } else {
loop := append(journey, mappedRefs) loop := append(journey, mappedRefs)
circRef := &CircularReferenceResult{ circRef := &CircularReferenceResult{
ParentNode: node.Content[i],
Journey: loop, Journey: loop,
Start: mappedRefs, Start: mappedRefs,
LoopIndex: i, LoopIndex: i,
@@ -751,6 +754,7 @@ func (resolver *Resolver) extractRelatives(ref *Reference, node, parent *yaml.No
loop := append(journey, mappedRefs) loop := append(journey, mappedRefs)
circRef := &CircularReferenceResult{ circRef := &CircularReferenceResult{
ParentNode: node.Content[i],
Journey: loop, Journey: loop,
Start: mappedRefs, Start: mappedRefs,
LoopIndex: i, LoopIndex: i,