added tests for slice of [primitive, slice, map, struct]

This commit is contained in:
Shreyas Goenka 2023-01-17 19:31:18 +01:00
parent ef6737d7de
commit f52d090166
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
2 changed files with 109 additions and 2 deletions

View File

@ -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

View File

@ -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"`