Added test for struct of structs

This commit is contained in:
Shreyas Goenka 2023-01-17 15:34:49 +01:00
parent 019b7d7fa9
commit 2021ca1886
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
1 changed files with 57 additions and 2 deletions

View File

@ -16,7 +16,12 @@ import (
// TODO: See that all golang reflect types are covered (reasonalble limits) within
// these tests
func TestSIntSchema(t *testing.T) {
// TODO: test what json schema is generated for an interface{} type. Make sure the behavior makes sense
// TODO: Have a test that combines multiple different cases
// TODO: have a test for what happens when omitempty in different cases: primitives, object, map, array
func TestIntSchema(t *testing.T) {
var elemInt int
expected :=
@ -73,7 +78,7 @@ func TestStringSchema(t *testing.T) {
assert.Equal(t, expected, string(jsonSchema))
}
func TestSchemaObjectOfPrimitives(t *testing.T) {
func TestStructOfPrimitivesSchema(t *testing.T) {
type Foo struct {
IntVal int `json:"int_val"`
Int8Val int8 `json:"int8_val"`
@ -157,6 +162,56 @@ func TestSchemaObjectOfPrimitives(t *testing.T) {
assert.Equal(t, expected, string(jsonSchema))
}
func TestStructOfStructsSchema(t *testing.T) {
type Bar struct {
A int `json:"a"`
B string `json:"b,string"`
}
type Foo struct {
Bar Bar `json:"bar"`
}
type MyStruct struct {
Foo Foo `json:"foo"`
}
elem := MyStruct{}
schema, err := NewSchema(reflect.TypeOf(elem))
assert.NoError(t, err)
jsonSchema, err := json.MarshalIndent(schema, " ", " ")
assert.NoError(t, err)
expected :=
`{
"type": "object",
"properties": {
"foo": {
"type": "object",
"properties": {
"bar": {
"type": "object",
"properties": {
"a": {
"type": "number"
},
"b": {
"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"`