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

fixed panic in path validation (issue #264) #266

Merged
merged 3 commits into from
Nov 15, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
36 changes: 19 additions & 17 deletions openapi3/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,32 @@ func (paths Paths) Validate(c context.Context) error {
}
normalizedPaths[path] = path

var globalCount uint
for _, parameterRef := range pathItem.Parameters {
if parameterRef != nil {
if parameter := parameterRef.Value; parameter != nil && parameter.In == ParameterInPath {
globalCount++
}
}
}
for method, operation := range pathItem.Operations() {
var count uint
for _, parameterRef := range operation.Parameters {
if pathItem != nil {
fenollp marked this conversation as resolved.
Show resolved Hide resolved
var globalCount uint
for _, parameterRef := range pathItem.Parameters {
if parameterRef != nil {
if parameter := parameterRef.Value; parameter != nil && parameter.In == ParameterInPath {
count++
globalCount++
}
}
}
if count+globalCount != pathParamsCount {
return fmt.Errorf("operation %s %s must define exactly all path parameters", method, path)
for method, operation := range pathItem.Operations() {
var count uint
for _, parameterRef := range operation.Parameters {
if parameterRef != nil {
if parameter := parameterRef.Value; parameter != nil && parameter.In == ParameterInPath {
count++
}
}
}
if count+globalCount != pathParamsCount {
return fmt.Errorf("operation %s %s must define exactly all path parameters", method, path)
}
}
}

if err := pathItem.Validate(c); err != nil {
return err
if err := pathItem.Validate(c); err != nil {
return err
}
}
}
return nil
Expand Down
28 changes: 28 additions & 0 deletions openapi3/paths_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package openapi3

import (
"context"
"testing"

"github.com/stretchr/testify/require"
)

var emptyPathSpec = `
openapi: "3.0.0"
info:
version: 1.0.0
title: Swagger Petstore
license:
name: MIT
servers:
- url: http://petstore.swagger.io/v1
paths:
/pets:
`

func TestPathValidate(t *testing.T) {
swagger, err := NewSwaggerLoader().LoadSwaggerFromData([]byte(emptyPathSpec))
require.NoError(t, err)
err = swagger.Paths.Validate(context.Background())
require.NoError(t, err)
}