Skip to content

Commit 2aa6245

Browse files
committed
Catching panic in ref location when ref is crazy.
Signed-off-by: Dave Shanley <[email protected]>
1 parent 46638b9 commit 2aa6245

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

paths/paths_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,3 +577,27 @@ paths:
577577

578578
assert.Len(t, errs, 1)
579579
}
580+
581+
func TestNewValidator_PostMatch_Error(t *testing.T) {
582+
583+
spec := `openapi: 3.1.0
584+
paths:
585+
/pizza/{cakes}:
586+
post:
587+
operationId: locateBurger
588+
parameters:
589+
- name: cakes
590+
in: path
591+
required: true
592+
schema:
593+
type: string`
594+
595+
doc, _ := libopenapi.NewDocument([]byte(spec))
596+
m, _ := doc.BuildV3Model()
597+
598+
request, _ := http.NewRequest(http.MethodPost, "https://things.com/pizza/1234", nil)
599+
600+
_, errs, _ := FindPath(request, &m.Model)
601+
602+
assert.Len(t, errs, 1)
603+
}

schema_validation/locate_schema_property.go

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,26 @@ import (
1212
// LocateSchemaPropertyNodeByJSONPath will locate a schema property node by a JSONPath. It converts something like
1313
// #/components/schemas/MySchema/properties/MyProperty to something like $.components.schemas.MySchema.properties.MyProperty
1414
func LocateSchemaPropertyNodeByJSONPath(doc *yaml.Node, JSONPath string) *yaml.Node {
15-
// first convert the path to something we can use as a lookup, remove the leading slash
16-
_, path := utils.ConvertComponentIdIntoFriendlyPathSearch(JSONPath)
17-
if path == "" {
18-
return nil
19-
}
20-
yamlPath, _ := yamlpath.NewPath(path)
21-
locatedNodes, _ := yamlPath.Find(doc)
22-
if len(locatedNodes) > 0 {
23-
return locatedNodes[0]
24-
}
25-
return nil
15+
var locatedNode *yaml.Node
16+
doneChan := make(chan bool)
17+
go func() {
18+
defer func() {
19+
if err := recover(); err != nil {
20+
// can't search path, too crazy.
21+
doneChan <- true
22+
}
23+
}()
24+
_, path := utils.ConvertComponentIdIntoFriendlyPathSearch(JSONPath)
25+
if path == "" {
26+
doneChan <- true
27+
}
28+
yamlPath, _ := yamlpath.NewPath(path)
29+
locatedNodes, _ := yamlPath.Find(doc)
30+
if len(locatedNodes) > 0 {
31+
locatedNode = locatedNodes[0]
32+
}
33+
doneChan <- true
34+
}()
35+
<-doneChan
36+
return locatedNode
2637
}

schema_validation/validate_document.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func ValidateOpenAPIDocument(doc libopenapi.Document) (bool, []*errors.Validatio
4242
if er.Error != "" {
4343

4444
// locate the violated property in the schema
45+
4546
located := LocateSchemaPropertyNodeByJSONPath(info.RootNode.Content[0], er.KeywordLocation)
4647
if located == nil {
4748
// try again with the instance location

0 commit comments

Comments
 (0)