Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion diff/checks.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
// SPDX-License-Identifier: Apache-2.0

package diff

Check notice on line 4 in diff/checks.go

View check run for this annotation

codefactor.io / CodeFactor

diff/checks.go#L4

Should have a package comment. (package-comments)

import (
"fmt"
Expand Down Expand Up @@ -225,7 +225,7 @@
type2.Type[0] == StringType {
minLengthDiffs := CompareIntValues("MinLength", type1.MinLength, type2.MinLength, NarrowedType, WidenedType)
diffs = append(diffs, minLengthDiffs...)
maxLengthDiffs := CompareIntValues("MaxLength", type1.MinLength, type2.MinLength, WidenedType, NarrowedType)
maxLengthDiffs := CompareIntValues("MaxLength", type1.MaxLength, type2.MaxLength, WidenedType, NarrowedType)
diffs = append(diffs, maxLengthDiffs...)
if type1.Pattern != type2.Pattern {
diffs = addTypeDiff(diffs, TypeDiff{Change: ChangedType, Description: fmt.Sprintf("Pattern Changed:%s->%s", type1.Pattern, type2.Pattern)})
Expand Down
54 changes: 54 additions & 0 deletions diff/checks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,60 @@ func Test_checkNumericTypeChanges(t *testing.T) {
}
}

func TestCheckStringTypeChanges(t *testing.T) {
tests := []struct {
name string
type1 *spec.SchemaProps
type2 *spec.SchemaProps
want []TypeDiff
}{
{
name: "MaxLength added",
type1: &spec.StringProperty().SchemaProps,
type2: &spec.StringProperty().WithMaxLength(255).SchemaProps,
want: []TypeDiff{{Change: AddedConstraint, Description: "MaxLength(255)"}},
},
{
name: "MinLength and MaxLength added",
type1: &spec.StringProperty().SchemaProps,
type2: &spec.StringProperty().WithMinLength(1).WithMaxLength(255).SchemaProps,
want: []TypeDiff{
{Change: AddedConstraint, Description: "MinLength(1)"},
{Change: AddedConstraint, Description: "MaxLength(255)"},
},
},
{
name: "MaxLength narrowed",
type1: &spec.StringProperty().WithMaxLength(1000).SchemaProps,
type2: &spec.StringProperty().WithMaxLength(255).SchemaProps,
want: []TypeDiff{{Change: NarrowedType, Description: "MaxLength 1000->255"}},
},
{
name: "MaxLength widened",
type1: &spec.StringProperty().WithMaxLength(255).SchemaProps,
type2: &spec.StringProperty().WithMaxLength(1000).SchemaProps,
want: []TypeDiff{{Change: WidenedType, Description: "MaxLength 255->1000"}},
},
{
name: "MaxLength removed",
type1: &spec.StringProperty().WithMaxLength(255).SchemaProps,
type2: &spec.StringProperty().SchemaProps,
want: []TypeDiff{{Change: DeletedConstraint, Description: "MaxLength(255)"}},
},
{
name: "MinLength changed with MaxLength unchanged",
type1: &spec.StringProperty().WithMinLength(1).WithMaxLength(255).SchemaProps,
type2: &spec.StringProperty().WithMinLength(2).WithMaxLength(255).SchemaProps,
want: []TypeDiff{{Change: NarrowedType, Description: "MinLength 1->2"}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, CheckStringTypeChanges(nil, tt.type1, tt.type2))
})
}
}

type compareValueCase struct {
name string
fieldName string
Expand Down
Loading