From f52d090166ecfb51ebcba7fcfe3688a4397826a2 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Tue, 17 Jan 2023 19:31:18 +0100 Subject: [PATCH] added tests for slice of [primitive, slice, map, struct] --- bundle/schema/schema.go | 7 ++- bundle/schema/schema_test.go | 104 +++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 2 deletions(-) diff --git a/bundle/schema/schema.go b/bundle/schema/schema.go index 2f58e8100..00a352b86 100644 --- a/bundle/schema/schema.go +++ b/bundle/schema/schema.go @@ -84,6 +84,7 @@ func NewSchema(golangType reflect.Type) (*Schema, error) { Type: rootProp.Type, Properties: rootProp.Properties, AdditionalProperties: rootProp.AdditionalProperties, + Items: rootProp.Items, }, nil } @@ -234,8 +235,10 @@ func toSchema(golangType reflect.Type, seenTypes map[reflect.Type]struct{}, debu } items = &Schema{ // TODO: Add a test for slice of object - Type: elemJavascriptType, - Properties: elemProps.Properties, + Type: elemJavascriptType, + Properties: elemProps.Properties, + AdditionalProperties: elemProps.AdditionalProperties, + Items: elemProps.Items, } // TODO: what if there is an array of maps. Add additional properties to // TODO: what if there are maps of maps diff --git a/bundle/schema/schema_test.go b/bundle/schema/schema_test.go index a00d3ee72..94faeda03 100644 --- a/bundle/schema/schema_test.go +++ b/bundle/schema/schema_test.go @@ -403,6 +403,110 @@ func TestMapOfSliceSchema(t *testing.T) { assert.Equal(t, expected, string(jsonSchema)) } +func TestSliceOfPrimitivesSchema(t *testing.T) { + var elem []float32 + + schema, err := NewSchema(reflect.TypeOf(elem)) + 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 + + schema, err := NewSchema(reflect.TypeOf(elem)) + assert.NoError(t, err) + + jsonSchema, err := json.MarshalIndent(schema, " ", " ") + assert.NoError(t, err) + + expected := + `{ + "type": "array", + "items": { + "type": "array", + "items": { + "type": "string" + } + } + }` + + t.Log("[DEBUG] actual: ", string(jsonSchema)) + t.Log("[DEBUG] expected: ", expected) + assert.Equal(t, expected, string(jsonSchema)) +} + +func TestSliceOfMapSchema(t *testing.T) { + var elem []map[string]int + + schema, err := NewSchema(reflect.TypeOf(elem)) + assert.NoError(t, err) + + jsonSchema, err := json.MarshalIndent(schema, " ", " ") + assert.NoError(t, err) + + expected := + `{ + "type": "array", + "items": { + "type": "object", + "additionalProperties": { + "type": "number" + } + } + }` + + t.Log("[DEBUG] actual: ", string(jsonSchema)) + t.Log("[DEBUG] expected: ", expected) + assert.Equal(t, expected, string(jsonSchema)) +} + +func TestSliceOfStructSchema(t *testing.T) { + type Foo struct { + MyInt int `json:"my_int"` + } + + var elem []Foo + + schema, err := NewSchema(reflect.TypeOf(elem)) + assert.NoError(t, err) + + jsonSchema, err := json.MarshalIndent(schema, " ", " ") + assert.NoError(t, err) + + expected := + `{ + "type": "array", + "items": { + "type": "object", + "properties": { + "my_int": { + "type": "number" + } + }, + "additionalProperties": false + } + }` + + t.Log("[DEBUG] actual: ", string(jsonSchema)) + t.Log("[DEBUG] expected: ", expected) + assert.Equal(t, expected, string(jsonSchema)) +} + func TestObjectSchema(t *testing.T) { type Person struct { Name string `json:"name"`