| 1 |
// Copyright 2023-2025 Princess B33f Heavy Industries / Dave Shanley |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
package paths |
| 5 |
|
| 6 |
import ( |
| 7 |
"net/http" |
| 8 |
"strings" |
| 9 |
|
| 10 |
v3 "github.com/pb33f/libopenapi/datamodel/high/v3" |
| 11 |
) |
| 12 |
|
| 13 |
// pathCandidate represents a potential path match with metadata for selection. |
| 14 |
type pathCandidate struct { |
| 15 |
pathItem *v3.PathItem |
| 16 |
path string |
| 17 |
score int |
| 18 |
hasMethod bool |
| 19 |
} |
| 20 |
|
| 21 |
// computeSpecificityScore calculates how specific a path template is. |
| 22 |
// literal segments score higher than parameterized segments, ensuring |
| 23 |
// "/pets/mine" is preferred over "/pets/{id}" per OpenAPI spec. |
| 24 |
// |
| 25 |
// scoring: |
| 26 |
// - literal segment: 1000 points |
| 27 |
// - parameter segment: 1 point |
| 28 |
// |
| 29 |
// this weighting ensures any path with more literal segments always wins, |
| 30 |
// regardless of parameter positions. |
| 31 |
func computeSpecificityScore(pathTemplate string) int { |
| 32 |
segments := strings.Split(pathTemplate, "/") |
| 33 |
score := 0 |
| 34 |
|
| 35 |
for _, seg := range segments { |
| 36 |
if seg == "" { |
| 37 |
continue |
| 38 |
} |
| 39 |
if isParameterSegment(seg) { |
| 40 |
score += 1 |
| 41 |
} else { |
| 42 |
score += 1000 |
| 43 |
} |
| 44 |
} |
| 45 |
return score |
| 46 |
} |
| 47 |
|
| 48 |
// isParameterSegment returns true if the segment contains a path parameter. |
| 49 |
// handles standard {param}, label {.param}, and exploded {param*} formats. |
| 50 |
func isParameterSegment(seg string) bool { |
| 51 |
return strings.Contains(seg, "{") && strings.Contains(seg, "}") |
| 52 |
} |
| 53 |
|
| 54 |
// pathHasMethod checks if the PathItem has an operation for the given HTTP method. |
| 55 |
func pathHasMethod(pathItem *v3.PathItem, method string) bool { |
| 56 |
switch method { |
| 57 |
case http.MethodGet: |
| 58 |
return pathItem.Get != nil |
| 59 |
case http.MethodPost: |
| 60 |
return pathItem.Post != nil |
| 61 |
case http.MethodPut: |
| 62 |
return pathItem.Put != nil |
| 63 |
case http.MethodDelete: |
| 64 |
return pathItem.Delete != nil |
| 65 |
case http.MethodOptions: |
| 66 |
return pathItem.Options != nil |
| 67 |
case http.MethodHead: |
| 68 |
// Treat HEAD as present when either |
| 69 |
// a Head operation exists or, if Head is absent, when a Get exists |
| 70 |
// per HTTP semantics (HEAD can be handled by GET if no explicit |
| 71 |
// HEAD operation is defined). |
| 72 |
return pathItem.Head != nil || pathItem.Get != nil |
| 73 |
case http.MethodPatch: |
| 74 |
return pathItem.Patch != nil |
| 75 |
case http.MethodTrace: |
| 76 |
return pathItem.Trace != nil |
| 77 |
} |
| 78 |
return false |
| 79 |
} |
| 80 |
|
| 81 |
// selectMatches finds the best matching candidates in a single pass. |
| 82 |
// returns the highest-scoring candidate with the method (or nil), and |
| 83 |
// the highest-scoring candidate overall (for error reporting). |
| 84 |
func selectMatches(candidates []pathCandidate) (withMethod, highest *pathCandidate) { |
| 85 |
for i := range candidates { |
| 86 |
c := &candidates[i] |
| 87 |
|
| 88 |
if c.hasMethod && (withMethod == nil || c.score > withMethod.score) { |
| 89 |
withMethod = c |
| 90 |
} |
| 91 |
|
| 92 |
if highest == nil || c.score > highest.score { |
| 93 |
highest = c |
| 94 |
} |
| 95 |
} |
| 96 |
return withMethod, highest |
| 97 |
} |