Skip to content

Commit 02fbe11

Browse files
spoltilsytj0413
andauthored
allow empty auth definitions in parser (#111)
* allow empty auth definitions in parser fixes #110 Signed-off-by: spolti <filippespolti@gmail.com> * allow empty auth definitions in parser fixes #110 fixes #126 Signed-off-by: spolti <filippespolti@gmail.com> * fix(*): use different types to auth validate Signed-off-by: lsytj0413 <511121939@qq.com> * small change Signed-off-by: spolti <filippespolti@gmail.com> Signed-off-by: spolti <filippespolti@gmail.com> Signed-off-by: lsytj0413 <511121939@qq.com> Co-authored-by: lsytj0413 <511121939@qq.com>
1 parent 588fc1f commit 02fbe11

File tree

4 files changed

+206
-152
lines changed

4 files changed

+206
-152
lines changed

model/auth.go

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,8 @@ package model
1717
import (
1818
"encoding/json"
1919
"fmt"
20-
"reflect"
21-
22-
validator "github.com/go-playground/validator/v10"
23-
val "github.com/serverlessworkflow/sdk-go/v2/validator"
2420
)
2521

26-
func init() {
27-
val.GetValidator().RegisterStructValidation(AuthDefinitionsStructLevelValidation, AuthDefinitions{})
28-
}
29-
30-
// AuthDefinitionsStructLevelValidation custom validator for unique name of the auth methods
31-
func AuthDefinitionsStructLevelValidation(structLevel validator.StructLevel) {
32-
authDefs := structLevel.Current().Interface().(AuthDefinitions)
33-
dict := map[string]bool{}
34-
35-
for _, a := range authDefs.Defs {
36-
if !dict[a.Name] {
37-
dict[a.Name] = true
38-
} else {
39-
structLevel.ReportError(reflect.ValueOf(a.Name), "Name", "name", "reqnameunique", "")
40-
}
41-
}
42-
}
43-
44-
// AuthDefinitions used to define authentication information applied to resources defined in the operation property of function definitions
45-
type AuthDefinitions struct {
46-
Defs []Auth
47-
}
48-
4922
// AuthType ...
5023
type AuthType string
5124

@@ -92,44 +65,6 @@ type Auth struct {
9265
Properties AuthProperties `json:"properties" validate:"required"`
9366
}
9467

95-
// UnmarshalJSON implements json.Unmarshaler
96-
func (a *AuthDefinitions) UnmarshalJSON(b []byte) error {
97-
if len(b) == 0 {
98-
// TODO: Normalize error messages
99-
return fmt.Errorf("no bytes to unmarshal")
100-
}
101-
102-
// See if we can guess based on the first character
103-
switch b[0] {
104-
case '"':
105-
return a.unmarshalFile(b)
106-
case '[':
107-
return a.unmarshalMany(b)
108-
}
109-
110-
return fmt.Errorf("auth value '%s' is not supported, it must be an array or string", string(b))
111-
}
112-
113-
func (a *AuthDefinitions) unmarshalFile(data []byte) error {
114-
b, err := unmarshalFile(data)
115-
if err != nil {
116-
return fmt.Errorf("authDefinitions value '%s' is not supported, it must be an object or string", string(data))
117-
}
118-
119-
return a.unmarshalMany(b)
120-
}
121-
122-
func (a *AuthDefinitions) unmarshalMany(data []byte) error {
123-
var auths []Auth
124-
err := json.Unmarshal(data, &auths)
125-
if err != nil {
126-
return fmt.Errorf("authDefinitions value '%s' is not supported, it must be an object or string", string(data))
127-
}
128-
129-
a.Defs = auths
130-
return nil
131-
}
132-
13368
// UnmarshalJSON Auth definition
13469
func (a *Auth) UnmarshalJSON(data []byte) error {
13570
auth := make(map[string]json.RawMessage)

model/auth_test.go

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -19,81 +19,8 @@ import (
1919
"testing"
2020

2121
"github.com/stretchr/testify/assert"
22-
23-
val "github.com/serverlessworkflow/sdk-go/v2/validator"
2422
)
2523

26-
func TestAuthDefinitionsStructLevelValidation(t *testing.T) {
27-
type testCase struct {
28-
desp string
29-
authDefs AuthDefinitions
30-
err string
31-
}
32-
testCases := []testCase{
33-
{
34-
desp: "nil defs",
35-
authDefs: AuthDefinitions{
36-
Defs: nil,
37-
},
38-
err: ``,
39-
},
40-
{
41-
desp: "zero length defs",
42-
authDefs: AuthDefinitions{
43-
Defs: []Auth{},
44-
},
45-
err: ``,
46-
},
47-
{
48-
desp: "multi unique defs",
49-
authDefs: AuthDefinitions{
50-
Defs: []Auth{
51-
{
52-
Name: "1",
53-
},
54-
{
55-
Name: "2",
56-
},
57-
{
58-
Name: "3",
59-
},
60-
},
61-
},
62-
err: ``,
63-
},
64-
{
65-
desp: "multi non-unique defs",
66-
authDefs: AuthDefinitions{
67-
Defs: []Auth{
68-
{
69-
Name: "1",
70-
},
71-
{
72-
Name: "2",
73-
},
74-
{
75-
Name: "1",
76-
},
77-
},
78-
},
79-
err: `Key: 'AuthDefinitions.Name' Error:Field validation for 'Name' failed on the 'reqnameunique' tag`,
80-
},
81-
}
82-
for _, tc := range testCases {
83-
t.Run(tc.desp, func(t *testing.T) {
84-
err := val.GetValidator().Struct(tc.authDefs)
85-
86-
if tc.err != "" {
87-
assert.Error(t, err)
88-
assert.Regexp(t, tc.err, err)
89-
return
90-
}
91-
92-
assert.NoError(t, err)
93-
})
94-
}
95-
}
96-
9724
func TestUnmarshalJSONMultipleAuthProperties(t *testing.T) {
9825
t.Run("BearerAuthProperties", func(t *testing.T) {
9926
a1JSON := `{

model/workflow.go

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ const (
5757

5858
func init() {
5959
val.GetValidator().RegisterStructValidation(continueAsStructLevelValidation, ContinueAs{})
60+
val.GetValidator().RegisterStructValidation(BaseWorkflowStructLevelValidation, BaseWorkflow{})
6061
}
6162

6263
func continueAsStructLevelValidation(structLevel validator.StructLevel) {
@@ -108,7 +109,60 @@ type BaseWorkflow struct {
108109
// Auth definitions can be used to define authentication information that should be applied to resources defined in the operation
109110
// property of function definitions. It is not used as authentication information for the function invocation,
110111
// but just to access the resource containing the function invocation information.
111-
Auth AuthDefinitions `json:"auth,omitempty"`
112+
Auth AuthArray `json:"auth,omitempty" validate:"omitempty"`
113+
}
114+
115+
// BaseWorkflowStructLevelValidation custom validator for unique name of the auth methods
116+
func BaseWorkflowStructLevelValidation(structLevel validator.StructLevel) {
117+
// NOTE: we cannot add the custom validation of auth to AuthArray
118+
// because `RegisterStructValidation` only works with struct type
119+
wf := structLevel.Current().Interface().(BaseWorkflow)
120+
dict := map[string]bool{}
121+
122+
for _, a := range wf.Auth {
123+
if !dict[a.Name] {
124+
dict[a.Name] = true
125+
} else {
126+
structLevel.ReportError(reflect.ValueOf(a.Name), "Name", "name", "reqnameunique", "")
127+
}
128+
}
129+
}
130+
131+
type AuthArray []Auth
132+
133+
func (r *AuthArray) UnmarshalJSON(data []byte) error {
134+
if len(data) == 0 {
135+
return fmt.Errorf("no bytes to unmarshal")
136+
}
137+
138+
switch data[0] {
139+
case '"':
140+
return r.unmarshalFile(data)
141+
case '[':
142+
return r.unmarshalMany(data)
143+
}
144+
145+
return fmt.Errorf("auth value '%s' is not supported, it must be an array or string", string(data))
146+
}
147+
148+
func (r *AuthArray) unmarshalFile(data []byte) error {
149+
b, err := unmarshalFile(data)
150+
if err != nil {
151+
return fmt.Errorf("authDefinitions value '%s' is not supported, it must be an object or string", string(data))
152+
}
153+
154+
return r.unmarshalMany(b)
155+
}
156+
157+
func (r *AuthArray) unmarshalMany(data []byte) error {
158+
var auths []Auth
159+
err := json.Unmarshal(data, &auths)
160+
if err != nil {
161+
return fmt.Errorf("authDefinitions value '%s' is not supported, it must be an object or string", string(data))
162+
}
163+
164+
*r = auths
165+
return nil
112166
}
113167

114168
// Workflow base definition
@@ -130,6 +184,7 @@ func (w *Workflow) UnmarshalJSON(data []byte) error {
130184
if err := json.Unmarshal(data, &workflowMap); err != nil {
131185
return err
132186
}
187+
133188
var rawStates []json.RawMessage
134189
if err := json.Unmarshal(workflowMap["states"], &rawStates); err != nil {
135190
return err

0 commit comments

Comments
 (0)