Skip to content

Commit

Permalink
Add validation for duplicated param names in TaskSpec
Browse files Browse the repository at this point in the history
Prior to this, when duplicated names for parameters were assigned in `taskSpec`, the param value would be overwritten by the last value.

To avoid this, a validation check was added to ensure that no param name is duplicated in TaskSpec.
  • Loading branch information
chitrangpatel authored and tekton-robot committed May 20, 2022
1 parent e6c7eb0 commit 3a305a0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pkg/apis/pipeline/v1beta1/task_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,12 @@ func ValidateParameterVariables(steps []Step, params []ParamSpec) *apis.FieldErr
parameterNames := sets.NewString()
arrayParameterNames := sets.NewString()
objectParamSpecs := []ParamSpec{}

var errs *apis.FieldError
for _, p := range params {
// validate no duplicate names
if parameterNames.Has(p.Name) {
errs = errs.Also(apis.ErrGeneric("parameter appears more than once", "").ViaFieldKey("params", p.Name))
}
parameterNames.Insert(p.Name)
if p.Type == ParamTypeArray {
arrayParameterNames.Insert(p.Name)
Expand All @@ -332,7 +336,7 @@ func ValidateParameterVariables(steps []Step, params []ParamSpec) *apis.FieldErr
}
}

errs := validateVariables(steps, "params", parameterNames)
errs = errs.Also(validateVariables(steps, "params", parameterNames))
errs = errs.Also(validateArrayUsage(steps, "params", arrayParameterNames))
return errs.Also(validateObjectUsage(steps, objectParamSpecs))
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/apis/pipeline/v1beta1/task_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,26 @@ func TestTaskSpecValidateError(t *testing.T) {
Paths: []string{"params"},
Details: "Names: \nMust only contain alphanumeric characters, hyphens (-), underscores (_), and dots (.)\nMust begin with a letter or an underscore (_)",
},
}, {
name: "duplicated param names",
fields: fields{
Params: []v1beta1.ParamSpec{{
Name: "foo",
Type: v1beta1.ParamTypeString,
Description: "parameter",
Default: v1beta1.NewArrayOrString("value1"),
}, {
Name: "foo",
Type: v1beta1.ParamTypeString,
Description: "parameter",
Default: v1beta1.NewArrayOrString("value2"),
}},
Steps: validSteps,
},
expectedError: apis.FieldError{
Message: `parameter appears more than once`,
Paths: []string{"params[foo]"},
},
}, {
name: "invalid param type",
fields: fields{
Expand Down

0 comments on commit 3a305a0

Please sign in to comment.