Skip to content

Commit 5828615

Browse files
its-hammer-timedaveshanley
authored andcommitted
Add test coverage for radix tree config and path lookup options
Cover WithPathTree, DisablePathTree, IsPathTreeDisabled, and the FindPath radix tree fast-path branches (method match and mismatch). Made-with: Cursor
1 parent bf6a37b commit 5828615

2 files changed

Lines changed: 92 additions & 0 deletions

File tree

config/config_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,3 +491,34 @@ func TestStrictModeWithIgnorePaths(t *testing.T) {
491491
assert.True(t, opts.StrictMode)
492492
assert.Equal(t, paths, opts.StrictIgnorePaths)
493493
}
494+
495+
func TestWithPathTree(t *testing.T) {
496+
// Use a mock/nil path tree — WithPathTree just sets the field
497+
opts := NewValidationOptions(WithPathTree(nil))
498+
assert.Nil(t, opts.PathTree)
499+
500+
// TestWithPathTree with a real value — use a custom implementation
501+
// We can verify the field is set using a simple check
502+
opts2 := &ValidationOptions{}
503+
WithPathTree(nil)(opts2)
504+
assert.Nil(t, opts2.PathTree)
505+
}
506+
507+
func TestDisablePathTree(t *testing.T) {
508+
opts := NewValidationOptions(DisablePathTree())
509+
assert.True(t, opts.IsPathTreeDisabled())
510+
}
511+
512+
func TestIsPathTreeDisabled_Default(t *testing.T) {
513+
opts := NewValidationOptions()
514+
assert.False(t, opts.IsPathTreeDisabled())
515+
}
516+
517+
func TestWithExistingOpts_PathTreeFields(t *testing.T) {
518+
original := NewValidationOptions(DisablePathTree())
519+
520+
opts := NewValidationOptions(WithExistingOpts(original))
521+
522+
assert.True(t, opts.IsPathTreeDisabled())
523+
assert.Nil(t, opts.PathTree)
524+
}

paths/paths_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)