Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow setting JSON type schema max depth #1844

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions transformers/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,10 @@ func WithPrimaryKeyComponents(fields ...string) StructTransformerOption {
t.pkComponentFields = fields
}
}

// WithMaxJSONTypeSchemaDepth allows to specify the maximum depth of JSON type schema
func WithMaxJSONTypeSchemaDepth(maxDepth int) StructTransformerOption {
return func(t *structTransformer) {
t.maxJSONTypeSchemaDepth = maxDepth
}
}
7 changes: 5 additions & 2 deletions transformers/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/thoas/go-funk"
)

const maxJSONTypeSchemaDepth = 4
const DefaultMaxJSONTypeSchemaDepth = 4

type structTransformer struct {
table *schema.Table
Expand All @@ -29,6 +29,8 @@ type structTransformer struct {
pkFieldsFound []string
pkComponentFields []string
pkComponentFieldsFound []string

maxJSONTypeSchemaDepth int
}

func isFieldStruct(reflectType reflect.Type) bool {
Expand Down Expand Up @@ -196,6 +198,7 @@ func TransformWithStruct(st any, opts ...StructTransformerOption) schema.Transfo
typeTransformer: DefaultTypeTransformer,
resolverTransformer: DefaultResolverTransformer,
ignoreInTestsTransformer: DefaultIgnoreInTestsTransformer,
maxJSONTypeSchemaDepth: DefaultMaxJSONTypeSchemaDepth,
}
for _, opt := range opts {
opt(t)
Expand Down Expand Up @@ -294,7 +297,7 @@ func (t *structTransformer) fieldToJSONSchema(field reflect.StructField, depth i
continue
}
// Avoid infinite recursion
if columnType == types.ExtensionTypes.JSON && depth < maxJSONTypeSchemaDepth {
if columnType == types.ExtensionTypes.JSON && depth < t.maxJSONTypeSchemaDepth {
fieldsMap[name] = t.fieldToJSONSchema(structField, depth+1)
continue
}
Expand Down
31 changes: 30 additions & 1 deletion transformers/struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ func TestJSONTypeSchema(t *testing.T) {
tests := []struct {
name string
testStruct any
maxDepth int
want map[string]string
}{
{
Expand Down Expand Up @@ -625,6 +626,30 @@ func TestJSONTypeSchema(t *testing.T) {
"level0": `{"level1":{"level2":{"level3":{"level4":{"level5":"json"}}}}}`,
},
},
{
name: "stops at the configured depth",
maxDepth: 2,
testStruct: struct {
Level0 struct {
Level1 struct {
Level2 struct {
Level3 struct {
Level4 struct {
Level5 struct {
Level6 struct {
Name string `json:"name"`
} `json:"level6"`
} `json:"level5"`
} `json:"level4"`
} `json:"level3"`
} `json:"level2"`
} `json:"level1"`
} `json:"level0"`
}{},
want: map[string]string{
"level0": `{"level1":{"level2":{"level3":"json"}}}`,
},
},
{
name: "ignores non exported and ignored types",
testStruct: struct {
Expand All @@ -648,7 +673,11 @@ func TestJSONTypeSchema(t *testing.T) {
table := schema.Table{
Name: "test",
}
transformer := TransformWithStruct(tt.testStruct)
opts := []StructTransformerOption{}
if tt.maxDepth > 0 {
opts = append(opts, WithMaxJSONTypeSchemaDepth(tt.maxDepth))
}
transformer := TransformWithStruct(tt.testStruct, opts...)
err := transformer(&table)
if err != nil {
t.Fatal(err)
Expand Down