From aa81d839708baaa297332f6ed6ee589703d63a60 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Tue, 17 Jan 2023 11:58:32 +0100 Subject: [PATCH] added ignoring - fields and tests for ignoring dashed and non annotated fields --- bundle/schema/schema.go | 7 ++--- bundle/schema/schema_test.go | 61 ++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/bundle/schema/schema.go b/bundle/schema/schema.go index 4cea1b4b6..98a280182 100644 --- a/bundle/schema/schema.go +++ b/bundle/schema/schema.go @@ -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 } diff --git a/bundle/schema/schema_test.go b/bundle/schema/schema_test.go index 154ad3470..edf882f5f 100644 --- a/bundle/schema/schema_test.go +++ b/bundle/schema/schema_test.go @@ -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{}