Skip to content

Commit

Permalink
add test for bfs final schema
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyas-goenka committed Sep 9, 2024
1 parent 2d62c0c commit 5f48b58
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
7 changes: 7 additions & 0 deletions libs/jsonschema/from_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,13 @@ func (c *constructor) fromTypeStruct(typ reflect.Type) (Schema, error) {
continue
}

// Skip property if it is already present in the schema.
// This can happen if the same field is defined multiple times across
// a tree of embedded structs. For example see: TestHigherLevelEmbeddedFieldIsInSchema
if _, ok := res.Properties[fieldName]; ok {
continue
}

// "omitempty" tags in the Go SDK structs represent fields that not are
// required to be present in the API payload. Thus its absence in the
// tags list indicates that the field is required.
Expand Down
41 changes: 41 additions & 0 deletions libs/jsonschema/from_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/databricks/cli/libs/jsonschema/test_types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestFromTypeBasic(t *testing.T) {
Expand Down Expand Up @@ -166,6 +167,46 @@ func TestGetStructFields(t *testing.T) {
assert.Equal(t, "InnerField", fields[3].Name)
}

func TestHigherLevelEmbeddedFieldIsInSchema(t *testing.T) {
type Inner struct {
Override string `json:"override,omitempty"`
}

type EmbeddedOne struct {
Inner
}

type EmbeddedTwo struct {
Override int `json:"override,omitempty"`
}

type Outer struct {
EmbeddedOne
EmbeddedTwo
}

intRef := "#/$defs/int"
expected := Schema{
Type: "object",
Definitions: map[string]any{
"int": Schema{
Type: "integer",
},
},
Properties: map[string]*Schema{
"override": {
Reference: &intRef,
},
},
AdditionalProperties: false,
Required: []string{},
}

s, err := FromType(reflect.TypeOf(Outer{}), nil)
require.NoError(t, err)
assert.Equal(t, expected, s)
}

func TestFromTypeNested(t *testing.T) {
type Inner struct {
S string `json:"s"`
Expand Down

0 comments on commit 5f48b58

Please sign in to comment.