Skip to content

Commit

Permalink
[PIPE-1247] Allow duplicate field *names* if they have different name…
Browse files Browse the repository at this point in the history
… values in the json tag (#25)
  • Loading branch information
WC authored Jun 3, 2019
1 parent cc3bb2d commit 91708fb
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 28 deletions.
30 changes: 30 additions & 0 deletions fixtures/duplicate_embedded_fields.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"$ref": "#/definitions/testmodels.Root",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"testmodels.Root": {
"additionalProperties": false,
"properties": {
"bar": {
"type": "string"
},
"baz": {
"type": "string"
},
"bazDifferent": {
"type": "string"
},
"foo": {
"type": "string"
}
},
"required": [
"foo",
"bar",
"baz"
],
"type": "object"
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ package testmodels
type MostInner struct {
Foo string `json:"foo,omitempty"`
Bar string `json:"bar,omitempty"`
Baz string `json:"bazDifferent,omitempty"`
}
type Inner struct {
MostInner
Foo string `json:"foo,omitempty"`
Bar string `json:"bar,omitempty"`
Baz string `json:"baz,omitempty"`
}

type Root struct {
Inner
Foo string `json:"foo"`
Bar string `json:"bar"`
Baz string `json:"baz"`
}
12 changes: 6 additions & 6 deletions reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,14 +327,15 @@ func (r *Reflector) reflectStructFields(st *Type, definitions Definitions, t ref
orderArray := []structOrder{}
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
// if not Anonymous and tag not present, insert
// else check if present and assign propert to current field

// Prevent duplicate jsonschema name declarations when using embeds
name, required := r.reflectFieldName(f, t)
if !f.Anonymous {
_, ok := st.tagPrecedence[f.Name]
_, ok := st.tagPrecedence[name]
if !ok {
st.tagPrecedence[f.Name] = f.Tag
st.tagPrecedence[name] = f.Tag
} else {
f.Tag = st.tagPrecedence[f.Name]
f.Tag = st.tagPrecedence[name]
}
}
// anonymous and exported type should be processed recursively
Expand All @@ -346,7 +347,6 @@ func (r *Reflector) reflectStructFields(st *Type, definitions Definitions, t ref
continue
}

name, required := r.reflectFieldName(f, t)
if name == "" {
continue
}
Expand Down
1 change: 1 addition & 0 deletions reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var schemaGenerationTests = []testSet{
{&jsonschema.Reflector{}, "fixtures/case.json", testmodels.ExampleCase{}},
{&jsonschema.Reflector{}, "fixtures/test_min_max_items.json", testmodels.SliceTestType{}},
{&jsonschema.Reflector{}, "fixtures/test_recursion.json", testmodels.TestFamilyMember{}},
{&jsonschema.Reflector{}, "fixtures/duplicate_embedded_fields.json", testmodels.Root{}},
}

func TestSchemaGeneration(t *testing.T) {
Expand Down

0 comments on commit 91708fb

Please sign in to comment.