Skip to content

Commit

Permalink
DataInputSchema can only be a string and not an object
Browse files Browse the repository at this point in the history
fixes serverlessworkflow#194

Signed-off-by: Spolti <filippespolti@gmail.com>
  • Loading branch information
spolti committed Nov 29, 2023
1 parent d19b014 commit 190bf31
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 9 deletions.
14 changes: 11 additions & 3 deletions model/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package model

import (
"bytes"
"encoding/json"

"github.com/serverlessworkflow/sdk-go/v2/util"
Expand Down Expand Up @@ -222,7 +223,8 @@ func (w *Workflow) ApplyDefault() {
w.ExpressionLang = JqExpressionLang
}

// +kubebuilder:validation:MinItems=1
// States ...
// +kubebuilder:validation:MinItems=1\
type States []State

type statesUnmarshal States
Expand Down Expand Up @@ -505,7 +507,7 @@ type StateDataFilter struct {
// DataInputSchema Used to validate the workflow data input against a defined JSON Schema
type DataInputSchema struct {
// +kubebuilder:validation:Required
Schema string `json:"schema" validate:"required"`
Schema Object `json:"schema" validate:"required"`
// +kubebuilder:validation:Required
FailOnValidationErrors bool `json:"failOnValidationErrors"`
}
Expand All @@ -515,7 +517,13 @@ type dataInputSchemaUnmarshal DataInputSchema
// UnmarshalJSON implements json.Unmarshaler
func (d *DataInputSchema) UnmarshalJSON(data []byte) error {
d.ApplyDefault()
return util.UnmarshalPrimitiveOrObject("dataInputSchema", data, &d.Schema, (*dataInputSchemaUnmarshal)(d))
if data[0] == '"' && len(data) > 0 {
replaced := bytes.Replace(data, []byte(`"`), []byte(``), -1)
d.Schema = FromString(string(replaced))
} else {
return util.UnmarshalObject("dataInputSchema", data, (*dataInputSchemaUnmarshal)(d))
}
return nil
}

// ApplyDefault set the default values for Data Input Schema
Expand Down
6 changes: 3 additions & 3 deletions model/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ func TestDataInputSchemaUnmarshalJSON(t *testing.T) {
desp: "string success",
data: `"schema name"`,
expect: DataInputSchema{
Schema: "schema name",
Schema: FromString("schema name"),
FailOnValidationErrors: true,
},
err: ``,
Expand All @@ -517,7 +517,7 @@ func TestDataInputSchemaUnmarshalJSON(t *testing.T) {
desp: `object success`,
data: `{"schema": "schema name"}`,
expect: DataInputSchema{
Schema: "schema name",
Schema: FromString("schema name"),
FailOnValidationErrors: true,
},
err: ``,
Expand All @@ -526,7 +526,7 @@ func TestDataInputSchemaUnmarshalJSON(t *testing.T) {
desp: `object fail`,
data: `{"schema": "schema name}`,
expect: DataInputSchema{
Schema: "schema name",
Schema: FromString("schema name"),
FailOnValidationErrors: true,
},
err: `unexpected end of JSON input`,
Expand Down
6 changes: 5 additions & 1 deletion model/workflow_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,10 @@ func TestDataInputSchemaStructLevelValidation(t *testing.T) {
buildFunctionRef(baseWorkflow, action1, "function 1")

testCases := []ValidationCase{
// TODO Empty DataInoputSchema will have this instead nil:
// &{Schema:{Type:0 StringValue: IntValue:0 FloatValue:0 MapValue:map[] SliceValue:[] BoolValue:false}
// We can, make Schema pointer, or, find a way to make all fields from Object as pointer.
// Using Schema: FromNull does have the same effect than just not set it.
{
Desp: "empty DataInputSchema",
Model: func() Workflow {
Expand All @@ -440,7 +444,7 @@ func TestDataInputSchemaStructLevelValidation(t *testing.T) {
Model: func() Workflow {
model := baseWorkflow.DeepCopy()
model.DataInputSchema = &DataInputSchema{
Schema: "sample schema",
Schema: FromString("sample schema"),
}
return *model
},
Expand Down
3 changes: 2 additions & 1 deletion model/zz_generated.deepcopy.go

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

16 changes: 15 additions & 1 deletion parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package parser

import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -583,7 +584,20 @@ func TestFromFile(t *testing.T) {
"./testdata/workflows/dataInputSchemaValidation.yaml", func(t *testing.T, w *model.Workflow) {
assert.NotNil(t, w.DataInputSchema)

assert.Equal(t, "sample schema", w.DataInputSchema.Schema)
assert.Equal(t, model.FromString("sample schema"), w.DataInputSchema.Schema)
assert.Equal(t, false, w.DataInputSchema.FailOnValidationErrors)
},
}, {
"./testdata/workflows/dataInputSchemaObject.json", func(t *testing.T, w *model.Workflow) {
assert.NotNil(t, w.DataInputSchema)
expected := model.Object{}
err := json.Unmarshal([]byte("{\"title\": \"Hello World Schema\", \"properties\": {\"person\": "+
"{\"type\": \"object\",\"properties\": {\"name\": {\"type\": \"string\"}},\"required\": "+
"[\"name\"]}}, \"required\": [\"person\"]}"),
&expected)
fmt.Printf("err: %s\n", err)
fmt.Printf("schema: %+v\n", expected)
assert.Equal(t, expected, w.DataInputSchema.Schema)
assert.Equal(t, false, w.DataInputSchema.FailOnValidationErrors)
},
},
Expand Down
56 changes: 56 additions & 0 deletions parser/testdata/workflows/dataInputSchemaObject.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"id": "greeting",
"version": "1.0.0",
"specVersion": "0.8",
"name": "Greeting Workflow",
"description": "Greet Someone",
"start": "Greet",
"dataInputSchema": {
"failOnValidationErrors": false,
"schema": {
"title": "Hello World Schema",
"properties": {
"person": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
},
"required": [
"name"
]
}
},
"required": [
"person"
]
}
},
"functions": [
{
"name": "greetingFunction",
"operation": "file://myapis/greetingapis.json#greeting"
}
],
"states": [
{
"name": "Greet",
"type": "operation",
"actions": [
{
"functionRef": {
"refName": "greetingFunction",
"arguments": {
"name": "${ .person.name }"
}
},
"actionDataFilter": {
"results": "${ {greeting: .greeting} }"
}
}
],
"end": true
}
]
}

0 comments on commit 190bf31

Please sign in to comment.