Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix trailing slash handling in FilterSpecByPathsWithoutSideEffects #520

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions pkg/aggregator/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,22 @@ func FilterSpecByPathsWithoutSideEffects(sp *spec.Swagger, keepPathPrefixes []st
// it is unused because of a path prune.
initialUsedDefinitions := usedDefinitionForSpec(sp)

// Ensure prefixes ends with a "/", so that "/v1beta1" won't be treated as
// having the prefix "/v1" for example.
var normalizedPrefixes []string
for _, p := range keepPathPrefixes {
normalizedPrefixes = append(normalizedPrefixes, ensureTailingSlash(p))
}

// First remove unwanted paths
prefixes := util.NewTrie(keepPathPrefixes)
prefixes := util.NewTrie(normalizedPrefixes)
ret := *sp
ret.Paths = &spec.Paths{
VendorExtensible: sp.Paths.VendorExtensible,
Paths: map[string]spec.PathItem{},
}
for path, pathItem := range sp.Paths.Paths {
if !prefixes.HasPrefix(path) {
if !prefixes.HasPrefix(ensureTailingSlash(path)) {
continue
}
ret.Paths.Paths[path] = pathItem
Expand All @@ -90,6 +97,13 @@ func FilterSpecByPathsWithoutSideEffects(sp *spec.Swagger, keepPathPrefixes []st
return &ret
}

func ensureTailingSlash(path string) string {
if strings.HasSuffix(path, "/") {
return path
}
return path + "/"
}

// renameDefinitions renames definition references, without mutating the input.
// The output might share data structures with the input.
func renameDefinitions(s *spec.Swagger, renames map[string]string) *spec.Swagger {
Expand Down
70 changes: 70 additions & 0 deletions pkg/aggregator/aggregator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,76 @@ definitions:
ast.Equal(DebugSpec{orig_spec1}, DebugSpec{spec1}, "unexpected mutation of input")
}

var trailingSlashesTestCases = []struct {
input string
prefix string
shouldKeep bool
}{
{"/test", "/test", true},
{"/test/", "/test/", true},
{"/test", "/test/", true},
{"/test/", "/test", true},
{"/testv1", "/test", false},
{"/testv1", "/test/", false},
}

func TestFilterSpecsTrailingSlashes(t *testing.T) {
specTemplate := `
swagger: "2.0"
paths:
%s:
post:
tags:
- "test"
summary: "Test API"
operationId: "addTest"
parameters:
- in: "body"
name: "body"
description: "test object"
required: true
schema:
$ref: "#/definitions/Test"
responses:
405:
description: "Invalid input"
$ref: "#/definitions/InvalidInput"
definitions:
Test:
type: "object"
properties:
id:
type: "integer"
format: "int64"
status:
type: "string"
description: "Status"
InvalidInput:
type: "string"
format: "string"
Unused:
type: "object"
`

for _, tc := range trailingSlashesTestCases {
var spec, specFiltered *spec.Swagger

yaml.Unmarshal([]byte(fmt.Sprintf(specTemplate, tc.input)), &spec)
yaml.Unmarshal([]byte(`
swagger: "2.0"
paths:
`), &specFiltered)

ast := assert.New(t)
filtered := FilterSpecByPathsWithoutSideEffects(spec, []string{tc.prefix})
if tc.shouldKeep {
ast.Equal(DebugSpec{filtered}, DebugSpec{spec}, "input=%q, prefix=%q, should be included", tc.input, tc.prefix)
} else {
ast.Equal(DebugSpec{spec}, DebugSpec{spec}, "input=%q, prefix=%q, should not be included", tc.input, tc.prefix)
}
}
}

func TestMergeSpecsSimple(t *testing.T) {
var spec1, spec2, expected *spec.Swagger
require.NoError(t, yaml.Unmarshal([]byte(`
Expand Down
Loading