From c6a5e2e969e2fe45ecad54f76c41098ac7d7f7d5 Mon Sep 17 00:00:00 2001 From: Iris Chen <10179943+iyabchen@users.noreply.github.com> Date: Fri, 5 Apr 2024 07:43:33 -0700 Subject: [PATCH] fix: fix breaking change not detecting adding max_size to array (#10348) --- tools/diff-processor/rules/rules_field.go | 14 +++++++---- .../diff-processor/rules/rules_field_test.go | 24 +++++++++++++++---- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/tools/diff-processor/rules/rules_field.go b/tools/diff-processor/rules/rules_field.go index 793a768fedf0..d4208b7531f2 100644 --- a/tools/diff-processor/rules/rules_field.go +++ b/tools/diff-processor/rules/rules_field.go @@ -178,8 +178,11 @@ func fieldRule_GrowingMin_func(old, new *schema.Schema, mc MessageContext) strin return "" } message := mc.message - if old.MinItems < new.MinItems { + if old.MinItems < new.MinItems || old.MinItems == 0 && new.MinItems > 0 { oldMin := fmt.Sprint(old.MinItems) + if old.MinItems == 0 { + oldMin = "unset" + } newMin := fmt.Sprint(new.MinItems) message = strings.ReplaceAll(message, "{{oldMin}}", oldMin) message = strings.ReplaceAll(message, "{{newMin}}", newMin) @@ -202,9 +205,12 @@ func fieldRule_ShrinkingMax_func(old, new *schema.Schema, mc MessageContext) str return "" } message := mc.message - if old.MaxItems > new.MaxItems { - oldMax := fmt.Sprint(old.MinItems) - newMax := fmt.Sprint(new.MinItems) + if old.MaxItems > new.MaxItems || old.MaxItems == 0 && new.MaxItems > 0 { + oldMax := fmt.Sprint(old.MaxItems) + if old.MaxItems == 0 { + oldMax = "unset" + } + newMax := fmt.Sprint(new.MaxItems) message = strings.ReplaceAll(message, "{{oldMax}}", oldMax) message = strings.ReplaceAll(message, "{{newMax}}", newMax) return populateMessageContext(message, mc) diff --git a/tools/diff-processor/rules/rules_field_test.go b/tools/diff-processor/rules/rules_field_test.go index e19fd13cbc4f..fe6ff9577f86 100644 --- a/tools/diff-processor/rules/rules_field_test.go +++ b/tools/diff-processor/rules/rules_field_test.go @@ -435,7 +435,7 @@ var fieldRule_GrowingMinTestCases = []fieldTestCase{ MinItems: 1, }, newField: &schema.Schema{ - MaxItems: 1, + MinItems: 1, }, expectedViolation: false, }, @@ -469,7 +469,7 @@ var fieldRule_GrowingMinTestCases = []fieldTestCase{ name: "field added", oldField: nil, newField: &schema.Schema{ - MaxItems: 1, + MinItems: 1, }, expectedViolation: false, }, @@ -481,6 +481,14 @@ var fieldRule_GrowingMinTestCases = []fieldTestCase{ newField: nil, expectedViolation: false, }, + { + name: "min unset to defined", + oldField: &schema.Schema{}, + newField: &schema.Schema{ + MinItems: 2, + }, + expectedViolation: true, + }, } func TestFieldRule_ShrinkingMax(t *testing.T) { @@ -493,7 +501,7 @@ var fieldRule_ShrinkingMaxTestCases = []fieldTestCase{ { name: "control:max - static", oldField: &schema.Schema{ - MinItems: 2, + MaxItems: 2, }, newField: &schema.Schema{ MaxItems: 2, @@ -537,11 +545,19 @@ var fieldRule_ShrinkingMaxTestCases = []fieldTestCase{ { name: "field removed", oldField: &schema.Schema{ - MinItems: 2, + MaxItems: 2, }, newField: nil, expectedViolation: false, }, + { + name: "max unset to defined", + oldField: &schema.Schema{}, + newField: &schema.Schema{ + MaxItems: 2, + }, + expectedViolation: true, + }, } func (tc *fieldTestCase) check(rule FieldRule, t *testing.T) {