Skip to content

Commit b1ba8ff

Browse files
authored
fix: added validation to StringMap scalar (#176)
* fix: added validation to StringMap scalar On-behalf-of: @SAP [email protected] Signed-off-by: Artem Shcherbatiuk <[email protected]>
1 parent c00f77b commit b1ba8ff

File tree

3 files changed

+90
-3
lines changed

3 files changed

+90
-3
lines changed

gateway/schema/exports_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package schema
2+
3+
var StringMapScalar = stringMapScalar

gateway/schema/scalars.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,25 @@ var stringMapScalar = graphql.NewScalar(graphql.ScalarConfig{
1212
return value
1313
},
1414
ParseValue: func(value interface{}) interface{} {
15-
return value
15+
switch val := value.(type) {
16+
case map[string]interface{}, map[string]string:
17+
return val
18+
default:
19+
return nil // to tell GraphQL that the value is invalid
20+
}
1621
},
1722
ParseLiteral: func(valueAST ast.Value) interface{} {
18-
result := map[string]string{}
1923
switch value := valueAST.(type) {
2024
case *ast.ObjectValue:
25+
result := map[string]string{}
2126
for _, field := range value.Fields {
2227
if strValue, ok := field.Value.GetValue().(string); ok {
2328
result[field.Name.Value] = strValue
2429
}
2530
}
31+
return result
32+
default:
33+
return nil // to tell GraphQL that the value is invalid
2634
}
27-
return result
2835
},
2936
})

gateway/schema/scalars_test.go

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package schema_test
2+
3+
import (
4+
"github.com/openmfp/kubernetes-graphql-gateway/gateway/schema"
5+
"reflect"
6+
"testing"
7+
8+
"github.com/graphql-go/graphql/language/ast"
9+
"github.com/graphql-go/graphql/language/kinds"
10+
)
11+
12+
func TestStringMapScalar_ParseValue(t *testing.T) {
13+
tests := []struct {
14+
input interface{}
15+
expected interface{}
16+
}{
17+
{
18+
input: map[string]interface{}{"key": "val"},
19+
expected: map[string]interface{}{"key": "val"},
20+
},
21+
{
22+
input: map[string]string{"a": "b"},
23+
expected: map[string]string{"a": "b"},
24+
},
25+
{
26+
input: "key=val",
27+
expected: nil,
28+
},
29+
}
30+
31+
for _, test := range tests {
32+
out := schema.StringMapScalar.ParseValue(test.input)
33+
if !reflect.DeepEqual(out, test.expected) {
34+
t.Errorf("ParseValue(%v) = %v; want %v", test.input, out, test.expected)
35+
}
36+
}
37+
}
38+
39+
func TestStringMapScalar_ParseLiteral(t *testing.T) {
40+
tests := []struct {
41+
name string
42+
input ast.Value
43+
expected interface{}
44+
}{
45+
{
46+
name: "valid_object_value",
47+
input: &ast.ObjectValue{
48+
Kind: kinds.ObjectValue,
49+
Fields: []*ast.ObjectField{
50+
{
51+
Name: &ast.Name{Value: "key"},
52+
Value: &ast.StringValue{Kind: kinds.StringValue, Value: "val"},
53+
},
54+
{
55+
Name: &ast.Name{Value: "key2"},
56+
Value: &ast.StringValue{Kind: kinds.StringValue, Value: "val2"},
57+
},
58+
},
59+
},
60+
expected: map[string]string{"key": "val", "key2": "val2"},
61+
},
62+
{
63+
name: "invalid_string_value",
64+
input: &ast.StringValue{Kind: kinds.StringValue, Value: "key=val"},
65+
expected: nil,
66+
},
67+
}
68+
69+
for _, tt := range tests {
70+
t.Run(tt.name, func(t *testing.T) {
71+
out := schema.StringMapScalar.ParseLiteral(tt.input)
72+
if !reflect.DeepEqual(out, tt.expected) {
73+
t.Errorf("ParseLiteral() = %v, want %v", out, tt.expected)
74+
}
75+
})
76+
}
77+
}

0 commit comments

Comments
 (0)