Skip to content

Commit

Permalink
fix: fix breaking change not detecting adding max_size to array (#10348)
Browse files Browse the repository at this point in the history
  • Loading branch information
iyabchen authored Apr 5, 2024
1 parent e94c560 commit c6a5e2e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
14 changes: 10 additions & 4 deletions tools/diff-processor/rules/rules_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
24 changes: 20 additions & 4 deletions tools/diff-processor/rules/rules_field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ var fieldRule_GrowingMinTestCases = []fieldTestCase{
MinItems: 1,
},
newField: &schema.Schema{
MaxItems: 1,
MinItems: 1,
},
expectedViolation: false,
},
Expand Down Expand Up @@ -469,7 +469,7 @@ var fieldRule_GrowingMinTestCases = []fieldTestCase{
name: "field added",
oldField: nil,
newField: &schema.Schema{
MaxItems: 1,
MinItems: 1,
},
expectedViolation: false,
},
Expand All @@ -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) {
Expand All @@ -493,7 +501,7 @@ var fieldRule_ShrinkingMaxTestCases = []fieldTestCase{
{
name: "control:max - static",
oldField: &schema.Schema{
MinItems: 2,
MaxItems: 2,
},
newField: &schema.Schema{
MaxItems: 2,
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit c6a5e2e

Please sign in to comment.