Skip to content

Commit

Permalink
test WithLocation
Browse files Browse the repository at this point in the history
  • Loading branch information
reuvenharrison committed Nov 25, 2024
1 parent 0e5acd7 commit ac2f9e2
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 18 deletions.
14 changes: 11 additions & 3 deletions checker/api_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ type ApiChange struct {
OperationId string
Path string
Source *load.Source
Origin *openapi3.Origin

SourceFile string
SourceLine int
Expand Down Expand Up @@ -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
}

Expand Down
10 changes: 7 additions & 3 deletions checker/api_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion checker/check_added_required_request_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func AddedRequestBodyCheck(diffReport *diff.Diff, operationsSources *diff.Operat
operationItem.Revision,
operation,
path,
))
).WithLocation(operationItem.Revision.RequestBody.Value.Origin, "required"))
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions checker/check_api_deprecation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion checker/check_request_body_required_value_updated.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func RequestBodyRequiredUpdatedCheck(diffReport *diff.Diff, operationsSources *d
operationItem.Revision,
operation,
path,
))
).WithLocation(operationItem.Revision.RequestBody.Value.Origin, "required"))
}
}
return result
Expand Down
20 changes: 10 additions & 10 deletions checker/check_request_body_required_value_updated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}

Expand Down
60 changes: 60 additions & 0 deletions data/checker/request_body_became_required_revision.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ac2f9e2

Please sign in to comment.