Skip to content

Commit 190bf31

Browse files
committed
DataInputSchema can only be a string and not an object
fixes #194 Signed-off-by: Spolti <[email protected]>
1 parent d19b014 commit 190bf31

File tree

6 files changed

+92
-9
lines changed

6 files changed

+92
-9
lines changed

model/workflow.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package model
1616

1717
import (
18+
"bytes"
1819
"encoding/json"
1920

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

225-
// +kubebuilder:validation:MinItems=1
226+
// States ...
227+
// +kubebuilder:validation:MinItems=1\
226228
type States []State
227229

228230
type statesUnmarshal States
@@ -505,7 +507,7 @@ type StateDataFilter struct {
505507
// DataInputSchema Used to validate the workflow data input against a defined JSON Schema
506508
type DataInputSchema struct {
507509
// +kubebuilder:validation:Required
508-
Schema string `json:"schema" validate:"required"`
510+
Schema Object `json:"schema" validate:"required"`
509511
// +kubebuilder:validation:Required
510512
FailOnValidationErrors bool `json:"failOnValidationErrors"`
511513
}
@@ -515,7 +517,13 @@ type dataInputSchemaUnmarshal DataInputSchema
515517
// UnmarshalJSON implements json.Unmarshaler
516518
func (d *DataInputSchema) UnmarshalJSON(data []byte) error {
517519
d.ApplyDefault()
518-
return util.UnmarshalPrimitiveOrObject("dataInputSchema", data, &d.Schema, (*dataInputSchemaUnmarshal)(d))
520+
if data[0] == '"' && len(data) > 0 {
521+
replaced := bytes.Replace(data, []byte(`"`), []byte(``), -1)
522+
d.Schema = FromString(string(replaced))
523+
} else {
524+
return util.UnmarshalObject("dataInputSchema", data, (*dataInputSchemaUnmarshal)(d))
525+
}
526+
return nil
519527
}
520528

521529
// ApplyDefault set the default values for Data Input Schema

model/workflow_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ func TestDataInputSchemaUnmarshalJSON(t *testing.T) {
508508
desp: "string success",
509509
data: `"schema name"`,
510510
expect: DataInputSchema{
511-
Schema: "schema name",
511+
Schema: FromString("schema name"),
512512
FailOnValidationErrors: true,
513513
},
514514
err: ``,
@@ -517,7 +517,7 @@ func TestDataInputSchemaUnmarshalJSON(t *testing.T) {
517517
desp: `object success`,
518518
data: `{"schema": "schema name"}`,
519519
expect: DataInputSchema{
520-
Schema: "schema name",
520+
Schema: FromString("schema name"),
521521
FailOnValidationErrors: true,
522522
},
523523
err: ``,
@@ -526,7 +526,7 @@ func TestDataInputSchemaUnmarshalJSON(t *testing.T) {
526526
desp: `object fail`,
527527
data: `{"schema": "schema name}`,
528528
expect: DataInputSchema{
529-
Schema: "schema name",
529+
Schema: FromString("schema name"),
530530
FailOnValidationErrors: true,
531531
},
532532
err: `unexpected end of JSON input`,

model/workflow_validator_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,10 @@ func TestDataInputSchemaStructLevelValidation(t *testing.T) {
426426
buildFunctionRef(baseWorkflow, action1, "function 1")
427427

428428
testCases := []ValidationCase{
429+
// TODO Empty DataInoputSchema will have this instead nil:
430+
// &{Schema:{Type:0 StringValue: IntValue:0 FloatValue:0 MapValue:map[] SliceValue:[] BoolValue:false}
431+
// We can, make Schema pointer, or, find a way to make all fields from Object as pointer.
432+
// Using Schema: FromNull does have the same effect than just not set it.
429433
{
430434
Desp: "empty DataInputSchema",
431435
Model: func() Workflow {
@@ -440,7 +444,7 @@ func TestDataInputSchemaStructLevelValidation(t *testing.T) {
440444
Model: func() Workflow {
441445
model := baseWorkflow.DeepCopy()
442446
model.DataInputSchema = &DataInputSchema{
443-
Schema: "sample schema",
447+
Schema: FromString("sample schema"),
444448
}
445449
return *model
446450
},

model/zz_generated.deepcopy.go

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

parser/parser_test.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package parser
1616

1717
import (
1818
"encoding/json"
19+
"fmt"
1920
"os"
2021
"path/filepath"
2122
"strings"
@@ -583,7 +584,20 @@ func TestFromFile(t *testing.T) {
583584
"./testdata/workflows/dataInputSchemaValidation.yaml", func(t *testing.T, w *model.Workflow) {
584585
assert.NotNil(t, w.DataInputSchema)
585586

586-
assert.Equal(t, "sample schema", w.DataInputSchema.Schema)
587+
assert.Equal(t, model.FromString("sample schema"), w.DataInputSchema.Schema)
588+
assert.Equal(t, false, w.DataInputSchema.FailOnValidationErrors)
589+
},
590+
}, {
591+
"./testdata/workflows/dataInputSchemaObject.json", func(t *testing.T, w *model.Workflow) {
592+
assert.NotNil(t, w.DataInputSchema)
593+
expected := model.Object{}
594+
err := json.Unmarshal([]byte("{\"title\": \"Hello World Schema\", \"properties\": {\"person\": "+
595+
"{\"type\": \"object\",\"properties\": {\"name\": {\"type\": \"string\"}},\"required\": "+
596+
"[\"name\"]}}, \"required\": [\"person\"]}"),
597+
&expected)
598+
fmt.Printf("err: %s\n", err)
599+
fmt.Printf("schema: %+v\n", expected)
600+
assert.Equal(t, expected, w.DataInputSchema.Schema)
587601
assert.Equal(t, false, w.DataInputSchema.FailOnValidationErrors)
588602
},
589603
},
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"id": "greeting",
3+
"version": "1.0.0",
4+
"specVersion": "0.8",
5+
"name": "Greeting Workflow",
6+
"description": "Greet Someone",
7+
"start": "Greet",
8+
"dataInputSchema": {
9+
"failOnValidationErrors": false,
10+
"schema": {
11+
"title": "Hello World Schema",
12+
"properties": {
13+
"person": {
14+
"type": "object",
15+
"properties": {
16+
"name": {
17+
"type": "string"
18+
}
19+
},
20+
"required": [
21+
"name"
22+
]
23+
}
24+
},
25+
"required": [
26+
"person"
27+
]
28+
}
29+
},
30+
"functions": [
31+
{
32+
"name": "greetingFunction",
33+
"operation": "file://myapis/greetingapis.json#greeting"
34+
}
35+
],
36+
"states": [
37+
{
38+
"name": "Greet",
39+
"type": "operation",
40+
"actions": [
41+
{
42+
"functionRef": {
43+
"refName": "greetingFunction",
44+
"arguments": {
45+
"name": "${ .person.name }"
46+
}
47+
},
48+
"actionDataFilter": {
49+
"results": "${ {greeting: .greeting} }"
50+
}
51+
}
52+
],
53+
"end": true
54+
}
55+
]
56+
}

0 commit comments

Comments
 (0)