From da714f42fa6c62b1eead22b7545b843c24bb8644 Mon Sep 17 00:00:00 2001 From: Samuel Monderer Date: Sun, 15 Nov 2020 21:06:41 +0200 Subject: [PATCH] fixed panic in path validation (issue #264) (#266) Co-authored-by: Samuel Monderer --- openapi3/paths.go | 5 +++++ openapi3/paths_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 openapi3/paths_test.go diff --git a/openapi3/paths.go b/openapi3/paths.go index 0bfeb2c79..baafaaabc 100644 --- a/openapi3/paths.go +++ b/openapi3/paths.go @@ -16,6 +16,11 @@ func (paths Paths) Validate(c context.Context) error { return fmt.Errorf("path %q does not start with a forward slash (/)", path) } + if pathItem == nil { + paths[path] = &PathItem{} + pathItem = paths[path] + } + normalizedPath, pathParamsCount := normalizeTemplatedPath(path) if oldPath, ok := normalizedPaths[normalizedPath]; ok { return fmt.Errorf("conflicting paths %q and %q", path, oldPath) diff --git a/openapi3/paths_test.go b/openapi3/paths_test.go new file mode 100644 index 000000000..cc8d9306c --- /dev/null +++ b/openapi3/paths_test.go @@ -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) +}