From 7db32fc211d2c35bc1a4dff3a2ebe6e23ccc5b66 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Tue, 20 Aug 2024 19:09:21 +0200 Subject: [PATCH] - --- bundle/schema/schema_test.go | 53 ------------------------------- libs/jsonschema/from_type.go | 6 ++-- libs/jsonschema/from_type_test.go | 18 +++++++++++ 3 files changed, 21 insertions(+), 56 deletions(-) diff --git a/bundle/schema/schema_test.go b/bundle/schema/schema_test.go index e754e5c6ca..1955dce1a4 100644 --- a/bundle/schema/schema_test.go +++ b/bundle/schema/schema_test.go @@ -2,7 +2,6 @@ package schema import ( "encoding/json" - "fmt" "reflect" "testing" @@ -187,36 +186,6 @@ func TestStructOfSliceSchema(t *testing.T) { assert.Equal(t, expected, string(jsonSchema)) } -func TestMapOfPrimitivesSchema(t *testing.T) { - var elem map[string]int - - schema, err := New(reflect.TypeOf(elem), nil) - assert.NoError(t, err) - - jsonSchema, err := json.MarshalIndent(schema, " ", " ") - assert.NoError(t, err) - - expected := - `{ - "type": "object", - "additionalProperties": { - "anyOf": [ - { - "type": "number" - }, - { - "type": "string", - "pattern": "\\$\\{([a-zA-Z]+([-_]?[a-zA-Z0-9]+)*(\\.[a-zA-Z]+([-_]?[a-zA-Z0-9]+)*(\\[[0-9]+\\])*)*(\\[[0-9]+\\])*)\\}" - } - ] - } - }` - - t.Log("[DEBUG] actual: ", string(jsonSchema)) - t.Log("[DEBUG] expected: ", expected) - assert.Equal(t, expected, string(jsonSchema)) -} - func TestMapOfStructSchema(t *testing.T) { type Foo struct { MyInt int `json:"my_int"` @@ -318,28 +287,6 @@ func TestMapOfSliceSchema(t *testing.T) { assert.Equal(t, expected, string(jsonSchema)) } -func TestSliceOfPrimitivesSchema(t *testing.T) { - var elem []float32 - - schema, err := New(reflect.TypeOf(elem), nil) - assert.NoError(t, err) - - jsonSchema, err := json.MarshalIndent(schema, " ", " ") - assert.NoError(t, err) - - expected := - `{ - "type": "array", - "items": { - "type": "number" - } - }` - - t.Log("[DEBUG] actual: ", string(jsonSchema)) - t.Log("[DEBUG] expected: ", expected) - assert.Equal(t, expected, string(jsonSchema)) -} - func TestSliceOfSliceSchema(t *testing.T) { var elem [][]string diff --git a/libs/jsonschema/from_type.go b/libs/jsonschema/from_type.go index 8332e70df6..c5f6250a9e 100644 --- a/libs/jsonschema/from_type.go +++ b/libs/jsonschema/from_type.go @@ -159,12 +159,12 @@ func (c *constructor) walk(typ reflect.Type) error { // This function returns all member fields of the provided type. // If the type has embedded (aka anonymous) fields, this function traverses // those in a breadth first manner -func getStructFields(golangType reflect.Type) []reflect.StructField { +func getStructFields(typ reflect.Type) []reflect.StructField { fields := []reflect.StructField{} bfsQueue := list.New() - for i := 0; i < golangType.NumField(); i++ { - bfsQueue.PushBack(golangType.Field(i)) + for i := 0; i < typ.NumField(); i++ { + bfsQueue.PushBack(typ.Field(i)) } for bfsQueue.Len() > 0 { front := bfsQueue.Front() diff --git a/libs/jsonschema/from_type_test.go b/libs/jsonschema/from_type_test.go index beea80e336..fd60bf44ff 100644 --- a/libs/jsonschema/from_type_test.go +++ b/libs/jsonschema/from_type_test.go @@ -124,3 +124,21 @@ func TestFromTypeBasic(t *testing.T) { }) } } + +func TestGetStructFields(t *testing.T) { + type EmbeddedStruct struct { + I int + B bool + } + + type MyStruct struct { + S string + *EmbeddedStruct + } + + fields := getStructFields(reflect.TypeOf(MyStruct{})) + assert.Len(t, fields, 3) + assert.Equal(t, "S", fields[0].Name) + assert.Equal(t, "I", fields[1].Name) + assert.Equal(t, "B", fields[2].Name) +}