added tests for struct of map and struct of slice

This commit is contained in:
Shreyas Goenka 2023-01-17 15:43:35 +01:00
parent 2021ca1886
commit f556e62c3b
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
1 changed files with 80 additions and 0 deletions

View File

@ -212,6 +212,86 @@ func TestStructOfStructsSchema(t *testing.T) {
assert.Equal(t, expected, string(jsonSchema))
}
func TestStructOfMapsSchema(t *testing.T) {
type Bar struct {
MyMap map[string]int `json:"my_map"`
}
type Foo struct {
Bar Bar `json:"bar"`
}
elem := Foo{}
schema, err := NewSchema(reflect.TypeOf(elem))
assert.NoError(t, err)
jsonSchema, err := json.MarshalIndent(schema, " ", " ")
assert.NoError(t, err)
expected :=
`{
"type": "object",
"properties": {
"bar": {
"type": "object",
"properties": {
"my_map": {
"type": "object",
"additionalProperties": {
"type": "number"
}
}
}
}
}
}`
t.Log("[DEBUG] actual: ", string(jsonSchema))
t.Log("[DEBUG] expected: ", expected)
assert.Equal(t, expected, string(jsonSchema))
}
func TestStructOfSliceSchema(t *testing.T) {
type Bar struct {
MySlice []string `json:"my_slice"`
}
type Foo struct {
Bar Bar `json:"bar"`
}
elem := Foo{}
schema, err := NewSchema(reflect.TypeOf(elem))
assert.NoError(t, err)
jsonSchema, err := json.MarshalIndent(schema, " ", " ")
assert.NoError(t, err)
expected :=
`{
"type": "object",
"properties": {
"bar": {
"type": "object",
"properties": {
"my_slice": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
}
}`
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"`