From 3f82db0e55dd0832fa6347ecc7b32cda49338a33 Mon Sep 17 00:00:00 2001 From: Leonardo Di Giovanna Date: Fri, 6 Dec 2024 10:12:30 +0100 Subject: [PATCH] fix(decl/loader): fix field bindings marshaling for nested fields Signed-off-by: Leonardo Di Giovanna Co-authored-by: Aldo Lacuku --- pkg/test/loader/loader.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/pkg/test/loader/loader.go b/pkg/test/loader/loader.go index e04264ad..96c0255c 100644 --- a/pkg/test/loader/loader.go +++ b/pkg/test/loader/loader.go @@ -20,6 +20,7 @@ import ( "io" "reflect" "regexp" + "strings" "github.com/go-playground/validator/v10" "github.com/go-viper/mapstructure/v2" @@ -579,9 +580,23 @@ func (s TestStep) MarshalYAML() (any, error) { for arg, argValue := range spec.Args { args[arg] = argValue } + + // Restore field bindings in the original locations. for _, fieldBinding := range s.FieldBindings { - args[fieldBinding.LocalField] = fmt.Sprintf("${%s.%s}", fieldBinding.SrcStep, fieldBinding.SrcField) + localFieldSegments := strings.Split(fieldBinding.LocalField, ".") + fieldContainer := args + for _, fieldSegment := range localFieldSegments[:len(localFieldSegments)-1] { + value, ok := fieldContainer[fieldSegment] + if !ok { + value = map[string]any{} + fieldContainer[fieldSegment] = value + } + fieldContainer = value.(map[string]any) + } + fieldContainer[localFieldSegments[len(localFieldSegments)-1]] = fmt.Sprintf("${%s.%s}", + fieldBinding.SrcStep, fieldBinding.SrcField) } + return struct { Type TestStepType `yaml:"type"` Name string `yaml:"name"`