Skip to content

Commit

Permalink
fix Unchanged
Browse files Browse the repository at this point in the history
  • Loading branch information
Reuven committed Mar 6, 2024
1 parent e1108f3 commit a90fd08
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 28 deletions.
29 changes: 29 additions & 0 deletions data/delta/base.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
openapi: 3.0.0
info:
title: Sample API
version: 1.0.0
paths:
/users:
get:
summary: List all users
responses:
'200':
description: A list of users.
/users/{userId}:
get:
summary: Get a single user by ID
parameters:
- name: userId
in: path
required: true
schema:
type: string
responses:
'200':
description: A single user.
/products:
get:
summary: List all products
responses:
'200':
description: A list of products.
17 changes: 17 additions & 0 deletions data/delta/revision.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
openapi: 3.0.0
info:
title: Sample API
version: 1.0.0
paths:
/users:
get:
summary: List all users
responses:
'200':
description: A list of users.
/products:
post:
summary: List all products
responses:
'200':
description: A list of products.
29 changes: 25 additions & 4 deletions delta/delta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,12 @@ func TestSymmetric(t *testing.T) {
s2, err := loader.LoadFromFile(pair.Y)
require.NoError(t, err)

d1, err := diff.Get(diff.NewConfig(), s1, s2)
config := diff.NewConfig().WithUnchanged()

d1, err := diff.Get(config, s1, s2)
require.NoError(t, err)

d2, err := diff.Get(diff.NewConfig(), s2, s1)
d2, err := diff.Get(config, s2, s1)
require.NoError(t, err)

require.Equal(t, delta.Get(false, d1), delta.Get(false, d2), pair)
Expand All @@ -215,11 +217,13 @@ func TestAsymmetric(t *testing.T) {
s2, err := loader.LoadFromFile(pair.Y)
require.NoError(t, err)

d1, err := diff.Get(diff.NewConfig(), s1, s2)
config := diff.NewConfig().WithUnchanged()

d1, err := diff.Get(config, s1, s2)
require.NoError(t, err)
asymmetric1 := delta.Get(true, d1)

d2, err := diff.Get(diff.NewConfig(), s2, s1)
d2, err := diff.Get(config, s2, s1)
require.NoError(t, err)
asymmetric2 := delta.Get(true, d2)

Expand All @@ -228,3 +232,20 @@ func TestAsymmetric(t *testing.T) {
require.Equal(t, asymmetric1+asymmetric2, symmetric, pair)
}
}

func TestUnchanged(t *testing.T) {

loader := openapi3.NewLoader()
s1, err := loader.LoadFromFile("../data/delta/base.yaml")
require.NoError(t, err)

s2, err := loader.LoadFromFile("../data/delta/revision.yaml")
require.NoError(t, err)

config := diff.NewConfig().WithUnchanged()

d, err := diff.Get(config, s1, s2)
require.NoError(t, err)

require.Equal(t, 0.75, delta.Get(false, d))
}
6 changes: 6 additions & 0 deletions diff/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Config struct {
PathStripPrefixRevision string
ExcludeElements utils.StringSet
IncludePathParams bool
Unchanged bool
}

const (
Expand Down Expand Up @@ -79,3 +80,8 @@ func (config *Config) WithCheckBreaking() *Config {

return config
}

func (config *Config) WithUnchanged() *Config {
config.Unchanged = true
return config

Check warning on line 86 in diff/config.go

View check run for this annotation

Codecov / codecov/patch

diff/config.go#L84-L86

Added lines #L84 - L86 were not covered by tests
}
7 changes: 3 additions & 4 deletions diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,9 @@ func TestDiff_ModifiedOperation(t *testing.T) {
require.NoError(t, err)

require.Equal(t, &diff.OperationsDiff{
Added: utils.StringList{"GET"},
Deleted: utils.StringList{"POST"},
Modified: diff.ModifiedOperations{},
Unchanged: utils.StringList{},
Added: utils.StringList{"GET"},
Deleted: utils.StringList{"POST"},
Modified: diff.ModifiedOperations{},
},
d.PathsDiff.Modified["/api/test"].OperationsDiff)
}
Expand Down
39 changes: 22 additions & 17 deletions diff/endpoints_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ func (diff *EndpointsDiff) Empty() bool {

return len(diff.Added) == 0 &&
len(diff.Deleted) == 0 &&
len(diff.Modified) == 0
len(diff.Modified) == 0 &&
len(diff.Unchanged) == 0
}

func newEndpointsDiff() *EndpointsDiff {
Expand All @@ -60,6 +61,10 @@ func getEndpointsDiff(config *Config, state *state, paths1, paths2 *openapi3.Pat
return nil, err
}

if !config.Unchanged {
diff.Unchanged = nil
}

if diff.Empty() {
return nil, nil
}
Expand All @@ -78,74 +83,74 @@ func getEndpointsDiffInternal(config *Config, state *state, paths1, paths2 *open

for path, pathItem := range addedPaths.Map() {
for method := range pathItem.Operations() {
result.addAddedPath(path, method)
result.addAddedEndpoint(path, method)
}
}

for path, pathItem := range deletedPaths.Map() {
for method := range pathItem.Operations() {
result.addDeletedPath(path, method)
result.addDeletedEndpoint(path, method)
}
}

for path, pathItemPair := range otherPaths {
if err := result.addModifiedPaths(config, state, path, pathItemPair); err != nil {
if err := result.addModifiedEndpoints(config, state, path, pathItemPair); err != nil {
return nil, err
}
}

return result, nil
}

func (diff *EndpointsDiff) addAddedPath(path string, method string) {
func (diff *EndpointsDiff) addAddedEndpoint(path string, method string) {
diff.Added = append(diff.Added, Endpoint{
Method: method,
Path: path,
})
}

func (diff *EndpointsDiff) addDeletedPath(path string, method string) {
func (diff *EndpointsDiff) addDeletedEndpoint(path string, method string) {
diff.Deleted = append(diff.Deleted, Endpoint{
Method: method,
Path: path,
})
}

func (diff *EndpointsDiff) addUnchangedPath(path string, method string) {
func (diff *EndpointsDiff) addUnchangedEndpoint(path string, method string) {

Check warning on line 119 in diff/endpoints_diff.go

View check run for this annotation

Codecov / codecov/patch

diff/endpoints_diff.go#L119

Added line #L119 was not covered by tests
diff.Unchanged = append(diff.Unchanged, Endpoint{
Method: method,
Path: path,
})
}

func (diff *EndpointsDiff) addModifiedPaths(config *Config, state *state, path string, pathItemPair *pathItemPair) error {
func (diff *EndpointsDiff) addModifiedEndpoints(config *Config, state *state, path string, pathItemPair *pathItemPair) error {

pathDiff, err := getPathDiff(config, state, pathItemPair)
operationsDiff, err := getOperationsDiff(config, state, pathItemPair)
if err != nil {
return err
}

if pathDiff.Empty() || pathDiff.OperationsDiff.Empty() {
if operationsDiff.Empty() {
return nil
}

for _, method := range pathDiff.OperationsDiff.Added {
diff.addAddedPath(path, method)
for _, method := range operationsDiff.Added {
diff.addAddedEndpoint(path, method)
}

for _, method := range pathDiff.OperationsDiff.Deleted {
diff.addDeletedPath(path, method)
for _, method := range operationsDiff.Deleted {
diff.addDeletedEndpoint(path, method)
}

for method, methodDiff := range pathDiff.OperationsDiff.Modified {
for method, methodDiff := range operationsDiff.Modified {
diff.Modified[Endpoint{
Method: method,
Path: path,
}] = methodDiff
}

for _, method := range pathDiff.OperationsDiff.Unchanged {
diff.addUnchangedPath(path, method)
for _, method := range operationsDiff.Unchanged {
diff.addUnchangedEndpoint(path, method)

Check warning on line 153 in diff/endpoints_diff.go

View check run for this annotation

Codecov / codecov/patch

diff/endpoints_diff.go#L153

Added line #L153 was not covered by tests
}

return nil
Expand Down
7 changes: 6 additions & 1 deletion diff/operations_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ func (operationsDiff *OperationsDiff) Empty() bool {

return len(operationsDiff.Added) == 0 &&
len(operationsDiff.Deleted) == 0 &&
len(operationsDiff.Modified) == 0
len(operationsDiff.Modified) == 0 &&
len(operationsDiff.Unchanged) == 0
}

func newOperationsDiff() *OperationsDiff {
Expand All @@ -51,6 +52,10 @@ func getOperationsDiff(config *Config, state *state, pathItemPair *pathItemPair)
return nil, err
}

if !config.Unchanged {
diff.Unchanged = nil
}

if diff.Empty() {
return nil, nil
}
Expand Down
7 changes: 6 additions & 1 deletion diff/parameters_diff_by_location.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func (diff *ParametersDiffByLocation) Empty() bool {

return len(diff.Added) == 0 &&
len(diff.Deleted) == 0 &&
len(diff.Modified) == 0
len(diff.Modified) == 0 &&
len(diff.Unchanged) == 0
}

// ParamLocations are the four possible locations of parameters: path, query, header or cookie
Expand Down Expand Up @@ -107,6 +108,10 @@ func getParametersDiffByLocation(config *Config, state *state, params1, params2
return nil, err
}

if !config.Unchanged {
diff.Unchanged = nil
}

if diff.Empty() {
return nil, nil
}
Expand Down
8 changes: 7 additions & 1 deletion diff/responses_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func (responsesDiff *ResponsesDiff) Empty() bool {

return len(responsesDiff.Added) == 0 &&
len(responsesDiff.Deleted) == 0 &&
len(responsesDiff.Modified) == 0
len(responsesDiff.Modified) == 0 &&
len(responsesDiff.Unchanged) == 0
}

// ModifiedResponses is map of response values to their respective diffs
Expand All @@ -48,6 +49,10 @@ func getResponsesDiff(config *Config, state *state, responses1, responses2 *open
return nil, err
}

if !config.Unchanged {
diff.Unchanged = nil
}

if diff.Empty() {
return nil, nil
}
Expand Down Expand Up @@ -75,6 +80,7 @@ func getResponsesDiffInternal(config *Config, state *state, responses1, response
if err != nil {
return nil, err
}

if diff.Empty() {
result.Unchanged = append(result.Unchanged, responseValue1)
} else {
Expand Down
6 changes: 6 additions & 0 deletions internal/changelog_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type ChangelogFlags struct {
deprecationDaysBeta int
deprecationDaysStable int
color string
unchanged bool
}

func (flags *ChangelogFlags) toConfig() *diff.Config {
Expand All @@ -41,6 +42,7 @@ func (flags *ChangelogFlags) toConfig() *diff.Config {
config.PathStripPrefixBase = flags.stripPrefixBase
config.PathStripPrefixRevision = flags.stripPrefixRevision
config.IncludePathParams = flags.includePathParams
config.Unchanged = flags.unchanged

return config
}
Expand Down Expand Up @@ -125,6 +127,10 @@ func (flags *ChangelogFlags) setRevision(source *load.Source) {
flags.revision = source
}

func (flags *ChangelogFlags) setUnchanged(unchanged bool) {
flags.unchanged = unchanged

Check warning on line 131 in internal/changelog_flags.go

View check run for this annotation

Codecov / codecov/patch

internal/changelog_flags.go#L130-L131

Added lines #L130 - L131 were not covered by tests
}

func (flags *ChangelogFlags) addExcludeElements(element string) {
flags.excludeElements = append(flags.excludeElements, element)
}
Expand Down
1 change: 1 addition & 0 deletions internal/delta.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func getDeltaCmd() *cobra.Command {
func runDelta(flags Flags, stdout io.Writer) (bool, *ReturnError) {

openapi3.CircularReferenceCounter = flags.getCircularReferenceCounter()
flags.setUnchanged(true)

diffResult, err := calcDiff(flags)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions internal/diff_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type DiffFlags struct {
circularReferenceCounter int
includePathParams bool
excludeElements []string
unchanged bool
}

func (flags *DiffFlags) toConfig() *diff.Config {
Expand All @@ -34,6 +35,7 @@ func (flags *DiffFlags) toConfig() *diff.Config {
config.PathStripPrefixBase = flags.stripPrefixBase
config.PathStripPrefixRevision = flags.stripPrefixRevision
config.IncludePathParams = flags.includePathParams
config.Unchanged = flags.unchanged

return config
}
Expand Down Expand Up @@ -118,6 +120,10 @@ func (flags *DiffFlags) setRevision(source *load.Source) {
flags.revision = source
}

func (flags *DiffFlags) setUnchanged(unchanged bool) {
flags.unchanged = unchanged
}

func (flags *DiffFlags) addExcludeElements(element string) {
flags.excludeElements = append(flags.excludeElements, element)
}
Expand Down
1 change: 1 addition & 0 deletions internal/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Flags interface {

setBase(source *load.Source)
setRevision(source *load.Source)
setUnchanged(bool)

addExcludeElements(string)

Expand Down

0 comments on commit a90fd08

Please sign in to comment.