@@ -18,7 +18,6 @@ import (
1818 "github.com/pb33f/libopenapi-validator/config"
1919 "github.com/pb33f/libopenapi-validator/errors"
2020 "github.com/pb33f/libopenapi-validator/helpers"
21- "github.com/pb33f/libopenapi-validator/radix"
2221)
2322
2423// FindPath will find the path in the document that matches the request path. If a successful match was found, then
@@ -37,26 +36,13 @@ func FindPath(request *http.Request, document *v3.Document, options *config.Vali
3736 stripped := StripRequestPath (request , document )
3837
3938 // Fast path: try radix tree first (O(k) where k = path depth)
40- tree := pathLookupFrom (options , document )
41- if tree != nil {
42- if pathItem , matchedPath , found := tree .Lookup (stripped ); found {
43- // Verify the path has the requested method
39+ // If no path lookup is provided, we will fall back to regex-based matching.
40+ if options != nil && options .PathTree != nil {
41+ if pathItem , matchedPath , found := options .PathTree .Lookup (stripped ); found {
4442 if pathHasMethod (pathItem , request .Method ) {
4543 return pathItem , nil , matchedPath
4644 }
47- // Path found but method doesn't exist
48- validationErrors := []* errors.ValidationError {{
49- ValidationType : helpers .ParameterValidationPath ,
50- ValidationSubType : "missingOperation" ,
51- Message : fmt .Sprintf ("%s Path '%s' not found" , request .Method , request .URL .Path ),
52- Reason : fmt .Sprintf ("The %s method for that path does not exist in the specification" ,
53- request .Method ),
54- SpecLine : - 1 ,
55- SpecCol : - 1 ,
56- HowToFix : errors .HowToFixPath ,
57- }}
58- errors .PopulateValidationErrors (validationErrors , request , matchedPath )
59- return pathItem , validationErrors , matchedPath
45+ return pathItem , missingOperationError (request , matchedPath ), matchedPath
6046 }
6147 }
6248
@@ -128,18 +114,7 @@ func FindPath(request *http.Request, document *v3.Document, options *config.Vali
128114 }
129115
130116 // path matches exist but none have the required method
131- validationErrors := []* errors.ValidationError {{
132- ValidationType : helpers .PathValidation ,
133- ValidationSubType : helpers .ValidationMissingOperation ,
134- Message : fmt .Sprintf ("%s Path '%s' not found" , request .Method , request .URL .Path ),
135- Reason : fmt .Sprintf ("The %s method for that path does not exist in the specification" ,
136- request .Method ),
137- SpecLine : - 1 ,
138- SpecCol : - 1 ,
139- HowToFix : errors .HowToFixPath ,
140- }}
141- errors .PopulateValidationErrors (validationErrors , request , bestOverall .path )
142- return bestOverall .pathItem , validationErrors , bestOverall .path
117+ return bestOverall .pathItem , missingOperationError (request , bestOverall .path ), bestOverall .path
143118}
144119
145120// normalizePathForMatching removes the fragment from a path template unless
@@ -257,13 +232,18 @@ func comparePaths(mapped, requested, basePaths []string, regexCache config.Regex
257232 return checkPathAgainstBase (l , r , basePaths )
258233}
259234
260- // pathLookupFrom returns the PathLookup from options, or builds one from the document.
261- func pathLookupFrom (options * config.ValidationOptions , document * v3.Document ) radix.PathLookup {
262- if options != nil && options .PathLookup != nil {
263- return options .PathLookup
264- }
265- if document != nil && document .Paths != nil {
266- return radix .BuildPathTree (document )
267- }
268- return nil
235+ // missingOperationError returns a validation error for when a path was found but the HTTP method doesn't exist.
236+ func missingOperationError (request * http.Request , matchedPath string ) []* errors.ValidationError {
237+ validationErrors := []* errors.ValidationError {{
238+ ValidationType : helpers .PathValidation ,
239+ ValidationSubType : helpers .ValidationMissingOperation ,
240+ Message : fmt .Sprintf ("%s Path '%s' not found" , request .Method , request .URL .Path ),
241+ Reason : fmt .Sprintf ("The %s method for that path does not exist in the specification" ,
242+ request .Method ),
243+ SpecLine : - 1 ,
244+ SpecCol : - 1 ,
245+ HowToFix : errors .HowToFixPath ,
246+ }}
247+ errors .PopulateValidationErrors (validationErrors , request , matchedPath )
248+ return validationErrors
269249}
0 commit comments