From 1598990f7d8b0c741fd34ed341f78986afc0a46e Mon Sep 17 00:00:00 2001 From: Mike Minicki Date: Fri, 2 Oct 2026 12:33:00 -0700 Subject: [PATCH] fix(diff): compare MaxLength, not MinLength, in CheckStringTypeChanges CheckStringTypeChanges passed type1.MinLength and type2.MinLength to the MaxLength comparison. diff.Compare reported every minLength change a second time as MaxLength, and never reported a maxLength change, so narrowing maxLength went unflagged. Add TestCheckStringTypeChanges, which fails on the old line. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Mike Minicki --- diff/checks.go | 2 +- diff/checks_test.go | 54 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/diff/checks.go b/diff/checks.go index eac39e8..c054a54 100644 --- a/diff/checks.go +++ b/diff/checks.go @@ -225,7 +225,7 @@ func CheckStringTypeChanges(diffs []TypeDiff, type1, type2 *spec.SchemaProps) [] 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)}) diff --git a/diff/checks_test.go b/diff/checks_test.go index 38cf757..511ce8e 100644 --- a/diff/checks_test.go +++ b/diff/checks_test.go @@ -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