@@ -13,6 +13,7 @@ import (
1313
1414 "github.com/pb33f/libopenapi"
1515 "github.com/pb33f/libopenapi-validator/config"
16+ "github.com/pb33f/libopenapi-validator/radix"
1617 "github.com/stretchr/testify/assert"
1718)
1819
@@ -1509,3 +1510,63 @@ paths:
15091510 assert .Equal (t , "missingOperation" , errs [0 ].ValidationSubType )
15101511 assert .Equal (t , "/entities('{id}')" , foundPath )
15111512}
1513+
1514+ func TestFindPath_WithPathTree_MethodMatch (t * testing.T ) {
1515+ // Exercise radix tree fast-path: options.PathTree != nil, method matches → return pathItem, nil, matchedPath
1516+ spec := `openapi: 3.1.0
1517+ info:
1518+ title: Radix Tree Test
1519+ version: 1.0.0
1520+ paths:
1521+ /users/{id}:
1522+ get:
1523+ operationId: getUser
1524+ responses:
1525+ '200':
1526+ description: OK
1527+ `
1528+ doc , _ := libopenapi .NewDocument ([]byte (spec ))
1529+ m , _ := doc .BuildV3Model ()
1530+
1531+ pathTree := radix .BuildPathTree (& m .Model )
1532+ opts := config .NewValidationOptions (config .WithPathTree (pathTree ))
1533+
1534+ request , _ := http .NewRequest (http .MethodGet , "https://api.com/users/123" , nil )
1535+ pathItem , errs , foundPath := FindPath (request , & m .Model , opts )
1536+
1537+ assert .Nil (t , errs )
1538+ assert .NotNil (t , pathItem )
1539+ assert .Equal (t , "getUser" , pathItem .Get .OperationId )
1540+ assert .Equal (t , "/users/{id}" , foundPath )
1541+ }
1542+
1543+ func TestFindPath_WithPathTree_MethodMismatch (t * testing.T ) {
1544+ // Exercise radix tree fast-path: options.PathTree != nil, path found but wrong method
1545+ // → return pathItem, missingOperationError(...), matchedPath
1546+ spec := `openapi: 3.1.0
1547+ info:
1548+ title: Radix Tree Test
1549+ version: 1.0.0
1550+ paths:
1551+ /users/{id}:
1552+ get:
1553+ operationId: getUser
1554+ responses:
1555+ '200':
1556+ description: OK
1557+ `
1558+ doc , _ := libopenapi .NewDocument ([]byte (spec ))
1559+ m , _ := doc .BuildV3Model ()
1560+
1561+ pathTree := radix .BuildPathTree (& m .Model )
1562+ opts := config .NewValidationOptions (config .WithPathTree (pathTree ))
1563+
1564+ request , _ := http .NewRequest (http .MethodPost , "https://api.com/users/123" , nil )
1565+ pathItem , errs , foundPath := FindPath (request , & m .Model , opts )
1566+
1567+ assert .NotNil (t , pathItem )
1568+ assert .NotNil (t , errs )
1569+ assert .Len (t , errs , 1 )
1570+ assert .True (t , errs [0 ].IsOperationMissingError ())
1571+ assert .Equal (t , "/users/{id}" , foundPath )
1572+ }
0 commit comments