Skip to content

Commit b3baf14

Browse files
authored
fix: fix resource path helpers for paths with =** (#1976)
1 parent 58185c1 commit b3baf14

File tree

6 files changed

+17
-6
lines changed

6 files changed

+17
-6
lines changed

packages/gapic-generator/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/client.py.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
194194
@staticmethod
195195
def {{ message.resource_type|snake_case }}_path({% for arg in message.resource_path_args %}{{ arg }}: str,{% endfor %}) -> str:
196196
"""Returns a fully-qualified {{ message.resource_type|snake_case }} string."""
197-
return "{{ message.resource_path }}".format({% for arg in message.resource_path_args %}{{ arg }}={{ arg }}, {% endfor %})
197+
return "{{ message.resource_path_formatted }}".format({% for arg in message.resource_path_args %}{{ arg }}={{ arg }}, {% endfor %})
198198

199199

200200
@staticmethod

packages/gapic-generator/gapic/ads-templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2259,7 +2259,7 @@ def test_{{ message.resource_type|snake_case }}_path():
22592259
{% for arg in message.resource_path_args %}
22602260
{{ arg }} = "{{ molluscs.next() }}"
22612261
{% endfor %}
2262-
expected = "{{ message.resource_path }}".format({% for arg in message.resource_path_args %}{{ arg }}={{ arg }}, {% endfor %})
2262+
expected = "{{ message.resource_path_formatted }}".format({% for arg in message.resource_path_args %}{{ arg }}={{ arg }}, {% endfor %})
22632263
actual = {{ service.client_name }}.{{ message.resource_type|snake_case }}_path({{message.resource_path_args|join(", ") }})
22642264
assert expected == actual
22652265

@@ -2282,7 +2282,7 @@ def test_common_{{ resource_msg.message_type.resource_type|snake_case }}_path():
22822282
{% for arg in resource_msg.message_type.resource_path_args %}
22832283
{{ arg }} = "{{ molluscs.next() }}"
22842284
{% endfor %}
2285-
expected = "{{ resource_msg.message_type.resource_path }}".format({% for arg in resource_msg.message_type.resource_path_args %}{{ arg }}={{ arg }}, {% endfor %})
2285+
expected = "{{ resource_msg.message_type.resource_path_formatted }}".format({% for arg in resource_msg.message_type.resource_path_args %}{{ arg }}={{ arg }}, {% endfor %})
22862286
actual = {{ service.client_name }}.common_{{ resource_msg.message_type.resource_type|snake_case }}_path({{ resource_msg.message_type.resource_path_args|join(", ") }})
22872287
assert expected == actual
22882288

packages/gapic-generator/gapic/schema/wrappers.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,16 @@ def resource_type_full_path(self) -> Optional[str]:
613613
def resource_path_args(self) -> Sequence[str]:
614614
return self.PATH_ARG_RE.findall(self.resource_path or '')
615615

616+
@property
617+
def resource_path_formatted(self) -> str:
618+
"""
619+
Returns a formatted version of `resource_path`. This re-writes
620+
patterns like: 'projects/{project}/metricDescriptors/{metric_descriptor=**}'
621+
to 'projects/{project}/metricDescriptors/{metric_descriptor}
622+
so it can be used in an f-string.
623+
"""
624+
return self.PATH_ARG_RE.sub(r"{\g<1>}", self.resource_path or '')
625+
616626
@utils.cached_property
617627
def path_regex_str(self) -> str:
618628
# The indirection here is a little confusing:

packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
195195
@staticmethod
196196
def {{ message.resource_type|snake_case }}_path({% for arg in message.resource_path_args %}{{ arg }}: str,{% endfor %}) -> str:
197197
"""Returns a fully-qualified {{ message.resource_type|snake_case }} string."""
198-
return "{{ message.resource_path }}".format({% for arg in message.resource_path_args %}{{ arg }}={{ arg }}, {% endfor %})
198+
return "{{ message.resource_path_formatted }}".format({% for arg in message.resource_path_args %}{{ arg }}={{ arg }}, {% endfor %})
199199

200200

201201
@staticmethod

packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,7 +1543,7 @@ def test_{{ message.resource_type|snake_case }}_path():
15431543
{% for arg in message.resource_path_args %}
15441544
{{ arg }} = "{{ molluscs.next() }}"
15451545
{% endfor %}
1546-
expected = "{{ message.resource_path }}".format({% for arg in message.resource_path_args %}{{ arg }}={{ arg }}, {% endfor %})
1546+
expected = "{{ message.resource_path_formatted }}".format({% for arg in message.resource_path_args %}{{ arg }}={{ arg }}, {% endfor %})
15471547
actual = {{ service.client_name }}.{{ message.resource_type|snake_case }}_path({{message.resource_path_args|join(", ") }})
15481548
assert expected == actual
15491549

@@ -1566,7 +1566,7 @@ def test_common_{{ resource_msg.message_type.resource_type|snake_case }}_path():
15661566
{% for arg in resource_msg.message_type.resource_path_args %}
15671567
{{ arg }} = "{{ molluscs.next() }}"
15681568
{% endfor %}
1569-
expected = "{{ resource_msg.message_type.resource_path }}".format({% for arg in resource_msg.message_type.resource_path_args %}{{ arg }}={{ arg }}, {% endfor %})
1569+
expected = "{{ resource_msg.message_type.resource_path_formatted }}".format({% for arg in resource_msg.message_type.resource_path_args %}{{ arg }}={{ arg }}, {% endfor %})
15701570
actual = {{ service.client_name }}.common_{{ resource_msg.message_type.resource_type|snake_case }}_path({{ resource_msg.message_type.resource_path_args|join(", ") }})
15711571
assert expected == actual
15721572

packages/gapic-generator/tests/unit/schema/wrappers/test_message.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ def test_resource_path_with_wildcard():
221221
"kingdoms/my-kingdom/phyla/my-phylum/classes/my-klass/additional-segment")
222222
assert re.match(message.path_regex_str,
223223
"kingdoms/my-kingdom/phyla/my-phylum/classes/") is None
224+
assert message.resource_path_formatted == "kingdoms/{kingdom}/phyla/{phylum}/classes/{klass}"
224225

225226

226227
def test_resource_path_pure_wildcard():

0 commit comments

Comments
 (0)