Skip to content

Commit a5d6833

Browse files
SebTardifdaveshanley
authored andcommitted
Fix goroutine leak: add missing return after doneChan send in LocateSchemaPropertyNodeByJSONPath
Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>
1 parent 38c5e03 commit a5d6833

2 files changed

Lines changed: 12 additions & 5 deletions

File tree

schema_validation/locate_schema_property.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,23 @@ import (
1313
// LocateSchemaPropertyNodeByJSONPath will locate a schema property node by a JSONPath. It converts something like
1414
// #/components/schemas/MySchema/properties/MyProperty to something like $.components.schemas.MySchema.properties.MyProperty
1515
func LocateSchemaPropertyNodeByJSONPath(doc *yaml.Node, JSONPath string) *yaml.Node {
16+
_, path := utils.ConvertComponentIdIntoFriendlyPathSearch(JSONPath)
17+
return locateSchemaPropertyNode(doc, path)
18+
}
19+
20+
func locateSchemaPropertyNode(doc *yaml.Node, path string) *yaml.Node {
21+
if path == "" {
22+
return nil
23+
}
1624
var locatedNode *yaml.Node
1725
doneChan := make(chan bool)
1826
locatedNodeChan := make(chan *yaml.Node)
1927
go func() {
2028
defer func() {
2129
if err := recover(); err != nil {
22-
// can't search path, too crazy.
2330
doneChan <- true
2431
}
2532
}()
26-
_, path := utils.ConvertComponentIdIntoFriendlyPathSearch(JSONPath)
27-
if path == "" {
28-
doneChan <- true
29-
}
3033
jsonPath, _ := jsonpath.NewPath(path, config.WithLazyContextTracking())
3134
locatedNodes := jsonPath.Query(doc)
3235
if len(locatedNodes) > 0 {

schema_validation/locate_schema_property_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ import (
1212
func TestLocateSchemaPropertyNodeByJSONPath_BadNode(t *testing.T) {
1313
assert.Nil(t, LocateSchemaPropertyNodeByJSONPath(nil, ""))
1414
}
15+
16+
func TestLocateSchemaPropertyNode_EmptyPath(t *testing.T) {
17+
assert.Nil(t, locateSchemaPropertyNode(nil, ""))
18+
}

0 commit comments

Comments
 (0)