Skip to content

Commit ba97125

Browse files
martelclaude
andauthored
fix(diff): compare MaxLength, not MinLength, in CheckStringTypeChanges (#253)
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. Signed-off-by: Mike Minicki <martel@post.pl> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent ba856fe commit ba97125

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

‎diff/checks.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ func CheckStringTypeChanges(diffs []TypeDiff, type1, type2 *spec.SchemaProps) []
225225
type2.Type[0] == StringType {
226226
minLengthDiffs := CompareIntValues("MinLength", type1.MinLength, type2.MinLength, NarrowedType, WidenedType)
227227
diffs = append(diffs, minLengthDiffs...)
228-
maxLengthDiffs := CompareIntValues("MaxLength", type1.MinLength, type2.MinLength, WidenedType, NarrowedType)
228+
maxLengthDiffs := CompareIntValues("MaxLength", type1.MaxLength, type2.MaxLength, WidenedType, NarrowedType)
229229
diffs = append(diffs, maxLengthDiffs...)
230230
if type1.Pattern != type2.Pattern {
231231
diffs = addTypeDiff(diffs, TypeDiff{Change: ChangedType, Description: fmt.Sprintf("Pattern Changed:%s->%s", type1.Pattern, type2.Pattern)})

‎diff/checks_test.go‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,60 @@ func Test_checkNumericTypeChanges(t *testing.T) {
231231
}
232232
}
233233

234+
func TestCheckStringTypeChanges(t *testing.T) {
235+
tests := []struct {
236+
name string
237+
type1 *spec.SchemaProps
238+
type2 *spec.SchemaProps
239+
want []TypeDiff
240+
}{
241+
{
242+
name: "MaxLength added",
243+
type1: &spec.StringProperty().SchemaProps,
244+
type2: &spec.StringProperty().WithMaxLength(255).SchemaProps,
245+
want: []TypeDiff{{Change: AddedConstraint, Description: "MaxLength(255)"}},
246+
},
247+
{
248+
name: "MinLength and MaxLength added",
249+
type1: &spec.StringProperty().SchemaProps,
250+
type2: &spec.StringProperty().WithMinLength(1).WithMaxLength(255).SchemaProps,
251+
want: []TypeDiff{
252+
{Change: AddedConstraint, Description: "MinLength(1)"},
253+
{Change: AddedConstraint, Description: "MaxLength(255)"},
254+
},
255+
},
256+
{
257+
name: "MaxLength narrowed",
258+
type1: &spec.StringProperty().WithMaxLength(1000).SchemaProps,
259+
type2: &spec.StringProperty().WithMaxLength(255).SchemaProps,
260+
want: []TypeDiff{{Change: NarrowedType, Description: "MaxLength 1000->255"}},
261+
},
262+
{
263+
name: "MaxLength widened",
264+
type1: &spec.StringProperty().WithMaxLength(255).SchemaProps,
265+
type2: &spec.StringProperty().WithMaxLength(1000).SchemaProps,
266+
want: []TypeDiff{{Change: WidenedType, Description: "MaxLength 255->1000"}},
267+
},
268+
{
269+
name: "MaxLength removed",
270+
type1: &spec.StringProperty().WithMaxLength(255).SchemaProps,
271+
type2: &spec.StringProperty().SchemaProps,
272+
want: []TypeDiff{{Change: DeletedConstraint, Description: "MaxLength(255)"}},
273+
},
274+
{
275+
name: "MinLength changed with MaxLength unchanged",
276+
type1: &spec.StringProperty().WithMinLength(1).WithMaxLength(255).SchemaProps,
277+
type2: &spec.StringProperty().WithMinLength(2).WithMaxLength(255).SchemaProps,
278+
want: []TypeDiff{{Change: NarrowedType, Description: "MinLength 1->2"}},
279+
},
280+
}
281+
for _, tt := range tests {
282+
t.Run(tt.name, func(t *testing.T) {
283+
assert.Equal(t, tt.want, CheckStringTypeChanges(nil, tt.type1, tt.type2))
284+
})
285+
}
286+
}
287+
234288
type compareValueCase struct {
235289
name string
236290
fieldName string

0 commit comments

Comments
 (0)