diff --git a/agent/pkg/oas/tree.go b/agent/pkg/oas/tree.go index bcee2f2d1..272872119 100644 --- a/agent/pkg/oas/tree.go +++ b/agent/pkg/oas/tree.go @@ -188,6 +188,9 @@ func (n *Node) searchInParams(paramObj *openapi.ParameterObj, chunk string, chun // TODO: and not in exceptions if subnode.pathParam.Schema.Pattern.Match([]byte(chunk)) { return subnode + } else if chunkIsGibberish { + // TODO: what to do if gibberish chunk does not match the pattern and not in exceptions? + return nil } else { return nil } @@ -344,8 +347,17 @@ func (n *Node) countParentParams() int { } func (n *Node) merge(other *Node) { - // TODO: merge operations, remember historical operationIDs + if n.constant == nil && other.constant == nil { + // make sure the params will match by name later in merge + other.pathParam.Name = n.pathParam.Name + } + + if n.pathObj != nil && other.pathObj != nil { + mergePathObj(n.pathObj, other.pathObj) + } + // TODO: if n is param and other is constant, could have added constant as an example + outer: for _, oChild := range other.children { for _, nChild := range n.children { diff --git a/agent/pkg/oas/utils.go b/agent/pkg/oas/utils.go index 59fb0849b..5db830fcb 100644 --- a/agent/pkg/oas/utils.go +++ b/agent/pkg/oas/utils.go @@ -474,3 +474,56 @@ func intersectSliceWithMap(required []string, names map[string]struct{}) []strin } return required } + +func mergePathObj(po *openapi.PathObj, other *openapi.PathObj) { + // merge parameters + mergePathParams(&po.Parameters, &other.Parameters) + + // merge ops + mergeOps(&po.Get, &other.Get) + mergeOps(&po.Put, &other.Put) + mergeOps(&po.Options, &other.Options) + mergeOps(&po.Patch, &other.Patch) + mergeOps(&po.Delete, &other.Delete) + mergeOps(&po.Head, &other.Head) + mergeOps(&po.Trace, &other.Trace) + mergeOps(&po.Post, &other.Post) +} + +func mergePathParams(params **openapi.ParameterList, other **openapi.ParameterList) { + if *other == nil { + return + } + + if *params == nil { + *params = new(openapi.ParameterList) + } + +outer: + for _, o := range **other { + oParam, err := o.ResolveParameter(paramResolver) + if err != nil { + logger.Log.Warningf("Failed to resolve reference: %s", err) + continue + } + + for _, p := range **params { + param, err := p.ResolveParameter(paramResolver) + if err != nil { + logger.Log.Warningf("Failed to resolve reference: %s", err) + continue + } + + if param.In == oParam.In && param.Name == oParam.Name { + continue outer + } + } + + **params = append(**params, oParam) + } + +} + +func mergeOps(op **openapi.Operation, other **openapi.Operation) { + // TODO: merge operations, remember historical operationIDs +}