Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix get_field for reserved names in field path #722

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions gapic/schema/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,9 @@ def get_field(self, *field_path: str,
# binds deeply in the one spot where that might be a problem).
collisions = collisions or self.meta.address.collisions

field_path = tuple([
name + "_" if name in utils.RESERVED_NAMES else name for name in field_path])

# Get the first field in the path.
cursor = self.fields[field_path[0]]

Expand Down
9 changes: 9 additions & 0 deletions tests/unit/schema/wrappers/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ def test_get_field_recursive():
assert outer.get_field('inner', 'one') == inner_fields[1]


def test_get_field_reserved_name():
# 'input' is a reserved word and will be renamed to 'input_'
fields = (make_field('input_'), make_field('one'))
msg = make_message('Inner', fields=fields, package='foo.v1')

# Assert that retrieval with the regular field path works
assert msg.get_field('input') is not None


def test_get_field_nested_not_found_error():
# Create the inner message.
inner_field = make_field('zero')
Expand Down