Skip to content

Commit

Permalink
new required params are breaking
Browse files Browse the repository at this point in the history
  • Loading branch information
Reuven committed Jan 2, 2022
1 parent 15a9fd2 commit 51c46df
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 17 deletions.
78 changes: 64 additions & 14 deletions diff/diff_breaking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,65 @@ func TestCompareWithDefault_Nil(t *testing.T) {
)
}

func TestBreaking_NewPathParam(t *testing.T) {
s1 := l(t, 1)
s2 := l(t, 1)

s1.Paths[installCommandPath].Get.Parameters.GetByInAndName(openapi3.ParameterInPath, "domain").Name = ""

d, err := diff.Get(&diff.Config{
BreakingOnly: true,
}, s1, s2)
require.NoError(t, err)

require.Contains(t,
d.PathsDiff.Modified[installCommandPath].OperationsDiff.Modified["GET"].ParametersDiff.Added[openapi3.ParameterInPath],
"domain")
}

func TestBreaking_NewRequiredHeaderParam(t *testing.T) {
s1 := l(t, 1)
s2 := l(t, 1)

s1.Paths[installCommandPath].Get.Parameters.GetByInAndName(openapi3.ParameterInHeader, "network-policies").Name = ""
s2.Paths[installCommandPath].Get.Parameters.GetByInAndName(openapi3.ParameterInHeader, "network-policies").Required = true

d, err := diff.Get(&diff.Config{
BreakingOnly: true,
}, s1, s2)
require.NoError(t, err)

require.Contains(t,
d.PathsDiff.Modified[installCommandPath].OperationsDiff.Modified["GET"].ParametersDiff.Added[openapi3.ParameterInHeader],
"network-policies")
}

func TestBreaking_NewNoneRequiredHeaderParam(t *testing.T) {
s1 := l(t, 1)
s2 := l(t, 1)

s1.Paths[installCommandPath].Get.Parameters.GetByInAndName(openapi3.ParameterInHeader, "network-policies").Name = ""
s2.Paths[installCommandPath].Get.Parameters.GetByInAndName(openapi3.ParameterInHeader, "network-policies").Required = false

d, err := diff.Get(&diff.Config{
BreakingOnly: true,
}, s1, s2)
require.NoError(t, err)

require.NotContains(t,
d.PathsDiff.Modified[installCommandPath].OperationsDiff.Modified["GET"].ParametersDiff.Added[openapi3.ParameterInPath],
"network-policies")
}

func TestBreaking_MaxLengthSmaller(t *testing.T) {
s1 := l(t, 1)
s2 := l(t, 1)

maxLengthFrom := uint64(13)
s1.Paths["/api/{domain}/{project}/install-command"].Get.Parameters.GetByInAndName("path", "domain").Schema.Value.MaxLength = &maxLengthFrom
s1.Paths[installCommandPath].Get.Parameters.GetByInAndName(openapi3.ParameterInPath, "domain").Schema.Value.MaxLength = &maxLengthFrom

maxLengthTo := uint64(11)
s2.Paths["/api/{domain}/{project}/install-command"].Get.Parameters.GetByInAndName("path", "domain").Schema.Value.MaxLength = &maxLengthTo
s2.Paths[installCommandPath].Get.Parameters.GetByInAndName(openapi3.ParameterInPath, "domain").Schema.Value.MaxLength = &maxLengthTo

d, err := diff.Get(&diff.Config{
BreakingOnly: true,
Expand All @@ -101,10 +151,10 @@ func TestBreaking_MaxLengthGreater(t *testing.T) {
s2 := l(t, 1)

maxLengthFrom := uint64(13)
s1.Paths["/api/{domain}/{project}/install-command"].Get.Parameters.GetByInAndName("path", "domain").Schema.Value.MaxLength = &maxLengthFrom
s1.Paths[installCommandPath].Get.Parameters.GetByInAndName(openapi3.ParameterInPath, "domain").Schema.Value.MaxLength = &maxLengthFrom

maxLengthTo := uint64(14)
s2.Paths["/api/{domain}/{project}/install-command"].Get.Parameters.GetByInAndName("path", "domain").Schema.Value.MaxLength = &maxLengthTo
s2.Paths[installCommandPath].Get.Parameters.GetByInAndName(openapi3.ParameterInPath, "domain").Schema.Value.MaxLength = &maxLengthTo

d, err := diff.Get(&diff.Config{
BreakingOnly: true,
Expand All @@ -117,10 +167,10 @@ func TestBreaking_MaxLengthFromNil(t *testing.T) {
s1 := l(t, 1)
s2 := l(t, 1)

s1.Paths["/api/{domain}/{project}/install-command"].Get.Parameters.GetByInAndName("path", "domain").Schema.Value.MaxLength = nil
s1.Paths[installCommandPath].Get.Parameters.GetByInAndName(openapi3.ParameterInPath, "domain").Schema.Value.MaxLength = nil

maxLengthTo := uint64(14)
s2.Paths["/api/{domain}/{project}/install-command"].Get.Parameters.GetByInAndName("path", "domain").Schema.Value.MaxLength = &maxLengthTo
s2.Paths[installCommandPath].Get.Parameters.GetByInAndName(openapi3.ParameterInPath, "domain").Schema.Value.MaxLength = &maxLengthTo

d, err := diff.Get(&diff.Config{
BreakingOnly: true,
Expand All @@ -134,9 +184,9 @@ func TestBreaking_MaxLengthToNil(t *testing.T) {
s2 := l(t, 1)

maxLengthFrom := uint64(13)
s1.Paths["/api/{domain}/{project}/install-command"].Get.Parameters.GetByInAndName("path", "domain").Schema.Value.MaxLength = &maxLengthFrom
s1.Paths[installCommandPath].Get.Parameters.GetByInAndName(openapi3.ParameterInPath, "domain").Schema.Value.MaxLength = &maxLengthFrom

s2.Paths["/api/{domain}/{project}/install-command"].Get.Parameters.GetByInAndName("path", "domain").Schema.Value.MaxLength = nil
s2.Paths[installCommandPath].Get.Parameters.GetByInAndName(openapi3.ParameterInPath, "domain").Schema.Value.MaxLength = nil

d, err := diff.Get(&diff.Config{
BreakingOnly: true,
Expand All @@ -149,8 +199,8 @@ func TestBreaking_MaxLengthBothNil(t *testing.T) {
s1 := l(t, 1)
s2 := l(t, 1)

s1.Paths["/api/{domain}/{project}/install-command"].Get.Parameters.GetByInAndName("path", "domain").Schema.Value.MaxLength = nil
s2.Paths["/api/{domain}/{project}/install-command"].Get.Parameters.GetByInAndName("path", "domain").Schema.Value.MaxLength = nil
s1.Paths[installCommandPath].Get.Parameters.GetByInAndName(openapi3.ParameterInPath, "domain").Schema.Value.MaxLength = nil
s2.Paths[installCommandPath].Get.Parameters.GetByInAndName(openapi3.ParameterInPath, "domain").Schema.Value.MaxLength = nil

d, err := diff.Get(&diff.Config{
BreakingOnly: true,
Expand All @@ -163,8 +213,8 @@ func TestBreaking_MinItemsSmaller(t *testing.T) {
s1 := l(t, 1)
s2 := l(t, 1)

s1.Paths["/api/{domain}/{project}/install-command"].Get.Parameters.GetByInAndName("path", "domain").Schema.Value.MinItems = 13
s2.Paths["/api/{domain}/{project}/install-command"].Get.Parameters.GetByInAndName("path", "domain").Schema.Value.MinItems = 11
s1.Paths[installCommandPath].Get.Parameters.GetByInAndName(openapi3.ParameterInPath, "domain").Schema.Value.MinItems = 13
s2.Paths[installCommandPath].Get.Parameters.GetByInAndName(openapi3.ParameterInPath, "domain").Schema.Value.MinItems = 11

d, err := diff.Get(&diff.Config{
BreakingOnly: true,
Expand All @@ -177,8 +227,8 @@ func TestBreaking_MinItemsGreater(t *testing.T) {
s1 := l(t, 1)
s2 := l(t, 1)

s1.Paths["/api/{domain}/{project}/install-command"].Get.Parameters.GetByInAndName("path", "domain").Schema.Value.MinItems = 13
s2.Paths["/api/{domain}/{project}/install-command"].Get.Parameters.GetByInAndName("path", "domain").Schema.Value.MinItems = 14
s1.Paths[installCommandPath].Get.Parameters.GetByInAndName(openapi3.ParameterInPath, "domain").Schema.Value.MinItems = 13
s2.Paths[installCommandPath].Get.Parameters.GetByInAndName(openapi3.ParameterInPath, "domain").Schema.Value.MinItems = 14

d, err := diff.Get(&diff.Config{
BreakingOnly: true,
Expand Down
25 changes: 22 additions & 3 deletions diff/parameters_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,32 @@ func (parametersDiff *ParametersDiff) Empty() bool {
len(parametersDiff.Modified) == 0
}

func (parametersDiff *ParametersDiff) removeNonBreaking() {
func (parametersDiff *ParametersDiff) removeNonBreaking(params2 openapi3.Parameters) {

if parametersDiff.Empty() {
return
}

parametersDiff.Added = nil
parametersDiff.removeAddedButNonRequired(params2)
}

func (parametersDiff *ParametersDiff) removeAddedButNonRequired(params2 openapi3.Parameters) {
for location, paramNames := range parametersDiff.Added {
newList := StringList{}
for _, paramName := range paramNames {
if param := params2.GetByInAndName(location, paramName); param != nil {
if param.Required || param.In == openapi3.ParameterInPath {
newList = append(newList, paramName)
}
}
}

if len(newList) > 0 {
parametersDiff.Added[location] = newList
} else {
delete(parametersDiff.Added, location)
}
}
}

// ParamNamesByLocation maps param location (path, query, header or cookie) to the params in this location
Expand Down Expand Up @@ -84,7 +103,7 @@ func getParametersDiff(config *Config, params1, params2 openapi3.Parameters) (*P
}

if config.BreakingOnly {
diff.removeNonBreaking()
diff.removeNonBreaking(params2)
}

if diff.Empty() {
Expand Down

0 comments on commit 51c46df

Please sign in to comment.