Working on merging pathObjs

This commit is contained in:
Andrey Pokhilko
2022-02-22 10:38:34 +03:00
parent 4dbdd8d0c1
commit 6c04d1d7c4
2 changed files with 66 additions and 1 deletions

View File

@@ -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 {

View File

@@ -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
}