From ac2f9e20fa716ca4aa0ea145de56a345cf163515 Mon Sep 17 00:00:00 2001 From: Reuven Harrison Date: Mon, 25 Nov 2024 18:37:19 +0200 Subject: [PATCH] test WithLocation --- checker/api_change.go | 14 ++++- checker/api_change_test.go | 10 +++- checker/check_added_required_request_body.go | 2 +- checker/check_api_deprecation_test.go | 6 ++ ...eck_request_body_required_value_updated.go | 2 +- ...equest_body_required_value_updated_test.go | 20 +++---- ...request_body_became_required_revision.yaml | 60 +++++++++++++++++++ 7 files changed, 96 insertions(+), 18 deletions(-) create mode 100644 data/checker/request_body_became_required_revision.yaml diff --git a/checker/api_change.go b/checker/api_change.go index aa7ece14..70233ce5 100644 --- a/checker/api_change.go +++ b/checker/api_change.go @@ -22,7 +22,6 @@ type ApiChange struct { OperationId string Path string Source *load.Source - Origin *openapi3.Origin SourceFile string SourceLine int @@ -50,8 +49,17 @@ func NewApiChange(id string, config *Config, args []any, comment string, operati } } -func (c ApiChange) WithOrigin(origin *openapi3.Origin) ApiChange { - c.Origin = origin +func (c ApiChange) WithLocation(origin *openapi3.Origin, field string) ApiChange { + if origin == nil { + return c + } + location, ok := origin.Fields[field] + if !ok { + return c + } + + c.SourceLine = location.Line + c.SourceColumn = location.Column return c } diff --git a/checker/api_change_test.go b/checker/api_change_test.go index 23a667a7..5a4380c6 100644 --- a/checker/api_change_test.go +++ b/checker/api_change_test.go @@ -86,7 +86,11 @@ func TestApiChange_SourceUrl(t *testing.T) { } func TestApiChange_WithOrigin(t *testing.T) { - apiChangeSourceFile := apiChange - origin := openapi3.Origin{} - require.True(t, &origin == apiChangeSourceFile.WithOrigin(&origin).Origin) + apiChangeSourceFile := apiChange.WithLocation(&openapi3.Origin{ + Fields: map[string]openapi3.Location{"field": { + Line: 1, + Column: 2, + }}}, "field") + require.Equal(t, 1, apiChangeSourceFile.SourceLine) + require.Equal(t, 2, apiChangeSourceFile.SourceColumn) } diff --git a/checker/check_added_required_request_body.go b/checker/check_added_required_request_body.go index 14e23aa9..a681e607 100644 --- a/checker/check_added_required_request_body.go +++ b/checker/check_added_required_request_body.go @@ -38,7 +38,7 @@ func AddedRequestBodyCheck(diffReport *diff.Diff, operationsSources *diff.Operat operationItem.Revision, operation, path, - )) + ).WithLocation(operationItem.Revision.RequestBody.Value.Origin, "required")) } } } diff --git a/checker/check_api_deprecation_test.go b/checker/check_api_deprecation_test.go index 5acf8dbd..79d122f5 100644 --- a/checker/check_api_deprecation_test.go +++ b/checker/check_api_deprecation_test.go @@ -18,6 +18,12 @@ func open(file string) (*load.SpecInfo, error) { return load.NewSpecInfo(openapi3.NewLoader(), load.NewSource(file)) } +func openWithLocation(file string) (*load.SpecInfo, error) { + loader := openapi3.NewLoader() + loader.IncludeOrigin = true + return load.NewSpecInfo(loader, load.NewSource(file)) +} + func getDeprecationFile(file string) string { return fmt.Sprintf("../data/deprecation/%s", file) } diff --git a/checker/check_request_body_required_value_updated.go b/checker/check_request_body_required_value_updated.go index 7f1b037b..22c1f2e2 100644 --- a/checker/check_request_body_required_value_updated.go +++ b/checker/check_request_body_required_value_updated.go @@ -41,7 +41,7 @@ func RequestBodyRequiredUpdatedCheck(diffReport *diff.Diff, operationsSources *d operationItem.Revision, operation, path, - )) + ).WithLocation(operationItem.Revision.RequestBody.Value.Origin, "required")) } } return result diff --git a/checker/check_request_body_required_value_updated_test.go b/checker/check_request_body_required_value_updated_test.go index 67950e66..2f5d8805 100644 --- a/checker/check_request_body_required_value_updated_test.go +++ b/checker/check_request_body_required_value_updated_test.go @@ -11,24 +11,24 @@ import ( // CL: changing request's body to required is breaking func TestRequestBodyBecameRequired(t *testing.T) { - s1, err := open("../data/checker/request_body_became_required_base.yaml") + s1, err := openWithLocation("../data/checker/request_body_became_required_base.yaml") require.NoError(t, err) - s2, err := open("../data/checker/request_body_became_required_base.yaml") + s2, err := openWithLocation("../data/checker/request_body_became_required_revision.yaml") require.NoError(t, err) - s2.Spec.Paths.Value("/api/v1.0/groups").Post.RequestBody.Value.Required = true - d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2) require.NoError(t, err) errs := checker.CheckBackwardCompatibility(singleCheckConfig(checker.RequestBodyRequiredUpdatedCheck), d, osm) require.Len(t, errs, 1) require.Equal(t, checker.ApiChange{ - Id: checker.RequestBodyBecameRequiredId, - Level: checker.ERR, - Operation: "POST", - Path: "/api/v1.0/groups", - Source: load.NewSource("../data/checker/request_body_became_required_base.yaml"), - OperationId: "createOneGroup", + Id: checker.RequestBodyBecameRequiredId, + Level: checker.ERR, + Operation: "POST", + Path: "/api/v1.0/groups", + Source: load.NewSource("../data/checker/request_body_became_required_revision.yaml"), + OperationId: "createOneGroup", + SourceLine: 19, + SourceColumn: 9, }, errs[0]) } diff --git a/data/checker/request_body_became_required_revision.yaml b/data/checker/request_body_became_required_revision.yaml new file mode 100644 index 00000000..5835dd48 --- /dev/null +++ b/data/checker/request_body_became_required_revision.yaml @@ -0,0 +1,60 @@ +openapi: 3.0.1 +info: + title: Tufin + version: "2.0" +servers: +- url: https://localhost:9080 +paths: + /api/v1.0/groups: + post: + tags: + - Group + operationId: createOneGroup + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/GroupView' + description: Creates one project. + required: true + responses: + "200": + content: + application/json: + schema: + $ref: '#/components/schemas/GroupView' + description: OK + "409": + content: + application/json: + schema: + $ref: '#/components/schemas/GroupView' + description: Conflict + summary: Create One Project +components: + parameters: + groupId: + in: path + name: groupId + required: true + schema: + type: string + schemas: + GroupView: + type: object + properties: + data: + type: object + properties: + created: + type: string + format: date-time + readOnly: true + pattern: "^[a-z]+$" + id: + type: string + readOnly: true + name: + type: string + required: + - name \ No newline at end of file