mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-06 04:20:11 +00:00
added parent node to model for references.
Signed-off-by: quobix <dave@quobix.com>
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
package index
|
||||
|
||||
import "strings"
|
||||
import (
|
||||
"gopkg.in/yaml.v3"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// CircularReferenceResult contains a circular reference found when traversing the graph.
|
||||
type CircularReferenceResult struct {
|
||||
Journey []*Reference
|
||||
ParentNode *yaml.Node
|
||||
Start *Reference
|
||||
LoopIndex int
|
||||
LoopPoint *Reference
|
||||
|
||||
@@ -57,6 +57,7 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
|
||||
_, jsonPath = utils.ConvertComponentIdIntoFriendlyPathSearch(definitionPath)
|
||||
}
|
||||
ref := &Reference{
|
||||
ParentNode: parent,
|
||||
FullDefinition: fullDefinitionPath,
|
||||
Definition: definitionPath,
|
||||
Node: node.Content[i+1],
|
||||
@@ -132,6 +133,7 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
|
||||
_, jsonPath = utils.ConvertComponentIdIntoFriendlyPathSearch(definitionPath)
|
||||
}
|
||||
ref := &Reference{
|
||||
ParentNode: parent,
|
||||
FullDefinition: fullDefinitionPath,
|
||||
Definition: definitionPath,
|
||||
Node: prop,
|
||||
@@ -178,6 +180,7 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
|
||||
}
|
||||
|
||||
ref := &Reference{
|
||||
ParentNode: parent,
|
||||
FullDefinition: fullDefinitionPath,
|
||||
Definition: definitionPath,
|
||||
Node: element,
|
||||
@@ -337,6 +340,7 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
|
||||
_, p := utils.ConvertComponentIdIntoFriendlyPathSearch(componentName)
|
||||
|
||||
ref := &Reference{
|
||||
ParentNode: parent,
|
||||
FullDefinition: fullDefinitionPath,
|
||||
Definition: componentName,
|
||||
Name: name,
|
||||
@@ -364,6 +368,7 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
|
||||
if len(node.Content) > 2 {
|
||||
copiedNode := *node
|
||||
copied := Reference{
|
||||
ParentNode: parent,
|
||||
FullDefinition: fullDefinitionPath,
|
||||
Definition: ref.Definition,
|
||||
Name: ref.Name,
|
||||
@@ -434,6 +439,7 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
|
||||
}
|
||||
|
||||
ref := &DescriptionReference{
|
||||
ParentNode: parent,
|
||||
Content: node.Content[i+1].Value,
|
||||
Path: jsonPath,
|
||||
Node: node.Content[i+1],
|
||||
@@ -455,6 +461,7 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
|
||||
b = node.Content[i+1]
|
||||
}
|
||||
ref := &DescriptionReference{
|
||||
ParentNode: parent,
|
||||
Content: b.Value,
|
||||
Path: jsonPath,
|
||||
Node: b,
|
||||
@@ -528,11 +535,11 @@ func (index *SpecIndex) ExtractRefs(node, parent *yaml.Node, seenPath []string,
|
||||
|
||||
if enumKeyValueNode != nil {
|
||||
ref := &EnumReference{
|
||||
ParentNode: parent,
|
||||
Path: jsonPath,
|
||||
Node: node.Content[i+1],
|
||||
Type: enumKeyValueNode,
|
||||
SchemaNode: node,
|
||||
ParentNode: parent,
|
||||
}
|
||||
|
||||
index.allEnums = append(index.allEnums, ref)
|
||||
|
||||
@@ -67,6 +67,16 @@ func FindComponent(root *yaml.Node, componentId, absoluteFilePath string, index
|
||||
resNode := res[0]
|
||||
fullDef := fmt.Sprintf("%s%s", absoluteFilePath, componentId)
|
||||
// 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{
|
||||
FullDefinition: fullDef,
|
||||
Definition: componentId,
|
||||
@@ -74,6 +84,7 @@ func FindComponent(root *yaml.Node, componentId, absoluteFilePath string, index
|
||||
Node: resNode,
|
||||
Path: friendlySearch,
|
||||
RemoteLocation: absoluteFilePath,
|
||||
ParentNode: parentNode,
|
||||
Index: index,
|
||||
RequiredRefProperties: extractDefinitionRequiredRefProperties(resNode, map[string][]string{}, fullDef, index),
|
||||
}
|
||||
@@ -166,7 +177,13 @@ func (index *SpecIndex) lookupRolodex(uri []string) *Reference {
|
||||
parsedDocument = parsedDocument.Content[0]
|
||||
}
|
||||
|
||||
var parentNode *yaml.Node
|
||||
if index.allRefs[absoluteFileLocation] != nil {
|
||||
parentNode = index.allRefs[absoluteFileLocation].ParentNode
|
||||
}
|
||||
|
||||
foundRef = &Reference{
|
||||
ParentNode: parentNode,
|
||||
FullDefinition: absoluteFileLocation,
|
||||
Definition: fileName,
|
||||
Name: fileName,
|
||||
|
||||
@@ -333,6 +333,7 @@ type DescriptionReference struct {
|
||||
Content string
|
||||
Path string
|
||||
Node *yaml.Node
|
||||
ParentNode *yaml.Node
|
||||
IsSummary bool
|
||||
}
|
||||
|
||||
|
||||
@@ -201,7 +201,7 @@ func (resolver *Resolver) Resolve() []*ResolvingError {
|
||||
if !resolver.circChecked {
|
||||
resolver.resolvingErrors = append(resolver.resolvingErrors, &ResolvingError{
|
||||
ErrorRef: fmt.Errorf("infinite circular reference detected: %s", circRef.Start.Definition),
|
||||
Node: circRef.LoopPoint.Node,
|
||||
Node: circRef.ParentNode,
|
||||
Path: circRef.GenerateJourneyPath(),
|
||||
})
|
||||
}
|
||||
@@ -221,7 +221,7 @@ func (resolver *Resolver) CheckForCircularReferences() []*ResolvingError {
|
||||
if !resolver.circChecked {
|
||||
resolver.resolvingErrors = append(resolver.resolvingErrors, &ResolvingError{
|
||||
ErrorRef: fmt.Errorf("infinite circular reference detected: %s", circRef.Start.Name),
|
||||
Node: circRef.LoopPoint.Node,
|
||||
Node: circRef.ParentNode,
|
||||
Path: circRef.GenerateJourneyPath(),
|
||||
CircularReference: circRef,
|
||||
})
|
||||
@@ -361,6 +361,7 @@ func (resolver *Resolver) VisitReference(ref *Reference, seen map[string]bool, j
|
||||
isArray = true
|
||||
}
|
||||
circRef = &CircularReferenceResult{
|
||||
ParentNode: foundDup.ParentNode,
|
||||
Journey: loop,
|
||||
Start: foundDup,
|
||||
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 i+1 <= len(node.Content) && utils.IsNodeMap(node.Content[i+1]) { // check for nested items
|
||||
// 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 d, _, l := utils.IsNodeRefValue(v); d {
|
||||
|
||||
@@ -662,6 +663,7 @@ func (resolver *Resolver) extractRelatives(ref *Reference, node, parent *yaml.No
|
||||
} else {
|
||||
loop := append(journey, mappedRefs)
|
||||
circRef := &CircularReferenceResult{
|
||||
ParentNode: k,
|
||||
Journey: loop,
|
||||
Start: mappedRefs,
|
||||
LoopIndex: i,
|
||||
@@ -706,6 +708,7 @@ func (resolver *Resolver) extractRelatives(ref *Reference, node, parent *yaml.No
|
||||
} else {
|
||||
loop := append(journey, mappedRefs)
|
||||
circRef := &CircularReferenceResult{
|
||||
ParentNode: node.Content[i],
|
||||
Journey: loop,
|
||||
Start: mappedRefs,
|
||||
LoopIndex: i,
|
||||
@@ -751,6 +754,7 @@ func (resolver *Resolver) extractRelatives(ref *Reference, node, parent *yaml.No
|
||||
loop := append(journey, mappedRefs)
|
||||
|
||||
circRef := &CircularReferenceResult{
|
||||
ParentNode: node.Content[i],
|
||||
Journey: loop,
|
||||
Start: mappedRefs,
|
||||
LoopIndex: i,
|
||||
|
||||
Reference in New Issue
Block a user