From bd87af4b18b688d97615d84042ce43082b409740 Mon Sep 17 00:00:00 2001 From: Praneet Loke <1466314+praneetloke@users.noreply.github.com> Date: Mon, 19 Sep 2022 20:21:42 -0700 Subject: [PATCH 1/6] Improve error message when path validation fails --- openapi3/path_item.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openapi3/path_item.go b/openapi3/path_item.go index 4801e5f83..390ac9db5 100644 --- a/openapi3/path_item.go +++ b/openapi3/path_item.go @@ -134,7 +134,7 @@ func (pathItem *PathItem) Validate(ctx context.Context) error { for _, method := range methods { operation := operations[method] if err := operation.Validate(ctx); err != nil { - return err + return fmt.Errorf("path (summary: %s) failed validation for operation %s: %v", pathItem.Summary, method, err) } } return nil From 11697a32d8c3abda913b0ba3282fa03ce8f10214 Mon Sep 17 00:00:00 2001 From: Praneet Loke <1466314+praneetloke@users.noreply.github.com> Date: Mon, 19 Sep 2022 20:59:26 -0700 Subject: [PATCH 2/6] Pass the path into the Validate method --- openapi3/example_validation_test.go | 4 ++-- openapi3/loader_test.go | 4 ++-- openapi3/path_item.go | 2 +- openapi3/response_issue224_test.go | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/openapi3/example_validation_test.go b/openapi3/example_validation_test.go index 85e158e6b..45d0dd518 100644 --- a/openapi3/example_validation_test.go +++ b/openapi3/example_validation_test.go @@ -26,7 +26,7 @@ func TestExamplesSchemaValidation(t *testing.T) { param1example: value: abcd `, - errContains: "invalid paths: param1example", + errContains: "param1example", }, { name: "valid_parameter_examples", @@ -64,7 +64,7 @@ func TestExamplesSchemaValidation(t *testing.T) { email: bad password: short `, - errContains: "invalid paths: BadUser", + errContains: "BadUser", }, { name: "valid_component_examples", diff --git a/openapi3/loader_test.go b/openapi3/loader_test.go index 64a923c39..f55d0e8d3 100644 --- a/openapi3/loader_test.go +++ b/openapi3/loader_test.go @@ -91,12 +91,12 @@ func TestResolveSchemaRef(t *testing.T) { } func TestResolveSchemaRefWithNullSchemaRef(t *testing.T) { - source := []byte(`{"openapi":"3.0.0","info":{"title":"MyAPI","version":"0.1","description":"An API"},"paths":{"/foo":{"post":{"requestBody":{"content":{"application/json":{"schema":null}}}}}}}`) + source := []byte(`{"openapi":"3.0.0","info":{"title":"MyAPI","version":"0.1","description":"An API"},"paths":{"/foo":{"post":{"summary":"Create Foo","requestBody":{"content":{"application/json":{"schema":null}}}}}}}`) loader := NewLoader() doc, err := loader.LoadFromData(source) require.NoError(t, err) err = doc.Validate(loader.Context) - require.EqualError(t, err, `invalid paths: found unresolved ref: ""`) + require.EqualError(t, err, `invalid paths: path /foo failed validation for operation POST: found unresolved ref: ""`) } func TestResolveResponseExampleRef(t *testing.T) { diff --git a/openapi3/path_item.go b/openapi3/path_item.go index 390ac9db5..df1381cfd 100644 --- a/openapi3/path_item.go +++ b/openapi3/path_item.go @@ -134,7 +134,7 @@ func (pathItem *PathItem) Validate(ctx context.Context) error { for _, method := range methods { operation := operations[method] if err := operation.Validate(ctx); err != nil { - return fmt.Errorf("path (summary: %s) failed validation for operation %s: %v", pathItem.Summary, method, err) + return fmt.Errorf("path %s failed validation for operation %s: %v", path, method, err) } } return nil diff --git a/openapi3/response_issue224_test.go b/openapi3/response_issue224_test.go index 97c7e6b20..3634ddc49 100644 --- a/openapi3/response_issue224_test.go +++ b/openapi3/response_issue224_test.go @@ -457,5 +457,5 @@ func TestEmptyResponsesAreInvalid(t *testing.T) { require.NoError(t, err) require.Equal(t, doc.ExternalDocs.Description, "See AsyncAPI example") err = doc.Validate(context.Background()) - require.EqualError(t, err, `invalid paths: the responses object MUST contain at least one response code`) + require.Contains(t, err.Error(), `the responses object MUST contain at least one response code`) } From ae39fb7a032d5145d8fef8222d940b67c3c723c5 Mon Sep 17 00:00:00 2001 From: Praneet Loke <1466314+praneetloke@users.noreply.github.com> Date: Wed, 21 Sep 2022 08:04:11 -0700 Subject: [PATCH 3/6] Simplify --- openapi3/loader_test.go | 2 +- openapi3/path_item.go | 2 +- openapi3/paths.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/openapi3/loader_test.go b/openapi3/loader_test.go index f55d0e8d3..9d5fbb440 100644 --- a/openapi3/loader_test.go +++ b/openapi3/loader_test.go @@ -96,7 +96,7 @@ func TestResolveSchemaRefWithNullSchemaRef(t *testing.T) { doc, err := loader.LoadFromData(source) require.NoError(t, err) err = doc.Validate(loader.Context) - require.EqualError(t, err, `invalid paths: path /foo failed validation for operation POST: found unresolved ref: ""`) + require.EqualError(t, err, `invalid paths: path /foo failed validation: validation failed for operation POST: found unresolved ref: ""`) } func TestResolveResponseExampleRef(t *testing.T) { diff --git a/openapi3/path_item.go b/openapi3/path_item.go index df1381cfd..940b1592a 100644 --- a/openapi3/path_item.go +++ b/openapi3/path_item.go @@ -134,7 +134,7 @@ func (pathItem *PathItem) Validate(ctx context.Context) error { for _, method := range methods { operation := operations[method] if err := operation.Validate(ctx); err != nil { - return fmt.Errorf("path %s failed validation for operation %s: %v", path, method, err) + return fmt.Errorf("invalid operation %s: %v", method, err) } } return nil diff --git a/openapi3/paths.go b/openapi3/paths.go index b116f6cb6..e3da7d05b 100644 --- a/openapi3/paths.go +++ b/openapi3/paths.go @@ -96,7 +96,7 @@ func (paths Paths) Validate(ctx context.Context) error { } if err := pathItem.Validate(ctx); err != nil { - return err + return fmt.Errorf("invalid path %s: %v", path, err) } } From 993697098e6a4822c6f9a37e3b1ea961d91a4185 Mon Sep 17 00:00:00 2001 From: Praneet Loke <1466314+praneetloke@users.noreply.github.com> Date: Wed, 21 Sep 2022 08:08:48 -0700 Subject: [PATCH 4/6] Update test --- openapi3/loader_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openapi3/loader_test.go b/openapi3/loader_test.go index 9d5fbb440..a08ed2754 100644 --- a/openapi3/loader_test.go +++ b/openapi3/loader_test.go @@ -96,7 +96,7 @@ func TestResolveSchemaRefWithNullSchemaRef(t *testing.T) { doc, err := loader.LoadFromData(source) require.NoError(t, err) err = doc.Validate(loader.Context) - require.EqualError(t, err, `invalid paths: path /foo failed validation: validation failed for operation POST: found unresolved ref: ""`) + require.EqualError(t, err, `invalid paths: invalid path /foo: invalid operation POST: found unresolved ref: ""`) } func TestResolveResponseExampleRef(t *testing.T) { From 1d8a9aedcd891c284213a289b63aef5974400483 Mon Sep 17 00:00:00 2001 From: Praneet Loke <1466314+praneetloke@users.noreply.github.com> Date: Wed, 21 Sep 2022 12:23:20 -0700 Subject: [PATCH 5/6] Update tests per feedback --- openapi3/example_validation_test.go | 4 ++-- openapi3/loader_test.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/openapi3/example_validation_test.go b/openapi3/example_validation_test.go index 45d0dd518..177360c13 100644 --- a/openapi3/example_validation_test.go +++ b/openapi3/example_validation_test.go @@ -26,7 +26,7 @@ func TestExamplesSchemaValidation(t *testing.T) { param1example: value: abcd `, - errContains: "param1example", + errContains: "invalid paths: invalid path /user: invalid operation POST: param1example", }, { name: "valid_parameter_examples", @@ -64,7 +64,7 @@ func TestExamplesSchemaValidation(t *testing.T) { email: bad password: short `, - errContains: "BadUser", + errContains: "invalid paths: invalid path /user: invalid operation POST: BadUser", }, { name: "valid_component_examples", diff --git a/openapi3/loader_test.go b/openapi3/loader_test.go index a08ed2754..d492e2471 100644 --- a/openapi3/loader_test.go +++ b/openapi3/loader_test.go @@ -91,7 +91,7 @@ func TestResolveSchemaRef(t *testing.T) { } func TestResolveSchemaRefWithNullSchemaRef(t *testing.T) { - source := []byte(`{"openapi":"3.0.0","info":{"title":"MyAPI","version":"0.1","description":"An API"},"paths":{"/foo":{"post":{"summary":"Create Foo","requestBody":{"content":{"application/json":{"schema":null}}}}}}}`) + source := []byte(`{"openapi":"3.0.0","info":{"title":"MyAPI","version":"0.1","description":"An API"},"paths":{"/foo":{"post":{"requestBody":{"content":{"application/json":{"schema":null}}}}}}}`) loader := NewLoader() doc, err := loader.LoadFromData(source) require.NoError(t, err) From ab4c65015a2bc532534a2101a7038bc84678847a Mon Sep 17 00:00:00 2001 From: Praneet Loke <1466314+praneetloke@users.noreply.github.com> Date: Thu, 22 Sep 2022 10:30:20 -0700 Subject: [PATCH 6/6] Update test --- openapi3/response_issue224_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openapi3/response_issue224_test.go b/openapi3/response_issue224_test.go index 3634ddc49..5de0525e4 100644 --- a/openapi3/response_issue224_test.go +++ b/openapi3/response_issue224_test.go @@ -457,5 +457,5 @@ func TestEmptyResponsesAreInvalid(t *testing.T) { require.NoError(t, err) require.Equal(t, doc.ExternalDocs.Description, "See AsyncAPI example") err = doc.Validate(context.Background()) - require.Contains(t, err.Error(), `the responses object MUST contain at least one response code`) + require.EqualError(t, err, `invalid paths: invalid path /pet: invalid operation POST: the responses object MUST contain at least one response code`) }