added ignoring - fields and tests for ignoring dashed and non annotated fields

This commit is contained in:
Shreyas Goenka 2023-01-17 11:58:32 +01:00
parent 2a66a2fc74
commit aa81d83970
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
2 changed files with 64 additions and 4 deletions

View File

@ -8,7 +8,6 @@ import (
)
// TODO: Add tests for the error cases, forcefully triggering them
// TODO: Add ignore for -
// TODO: Add required validation for omitempty
// TODO: Add example documentation
// TODO: Do final checks for more validation that can be added to json schema
@ -34,7 +33,7 @@ type Item struct {
// NOTE about loops in golangType: Right now we error out if there is a loop
// in the types traversed when generating the json schema. This can be solved
// using $refs but there is complexity around making sure we do not create json
// schema's where properties indirectly refer to each other, which would be an
// schemas where properties indirectly refer to each other, which would be an
// invalid json schema. See https://json-schema.org/understanding-json-schema/structuring.html#recursion
// for more details
func NewSchema(golangType reflect.Type) (*Schema, error) {
@ -219,8 +218,8 @@ func toProperty(golangType reflect.Type, seenTypes map[reflect.Type]struct{}, de
// add current field to debug trace
debugTrace.PushBack(childName)
// skip non json annotated fields
if childName == "" {
// skip fields that are not annotated or annotated with "-"
if childName == "" || childName == "-" {
continue
}

View File

@ -7,6 +7,7 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TODO: add tests to assert that these are valid json schemas. Maybe validate some
@ -354,6 +355,66 @@ func TestErrorWithTrace(t *testing.T) {
assert.ErrorContains(t, err, "[ERROR] with depth = 4. traversal trace: root -> resources -> pipelines -> datasets")
}
func TestNonAnnotatedFieldsAreSkipped(t *testing.T) {
type MyStruct struct {
Foo string
Bar int `json:"bar"`
}
elem := MyStruct{}
schema, err := NewSchema(reflect.TypeOf(elem))
require.NoError(t, err)
jsonSchema, err := json.MarshalIndent(schema, " ", " ")
assert.NoError(t, err)
expectedSchema :=
`{
"type": "object",
"properties": {
"bar": {
"type": "number"
}
}
}`
t.Log("[DEBUG] actual: ", string(jsonSchema))
t.Log("[DEBUG] expected: ", expectedSchema)
assert.Equal(t, expectedSchema, string(jsonSchema))
}
func TestDashFieldsAreSkipped(t *testing.T) {
type MyStruct struct {
Foo string `json:"-"`
Bar int `json:"bar"`
}
elem := MyStruct{}
schema, err := NewSchema(reflect.TypeOf(elem))
require.NoError(t, err)
jsonSchema, err := json.MarshalIndent(schema, " ", " ")
assert.NoError(t, err)
expectedSchema :=
`{
"type": "object",
"properties": {
"bar": {
"type": "number"
}
}
}`
t.Log("[DEBUG] actual: ", string(jsonSchema))
t.Log("[DEBUG] expected: ", expectedSchema)
assert.Equal(t, expectedSchema, string(jsonSchema))
}
// // Only for testing bundle, will be removed
// func TestBundleSchema(t *testing.T) {
// elem := config.Root{}