added nested tests

This commit is contained in:
Shreyas Goenka 2024-08-20 21:06:42 +02:00
parent b023ba0dd4
commit e24725d230
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
2 changed files with 108 additions and 8 deletions

View File

@ -115,7 +115,7 @@ func (c *constructor) walk(typ reflect.Type) (string, error) {
// Return value directly if it's already been processed. // Return value directly if it's already been processed.
if _, ok := c.definitions[typPath]; ok { if _, ok := c.definitions[typPath]; ok {
return "", nil return typPath, nil
} }
var s Schema var s Schema

View File

@ -1,7 +1,6 @@
package jsonschema package jsonschema
import ( import (
"encoding/json"
"reflect" "reflect"
"testing" "testing"
@ -130,14 +129,14 @@ func TestFromTypeBasic(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, tc.expected, s) assert.Equal(t, tc.expected, s)
jsonSchema, err := json.MarshalIndent(s, " ", " ") // jsonSchema, err := json.MarshalIndent(s, " ", " ")
assert.NoError(t, err) // assert.NoError(t, err)
expectedJson, err := json.MarshalIndent(tc.expected, " ", " ") // expectedJson, err := json.MarshalIndent(tc.expected, " ", " ")
assert.NoError(t, err) // assert.NoError(t, err)
t.Log("[DEBUG] actual: ", string(jsonSchema)) // t.Log("[DEBUG] actual: ", string(jsonSchema))
t.Log("[DEBUG] expected: ", string(expectedJson)) // t.Log("[DEBUG] expected: ", string(expectedJson))
}) })
} }
} }
@ -159,3 +158,104 @@ func TestGetStructFields(t *testing.T) {
assert.Equal(t, "I", fields[1].Name) assert.Equal(t, "I", fields[1].Name)
assert.Equal(t, "B", fields[2].Name) assert.Equal(t, "B", fields[2].Name)
} }
func TestFromTypeNested(t *testing.T) {
type Inner struct {
S string `json:"s"`
}
type Outer struct {
I string `json:"i"`
Inner Inner `json:"inner"`
}
innerRef := "#/$defs/github.com/databricks/cli/libs/jsonschema.Inner"
strRef := "#/$defs/string"
expectedDefinitions := map[string]any{
"github.com": map[string]any{
"databricks": map[string]any{
"cli": map[string]any{
"libs": map[string]any{
"jsonschema.Inner": Schema{
Type: "object",
Properties: map[string]*Schema{
"s": {
Reference: &strRef,
},
},
AdditionalProperties: false,
Required: []string{"s"},
},
},
},
},
},
"string": Schema{
Type: "string",
},
}
tcases := []struct {
name string
typ reflect.Type
expected Schema
}{
{
name: "struct in struct",
typ: reflect.TypeOf(Outer{}),
expected: Schema{
Type: "object",
Definitions: expectedDefinitions,
Properties: map[string]*Schema{
"i": {
Reference: &strRef,
},
"inner": {
Reference: &innerRef,
},
},
AdditionalProperties: false,
Required: []string{"i", "inner"},
},
},
{
name: "struct as a map value",
typ: reflect.TypeOf(map[string]Inner{}),
expected: Schema{
Type: "object",
Definitions: expectedDefinitions,
AdditionalProperties: &Schema{
Reference: &innerRef,
},
},
},
{
name: "struct as a slice element",
typ: reflect.TypeOf([]Inner{}),
expected: Schema{
Type: "array",
Definitions: expectedDefinitions,
Items: &Schema{
Reference: &innerRef,
},
},
},
}
for _, tc := range tcases {
t.Run(tc.name, func(t *testing.T) {
s, err := FromType(tc.typ, nil)
assert.NoError(t, err)
assert.Equal(t, tc.expected, s)
// jsonSchema, err := json.MarshalIndent(s, " ", " ")
// assert.NoError(t, err)
// expectedJson, err := json.MarshalIndent(tc.expected, " ", " ")
// assert.NoError(t, err)
// t.Log("[DEBUG] actual: ", string(jsonSchema))
// t.Log("[DEBUG] expected: ", string(expectedJson))
})
}
}