Skip to content
This repository has been archived by the owner on Dec 10, 2018. It is now read-only.

Commit

Permalink
Merge pull request #139 from hit9/check-field-id-name-dupl
Browse files Browse the repository at this point in the history
Check duplicate field id or name
  • Loading branch information
lxyu committed Jun 18, 2015
2 parents f9f0ca4 + 19ea72d commit 193c752
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions tests/parser-cases/e_duplicate_field_id.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
struct Foo {
1: required i32 abc
1: required i32 efg
}
4 changes: 4 additions & 0 deletions tests/parser-cases/e_duplicate_field_name.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
struct Foo {
1: required i32 abc
2: required i32 abc
}
9 changes: 9 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,15 @@ def test_e_use_thrift_reserved_keywords():
assert 'Cannot use reserved language keyword' in str(excinfo.value)


def test_e_duplicate_field_id_or_name():
with pytest.raises(ThriftGrammerError) as excinfo:
load('parser-cases/e_duplicate_field_id.thrift')
assert 'field identifier/name has already been used' in str(excinfo.value)
with pytest.raises(ThriftGrammerError) as excinfo:
load('parser-cases/e_duplicate_field_name.thrift')
assert 'field identifier/name has already been used' in str(excinfo.value)


def test_thrift_meta():
thrift = load('parser-cases/tutorial.thrift')
meta = thrift.__thrift_meta__
Expand Down
4 changes: 4 additions & 0 deletions thriftpy/parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,10 @@ def _make_struct(name, fields, ttype=TType.STRUCT, base_cls=TPayload,
_tspec = {}

for field in fields:
if field[0] in thrift_spec or field[3] in _tspec:
raise ThriftGrammerError(('\'%d:%s\' field identifier/name has '
'already been used') % (
field[0], field[3]))
ttype = field[2]
thrift_spec[field[0]] = _ttype_spec(ttype, field[3], field[1])
default_spec.append((field[3], field[4]))
Expand Down

0 comments on commit 193c752

Please sign in to comment.