Skip to content

Commit e97f243

Browse files
authored
fix: Don't include non exported fields or ignored types in JSON schema (#1824)
#### Summary Noticed this while opening cloudquery/cloudquery-issues#2116. ---
1 parent cb1dcb4 commit e97f243

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

transformers/struct.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,11 +277,15 @@ func (t *structTransformer) fieldToJSONSchema(field reflect.StructField, depth i
277277
fieldsMap := make(map[string]any)
278278
fieldType := normalizedField.Elem().Type()
279279
for i := 0; i < fieldType.NumField(); i++ {
280-
name, err := t.nameTransformer(fieldType.Field(i))
280+
structField := fieldType.Field(i)
281+
if !structField.IsExported() || isTypeIgnored(structField.Type) {
282+
continue
283+
}
284+
name, err := t.nameTransformer(structField)
281285
if err != nil {
282286
continue
283287
}
284-
columnType, err := t.getColumnType(fieldType.Field(i))
288+
columnType, err := t.getColumnType(structField)
285289
if err != nil {
286290
continue
287291
}
@@ -291,7 +295,7 @@ func (t *structTransformer) fieldToJSONSchema(field reflect.StructField, depth i
291295
}
292296
// Avoid infinite recursion
293297
if columnType == types.ExtensionTypes.JSON && depth < maxJSONTypeSchemaDepth {
294-
fieldsMap[name] = t.fieldToJSONSchema(fieldType.Field(i), depth+1)
298+
fieldsMap[name] = t.fieldToJSONSchema(structField, depth+1)
295299
continue
296300
}
297301
asList, ok := columnType.(*arrow.ListType)

transformers/struct_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"slices"
77
"testing"
88
"time"
9+
"unsafe"
910

1011
"github.com/apache/arrow/go/v17/arrow"
1112
"github.com/cloudquery/plugin-sdk/v4/schema"
@@ -624,6 +625,21 @@ func TestJSONTypeSchema(t *testing.T) {
624625
"level0": `{"level1":{"level2":{"level3":{"level4":{"level5":"json"}}}}}`,
625626
},
626627
},
628+
{
629+
name: "ignores non exported and ignored types",
630+
testStruct: struct {
631+
Item struct {
632+
nonExported string
633+
f func()
634+
c chan int
635+
unsafePointer unsafe.Pointer
636+
Exported string `json:"exported"`
637+
} `json:"item"`
638+
}{},
639+
want: map[string]string{
640+
"item": `{"exported":"utf8"}`,
641+
},
642+
},
627643
}
628644

629645
for _, tt := range tests {

0 commit comments

Comments
 (0)