fix: treat empty and nil Resources the same in validation

- Empty Resources slice is now treated the same as nil (skip validation)
- Only validate Resources when it has items (not nil and not empty)
- Update test to expect success for empty resources list
- This aligns with treating empty as using the old API path
This commit is contained in:
Roberto Jimenez Sanchez
2025-12-02 23:35:02 +01:00
parent 42f18eb48d
commit 20bee04c48
2 changed files with 8 additions and 10 deletions
+4 -7
View File
@@ -101,13 +101,10 @@ func validateExportJobOptions(opts *provisioning.ExportJobOptions) field.ErrorLi
}
// Validate resources if specified
// Only validate Resources when it's explicitly provided (not nil).
// When Resources is nil, the old API path (using Folder) is used, so we skip validation.
// When Resources is explicitly provided as empty slice [], we must validate it's not empty.
if opts.Resources != nil {
if len(opts.Resources) == 0 {
list = append(list, field.Required(field.NewPath("spec", "push", "resources"), "resources list cannot be empty when specified"))
}
// Only validate Resources when it has items (not nil and not empty).
// When Resources is nil or empty, the old API path (using Folder) is used, so we skip validation.
// Empty and nil slices are treated the same for validation purposes.
if opts.Resources != nil && len(opts.Resources) > 0 {
for i, r := range opts.Resources {
resourcePath := field.NewPath("spec", "push", "resources").Index(i)
@@ -394,7 +394,8 @@ func TestIntegrationProvisioning_ExportSpecificResourcesEmptyList(t *testing.T)
}
helper.CreateRepo(t, testRepo)
// Try to export with empty resources list (should fail validation)
// Try to export with empty resources list (should succeed, as empty is treated same as nil)
// Empty resources list means using the old API path (using Folder), so validation is skipped
spec := provisioning.JobSpec{
Action: provisioning.JobActionPush,
Push: &provisioning.ExportJobOptions{
@@ -402,7 +403,7 @@ func TestIntegrationProvisioning_ExportSpecificResourcesEmptyList(t *testing.T)
},
}
// This should fail with validation error
// This should succeed (empty resources is treated same as nil, skipping validation)
body := asJSON(spec)
result := helper.AdminREST.Post().
Namespace("default").
@@ -414,5 +415,5 @@ func TestIntegrationProvisioning_ExportSpecificResourcesEmptyList(t *testing.T)
Do(ctx)
err := result.Error()
require.Error(t, err, "should fail validation when resources list is empty")
require.NoError(t, err, "empty resources list should be treated same as nil and skip validation")
}