Skip to content

Commit

Permalink
Verify type definitions do not have duplicate fields. (#3925)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinmr authored Sep 6, 2019
1 parent 7f493e9 commit 66e3670
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions schema/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,16 @@ func parseTypeDeclaration(it *lex.ItemIterator) (*pb.TypeUpdate, error) {
}
it.Prev()

fieldSet := make(map[string]struct{})
for _, field := range fields {
if _, ok := fieldSet[field.GetPredicate()]; ok {
return nil, it.Item().Errorf("Duplicate fields with name: %s",
field.GetPredicate())
}

fieldSet[field.GetPredicate()] = struct{}{}
}

typeUpdate.Fields = fields
return typeUpdate, nil
case itemText:
Expand Down
12 changes: 12 additions & 0 deletions schema/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,18 @@ func TestParseNonNullableScalarAndList(t *testing.T) {
}, result.Types[0])
}

func TestParseTypeDuplicateFields(t *testing.T) {
reset()
_, err := Parse(`
type Person {
Name: string!
Name: string
}
`)
require.Error(t, err)
require.Contains(t, err.Error(), "Duplicate fields with name: Name")
}

func TestParseTypeErrMissingNewLine(t *testing.T) {
reset()
_, err := Parse(`
Expand Down

0 comments on commit 66e3670

Please sign in to comment.