Skip to content

Commit 64e5552

Browse files
authored
fix: map metadata to objectmeta (#292)
* map metadata to objectmeta On-behalf-of: @SAP [email protected] Signed-off-by: Artem Shcherbatiuk <[email protected]> * language On-behalf-of: @SAP [email protected] Signed-off-by: Artem Shcherbatiuk <[email protected]> * use existing field name instead of creating a new one On-behalf-of: @SAP [email protected] Signed-off-by: Artem Shcherbatiuk <[email protected]> * removed allowlist On-behalf-of: @SAP [email protected] Signed-off-by: Artem Shcherbatiuk <[email protected]> * json On-behalf-of: @SAP [email protected] Signed-off-by: Artem Shcherbatiuk <[email protected]> * jsonScalar On-behalf-of: @SAP [email protected] Signed-off-by: Artem Shcherbatiuk <[email protected]> --------- Signed-off-by: Artem Shcherbatiuk <[email protected]>
1 parent 662927f commit 64e5552

File tree

4 files changed

+90
-2
lines changed

4 files changed

+90
-2
lines changed

gateway/schema/exports_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package schema
33
import "k8s.io/apimachinery/pkg/runtime/schema"
44

55
var StringMapScalarForTest = stringMapScalar
6+
var JSONStringScalarForTest = jsonStringScalar
67

78
func GetGatewayForTest(typeNameRegistry map[string]string) *Gateway {
89
return &Gateway{

gateway/schema/scalars.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package schema
22

33
import (
4+
"encoding/json"
5+
46
"github.com/graphql-go/graphql"
57
"github.com/graphql-go/graphql/language/ast"
68
)
@@ -34,3 +36,39 @@ var stringMapScalar = graphql.NewScalar(graphql.ScalarConfig{
3436
}
3537
},
3638
})
39+
40+
var jsonStringScalar = graphql.NewScalar(graphql.ScalarConfig{
41+
Name: "JSONString",
42+
Description: "A JSON-serialized string representation of any object.",
43+
Serialize: func(value interface{}) interface{} {
44+
// Convert the value to JSON string
45+
jsonBytes, err := json.Marshal(value)
46+
if err != nil {
47+
// Fallback to empty JSON object if marshaling fails
48+
return "{}"
49+
}
50+
return string(jsonBytes)
51+
},
52+
ParseValue: func(value interface{}) interface{} {
53+
if str, ok := value.(string); ok {
54+
var result interface{}
55+
err := json.Unmarshal([]byte(str), &result)
56+
if err != nil {
57+
return nil // Invalid JSON
58+
}
59+
return result
60+
}
61+
return nil
62+
},
63+
ParseLiteral: func(valueAST ast.Value) interface{} {
64+
if value, ok := valueAST.(*ast.StringValue); ok {
65+
var result interface{}
66+
err := json.Unmarshal([]byte(value.Value), &result)
67+
if err != nil {
68+
return nil // Invalid JSON
69+
}
70+
return result
71+
}
72+
return nil
73+
},
74+
})

gateway/schema/scalars_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package schema_test
22

33
import (
4+
"encoding/json"
45
"reflect"
56
"testing"
67

@@ -153,3 +154,51 @@ func TestGenerateTypeName(t *testing.T) {
153154
})
154155
}
155156
}
157+
158+
func TestJSONStringScalar_ProperSerialization(t *testing.T) {
159+
testObject := map[string]interface{}{
160+
"name": "example-config",
161+
"namespace": "default",
162+
"labels": map[string]string{
163+
"hello": "world",
164+
},
165+
"annotations": map[string]string{
166+
"kcp.io/cluster": "root",
167+
},
168+
}
169+
170+
// Test the JSONString scalar serialization
171+
result := schema.JSONStringScalarForTest.Serialize(testObject)
172+
173+
if result == nil {
174+
t.Fatal("JSONStringScalar.Serialize returned nil")
175+
}
176+
177+
resultStr, ok := result.(string)
178+
if !ok {
179+
t.Fatalf("JSONStringScalar.Serialize returned %T, expected string", result)
180+
}
181+
182+
// Verify it's valid JSON
183+
var parsed map[string]interface{}
184+
err := json.Unmarshal([]byte(resultStr), &parsed)
185+
if err != nil {
186+
t.Fatalf("Result is not valid JSON: %s\nResult: %s", err, resultStr)
187+
}
188+
189+
// Verify the content is preserved
190+
if parsed["name"] != "example-config" {
191+
t.Errorf("Name not preserved: got %v, want %v", parsed["name"], "example-config")
192+
}
193+
194+
if parsed["namespace"] != "default" {
195+
t.Errorf("Namespace not preserved: got %v, want %v", parsed["namespace"], "default")
196+
}
197+
198+
// Verify it's NOT Go map format
199+
if len(resultStr) > 10 && resultStr[:4] == "map[" {
200+
t.Errorf("Result is in Go map format, not JSON: %s", resultStr)
201+
}
202+
203+
t.Logf("Proper JSON output: %s", resultStr)
204+
}

gateway/schema/schema.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,8 @@ func (g *Gateway) handleObjectFieldSpecType(fieldSpec spec.Schema, typePrefix st
453453
}
454454
}
455455

456-
// It's an empty object
457-
return graphql.String, graphql.String, nil
456+
// It's an empty object, serialize as JSON string
457+
return jsonStringScalar, jsonStringScalar, nil
458458
}
459459

460460
func (g *Gateway) generateTypeName(typePrefix string, fieldPath []string) string {

0 commit comments

Comments
 (0)