Skip to content

Commit 45ad34a

Browse files
committed
DataInputSchema can only be a string and not an object
fixes #194 Signed-off-by: Spolti <filippespolti@gmail.com>
1 parent b1933e7 commit 45ad34a

File tree

6 files changed

+100
-17
lines changed

6 files changed

+100
-17
lines changed

model/workflow.go

Lines changed: 10 additions & 2 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"
@@ -225,6 +226,7 @@ func (w *Workflow) UnmarshalJSON(data []byte) error {
225226
return nil
226227
}
227228

229+
// States ...
228230
// +kubebuilder:validation:MinItems=1
229231
type States []State
230232

@@ -510,7 +512,7 @@ type StateDataFilter struct {
510512
// +builder-gen:new-call=ApplyDefault
511513
type DataInputSchema struct {
512514
// +kubebuilder:validation:Required
513-
Schema string `json:"schema" validate:"required"`
515+
Schema Object `json:"schema" validate:"required"`
514516
// +kubebuilder:validation:Required
515517
FailOnValidationErrors bool `json:"failOnValidationErrors"`
516518
}
@@ -520,7 +522,13 @@ type dataInputSchemaUnmarshal DataInputSchema
520522
// UnmarshalJSON implements json.Unmarshaler
521523
func (d *DataInputSchema) UnmarshalJSON(data []byte) error {
522524
d.ApplyDefault()
523-
return util.UnmarshalPrimitiveOrObject("dataInputSchema", data, &d.Schema, (*dataInputSchemaUnmarshal)(d))
525+
if data[0] == '"' && len(data) > 0 {
526+
replaced := bytes.Replace(data, []byte(`"`), []byte(``), -1)
527+
d.Schema = FromString(string(replaced))
528+
} else {
529+
return util.UnmarshalObject("dataInputSchema", data, (*dataInputSchemaUnmarshal)(d))
530+
}
531+
return nil
524532
}
525533

526534
// 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
@@ -510,7 +510,7 @@ func TestDataInputSchemaUnmarshalJSON(t *testing.T) {
510510
desp: "string success",
511511
data: `"schema name"`,
512512
expect: DataInputSchema{
513-
Schema: "schema name",
513+
Schema: FromString("schema name"),
514514
FailOnValidationErrors: true,
515515
},
516516
err: ``,
@@ -519,7 +519,7 @@ func TestDataInputSchemaUnmarshalJSON(t *testing.T) {
519519
desp: `object success`,
520520
data: `{"schema": "schema name"}`,
521521
expect: DataInputSchema{
522-
Schema: "schema name",
522+
Schema: FromString("schema name"),
523523
FailOnValidationErrors: true,
524524
},
525525
err: ``,
@@ -528,7 +528,7 @@ func TestDataInputSchemaUnmarshalJSON(t *testing.T) {
528528
desp: `object fail`,
529529
data: `{"schema": "schema name}`,
530530
expect: DataInputSchema{
531-
Schema: "schema name",
531+
Schema: FromString("schema name"),
532532
FailOnValidationErrors: true,
533533
},
534534
err: `unexpected end of JSON input`,

model/workflow_validator_test.go

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

428428
testCases := []ValidationCase{
429-
{
430-
Desp: "empty DataInputSchema",
431-
Model: func() Workflow {
432-
model := baseWorkflow.DeepCopy()
433-
model.DataInputSchema = &DataInputSchema{}
434-
return *model
435-
},
436-
Err: `workflow.dataInputSchema.schema is required`,
437-
},
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.
433+
//{
434+
// Desp: "empty DataInputSchema",
435+
// Model: func() Workflow {
436+
// model := baseWorkflow.DeepCopy()
437+
// model.DataInputSchema = &DataInputSchema{}
438+
// return *model
439+
// },
440+
// Err: `workflow.dataInputSchema.schema is required`,
441+
//},
438442
{
439443
Desp: "filled Schema, default failOnValidationErrors",
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)