diff --git a/agent/pkg/oas/specgen.go b/agent/pkg/oas/specgen.go index ec542595b..60f9cf75f 100644 --- a/agent/pkg/oas/specgen.go +++ b/agent/pkg/oas/specgen.go @@ -188,7 +188,7 @@ func (g *SpecGen) handlePathObj(entryWithSource *EntryWithSource) (string, error } if entry.Request.Method == "OPTIONS" { - logger.Log.Debugf("Dropped traffic entry due to its method: %s", urlParsed.Path) + logger.Log.Debugf("Dropped traffic entry due to its method: %s %s", entry.Request.Method, urlParsed.Path) return "", nil } diff --git a/agent/pkg/oas/tree.go b/agent/pkg/oas/tree.go index 5faa2a373..36bead6e0 100644 --- a/agent/pkg/oas/tree.go +++ b/agent/pkg/oas/tree.go @@ -214,7 +214,48 @@ func (n *Node) searchInConstants(pathChunk string) *Node { } func (n *Node) compact() { - // TODO + // TODO: introduce and leverage "dirty" flag? + var param *Node + // find the param + for _, subnode := range n.children { + if subnode.constant != nil { + continue + } + + param = subnode + } + + if param != nil { + // take its regex + pRegex := param.pathParam.Schema.Pattern + if pRegex == nil { + return + } + + newChildren := make([]*Node, 0) + + // compact the constants via regex + for _, subnode := range n.children { + if subnode.constant != nil { + if pRegex.Match([]byte(*subnode.constant)) { + param.merge(subnode) + continue + } + } + newChildren = append(newChildren, subnode) + } + + if len(n.children) != len(newChildren) { + logger.Log.Debugf("Shrinking children from %d to %d", len(n.children), len(newChildren)) + n.children = newChildren + n.compact() + } + } + + // recurse into next tree level + for _, subnode := range n.children { + subnode.compact() + } } func (n *Node) listPaths() *openapi.Paths { @@ -303,3 +344,18 @@ func (n *Node) countParentParams() int { } return res } + +func (n *Node) merge(other *Node) { +outer: + for _, oChild := range other.children { + for _, nChild := range n.children { + matchedConst := oChild.constant != nil && oChild.constant == nChild.constant + matchedParam := oChild.constant == nil && nChild.constant == nil + if matchedConst || matchedParam { + nChild.merge(oChild) + continue outer + } + } + n.children = append(n.children, oChild) + } +}