Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.

Commit 8e1b384

Browse files
authored
fix: the common resources are not targets for lookup (#650)
The five common resources of Google Cloud are file-level options that are rendered separately from the rest of the APIs resources.
1 parent 8abbc52 commit 8e1b384

7 files changed

Lines changed: 24 additions & 21 deletions

File tree

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
@@ -141,7 +141,7 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
141141
m = re.match(r"{{ message.path_regex_str }}", path)
142142
return m.groupdict() if m else {}
143143
{% endfor %}
144-
{% for resource_msg in service.common_resources|sort(attribute="type_name") -%}
144+
{% for resource_msg in service.common_resources.values()|sort(attribute="type_name") -%}
145145
@staticmethod
146146
def common_{{ resource_msg.message_type.resource_type|snake_case }}_path({% for arg in resource_msg.message_type.resource_path_args %}{{ arg }}: str, {%endfor %}) -> str:
147147
"""Return a fully-qualified {{ resource_msg.message_type.resource_type|snake_case }} string."""

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ def test_parse_{{ message.resource_type|snake_case }}_path():
738738
assert expected == actual
739739

740740
{% endfor -%}
741-
{% for resource_msg in service.common_resources -%}
741+
{% for resource_msg in service.common_resources.values()|sort(attribute="type_name") -%}
742742
def test_common_{{ resource_msg.message_type.resource_type|snake_case }}_path():
743743
{% for arg in resource_msg.message_type.resource_path_args -%}
744744
{{ arg }} = "{{ molluscs.next() }}"

gapic/schema/wrappers.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -909,29 +909,29 @@ class Service:
909909
default_factory=metadata.Metadata,
910910
)
911911

912-
common_resources: ClassVar[Sequence[CommonResource]] = dataclasses.field(
913-
default=(
914-
CommonResource(
912+
common_resources: ClassVar[Mapping[str, CommonResource]] = dataclasses.field(
913+
default={
914+
"cloudresourcemanager.googleapis.com/Project": CommonResource(
915915
"cloudresourcemanager.googleapis.com/Project",
916916
"projects/{project}",
917917
),
918-
CommonResource(
918+
"cloudresourcemanager.googleapis.com/Organization": CommonResource(
919919
"cloudresourcemanager.googleapis.com/Organization",
920920
"organizations/{organization}",
921921
),
922-
CommonResource(
922+
"cloudresourcemanager.googleapis.com/Folder": CommonResource(
923923
"cloudresourcemanager.googleapis.com/Folder",
924924
"folders/{folder}",
925925
),
926-
CommonResource(
926+
"cloudbilling.googleapis.com/BillingAccount": CommonResource(
927927
"cloudbilling.googleapis.com/BillingAccount",
928928
"billingAccounts/{billing_account}",
929929
),
930-
CommonResource(
930+
"locations.googleapis.com/Location": CommonResource(
931931
"locations.googleapis.com/Location",
932932
"projects/{project}/locations/{location}",
933933
),
934-
),
934+
},
935935
init=False,
936936
compare=False,
937937
)
@@ -1051,7 +1051,10 @@ def gen_indirect_resources_used(message):
10511051
resource = field.options.Extensions[
10521052
resource_pb2.resource_reference]
10531053
resource_type = resource.type or resource.child_type
1054-
if resource_type:
1054+
# The common resources are defined (and rendered) explicitly
1055+
# by separate logic, and the resource definitions are never
1056+
# visible in any of the APIs file descriptor protos.
1057+
if resource_type and resource_type not in self.common_resources:
10551058
yield self.visible_resources[resource_type]
10561059

10571060
return frozenset(

gapic/templates/%namespace/%name_%version/%sub/services/%service/async_client.py.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class {{ service.async_client_name }}:
4242
{{ message.resource_type|snake_case }}_path = staticmethod({{ service.client_name }}.{{ message.resource_type|snake_case }}_path)
4343
parse_{{ message.resource_type|snake_case}}_path = staticmethod({{ service.client_name }}.parse_{{ message.resource_type|snake_case }}_path)
4444
{% endfor %}
45-
{% for resource_msg in service.common_resources %}
45+
{% for resource_msg in service.common_resources.values()|sort(attribute="type_name") %}
4646
common_{{ resource_msg.message_type.resource_type|snake_case }}_path = staticmethod({{ service.client_name }}.common_{{ resource_msg.message_type.resource_type|snake_case }}_path)
4747
parse_common_{{ resource_msg.message_type.resource_type|snake_case }}_path = staticmethod({{ service.client_name }}.parse_common_{{ resource_msg.message_type.resource_type|snake_case }}_path)
4848
{% endfor %}

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
@@ -148,7 +148,7 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
148148
return m.groupdict() if m else {}
149149

150150
{% endfor %} {# resources #}
151-
{% for resource_msg in service.common_resources|sort(attribute="type_name") -%}
151+
{% for resource_msg in service.common_resources.values()|sort(attribute="type_name") -%}
152152
@staticmethod
153153
def common_{{ resource_msg.message_type.resource_type|snake_case }}_path({% for arg in resource_msg.message_type.resource_path_args %}{{ arg }}: str, {%endfor %}) -> str:
154154
"""Return a fully-qualified {{ resource_msg.message_type.resource_type|snake_case }} string."""

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1328,7 +1328,7 @@ def test_parse_{{ message.resource_type|snake_case }}_path():
13281328
assert expected == actual
13291329

13301330
{% endfor -%}
1331-
{% for resource_msg in service.common_resources -%}
1331+
{% for resource_msg in service.common_resources.values()|sort(attribute="type_name") -%}
13321332
def test_common_{{ resource_msg.message_type.resource_type|snake_case }}_path():
13331333
{% for arg in resource_msg.message_type.resource_path_args -%}
13341334
{{ arg }} = "{{ molluscs.next() }}"

tests/unit/schema/wrappers/test_service.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -311,28 +311,28 @@ def test_has_pagers():
311311
def test_default_common_resources():
312312
service = make_service(name="MolluscMaker")
313313

314-
assert service.common_resources == (
315-
CommonResource(
314+
assert service.common_resources == {
315+
"cloudresourcemanager.googleapis.com/Project": CommonResource(
316316
"cloudresourcemanager.googleapis.com/Project",
317317
"projects/{project}",
318318
),
319-
CommonResource(
319+
"cloudresourcemanager.googleapis.com/Organization": CommonResource(
320320
"cloudresourcemanager.googleapis.com/Organization",
321321
"organizations/{organization}",
322322
),
323-
CommonResource(
323+
"cloudresourcemanager.googleapis.com/Folder": CommonResource(
324324
"cloudresourcemanager.googleapis.com/Folder",
325325
"folders/{folder}",
326326
),
327-
CommonResource(
327+
"cloudbilling.googleapis.com/BillingAccount": CommonResource(
328328
"cloudbilling.googleapis.com/BillingAccount",
329329
"billingAccounts/{billing_account}",
330330
),
331-
CommonResource(
331+
"locations.googleapis.com/Location": CommonResource(
332332
"locations.googleapis.com/Location",
333333
"projects/{project}/locations/{location}",
334334
),
335-
)
335+
}
336336

337337

338338
def test_common_resource_patterns():

0 commit comments

Comments
 (0)