Skip to content

Commit

Permalink
Fix a bug where schema validation looses type information if the inpu…
Browse files Browse the repository at this point in the history
…t has an any in it (#827)
  • Loading branch information
nolag authored Oct 7, 2024
1 parent 298546d commit 93c2fb8
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 22 deletions.
70 changes: 50 additions & 20 deletions pkg/workflows/sdk/compute_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions pkg/workflows/sdk/gen/compute.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,15 @@ func Compute{{.}}[{{range RangeNum .}}I{{.}} any, {{ end }}O any](w *WorkflowSp
return capabilities.CapabilityResponse{}, err
}

// verify against any schema
// verify against any schema by marshalling and unmarshalling
ji, err := json.Marshal(inputs)
if err != nil {
return capabilities.CapabilityResponse{}, err
}
if err := json.Unmarshal(ji, &inputs); err != nil {

// use a temp variable to unmarshal to avoid type loss if the inputs has an any in it
var tmp runtime{{.}}Inputs[{{range RangeNum . }}I{{.}},{{ end }}]
if err := json.Unmarshal(ji, &tmp); err != nil {
return capabilities.CapabilityResponse{}, err
}

Expand Down
35 changes: 35 additions & 0 deletions pkg/workflows/sdk/testutils/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package testutils_test
import (
"context"
"errors"
"fmt"
"reflect"
"strconv"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -253,6 +255,39 @@ func TestRunner(t *testing.T) {
})
}

func TestCompute(t *testing.T) {
t.Run("Inputs don't loose integer types when any is deserialized to", func(t *testing.T) {
workflow := sdk.NewWorkflowSpecFactory(sdk.NewWorkflowParams{Name: "name", Owner: "owner"})
trigger := basictrigger.TriggerConfig{Name: "foo", Number: 100}.New(workflow)
toMap := sdk.Compute1(workflow, "tomap", sdk.Compute1Inputs[string]{Arg0: trigger.CoolOutput()}, func(runtime sdk.Runtime, i0 string) (map[string]any, error) {
v, err := strconv.Atoi(i0)
if err != nil {
return nil, err
}

return map[string]any{"a": int64(v)}, nil
})

sdk.Compute1(workflow, "compute", sdk.Compute1Inputs[map[string]any]{Arg0: toMap.Value()}, func(runtime sdk.Runtime, input map[string]any) (any, error) {
actual := input["a"]
if int64(100) != actual {
return nil, fmt.Errorf("expected uint64(100), got %v of type %T", actual, actual)
}

return actual, nil
})

runner := testutils.NewRunner(tests.Context(t))
basictriggertest.Trigger(runner, func() (basictrigger.TriggerOutputs, error) {
return basictrigger.TriggerOutputs{CoolOutput: "100"}, nil
})

runner.Run(workflow)

require.NoError(t, runner.Err())
})
}

func registrationWorkflow() (*sdk.WorkflowSpecFactory, map[string]any, map[string]any) {
workflow := sdk.NewWorkflowSpecFactory(sdk.NewWorkflowParams{Name: "tester", Owner: "ryan"})
testTriggerConfig := map[string]any{"something": "from nothing"}
Expand Down

0 comments on commit 93c2fb8

Please sign in to comment.