Skip to content

Commit 588cda9

Browse files
authored
fix: resolve collision with reserved words in samples (#17912)
Towards b/537730936
1 parent 42323c9 commit 588cda9

6 files changed

Lines changed: 129 additions & 4 deletions

File tree

packages/gapic-generator/gapic/samplegen/samplegen.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ def preprocess_sample(sample, api_schema: api.API, rpc: wrappers.Method):
318318
sample["client_name"] = (
319319
service.async_client_name if is_async else service.client_name
320320
)
321+
sample["client_method_name"] = rpc.client_method_name
321322

322323
# the MessageType of the request object passed to the rpc e.g., `ListRequest`
323324
sample["request_type"] = rpc.input

packages/gapic-generator/gapic/templates/examples/feature_fragments.j2

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,11 @@ response = {% if transport == "grpc-async" %}await {% endif %}operation.result()
297297
{% endmacro %}
298298

299299
{% macro render_method_name(sample) %}
300-
{% if sample.is_internal %}
301-
_{{ sample.rpc|snake_case }}
300+
{% set name = (sample.client_method_name|default(sample.rpc, true))|snake_case %}
301+
{% if sample.is_internal and not name.startswith('_') %}
302+
_{{ name }}
302303
{% else %}
303-
{{ sample.rpc|snake_case }}
304+
{{ name }}
304305
{% endif %}
305306
{% endmacro %}
306307

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2026 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
# Generated code. DO NOT EDIT!
17+
#
18+
# Snippet for Import
19+
# NOTE: This snippet has been automatically generated for illustrative purposes only.
20+
# It may require modifications to work in your environment.
21+
22+
# To install the latest published package dependency, execute the following:
23+
# python3 -m pip install animalia-mollusca
24+
25+
26+
# [START mollusca_v1_generated_Snippets_Import_async]
27+
# This snippet has been automatically generated and should be regarded as a
28+
# code template only.
29+
# It will require modifications to work:
30+
# - It may require correct/in-range values for request initialization.
31+
# - It may require specifying regional endpoints when creating the service
32+
# client as shown in:
33+
# https://googleapis.dev/python/google-api-core/latest/client_options.html
34+
from animalia import mollusca_v1
35+
36+
37+
async def sample_import():
38+
# Create a client
39+
client = mollusca_v1.SnippetsAsyncClient()
40+
41+
# Initialize request argument(s)
42+
my_message = mollusca_v1.MessageWithNesting()
43+
my_message.message.required_string = "required_string_value"
44+
my_message.my_int = 656
45+
46+
request = mollusca_v1.SignatureRequest(
47+
my_string="my_string_value",
48+
my_int=656,
49+
my_bool=True,
50+
my_message=my_message,
51+
single_enum="DEFAULT",
52+
)
53+
54+
# Make the request
55+
response = await client.import_(request=request)
56+
57+
# Handle the response
58+
print(response)
59+
60+
# [END mollusca_v1_generated_Snippets_Import_async]
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2026 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
# Generated code. DO NOT EDIT!
17+
#
18+
# Snippet for Import
19+
# NOTE: This snippet has been automatically generated for illustrative purposes only.
20+
# It may require modifications to work in your environment.
21+
22+
# To install the latest published package dependency, execute the following:
23+
# python3 -m pip install animalia-mollusca
24+
25+
26+
# [START mollusca_v1_generated_Snippets_Import_sync]
27+
# This snippet has been automatically generated and should be regarded as a
28+
# code template only.
29+
# It will require modifications to work:
30+
# - It may require correct/in-range values for request initialization.
31+
# - It may require specifying regional endpoints when creating the service
32+
# client as shown in:
33+
# https://googleapis.dev/python/google-api-core/latest/client_options.html
34+
from animalia import mollusca_v1
35+
36+
37+
def sample_import():
38+
# Create a client
39+
client = mollusca_v1.SnippetsClient()
40+
41+
# Initialize request argument(s)
42+
my_message = mollusca_v1.MessageWithNesting()
43+
my_message.message.required_string = "required_string_value"
44+
my_message.my_int = 656
45+
46+
request = mollusca_v1.SignatureRequest(
47+
my_string="my_string_value",
48+
my_int=656,
49+
my_bool=True,
50+
my_message=my_message,
51+
single_enum="DEFAULT",
52+
)
53+
54+
# Make the request
55+
response = client.import_(request=request)
56+
57+
# Handle the response
58+
print(response)
59+
60+
# [END mollusca_v1_generated_Snippets_Import_sync]

packages/gapic-generator/tests/snippetgen/snippets.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ service Snippets {
5757
rpc OneOfMethod(OneOfRequest) returns (Response);
5858

5959
rpc OneOfMethodRequiredField(OneOfRequestWithRequiredField) returns (Response);
60+
61+
rpc Import(SignatureRequest) returns (Response);
6062
}
6163

6264
enum Enum {

packages/gapic-generator/tests/unit/common_types.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ class DummyMethod:
4545

4646
@property
4747
def client_method_name(self):
48-
method_wrapper = make_method(name=self.name, is_internal=self.is_internal)
48+
name = self.name if isinstance(self.name, str) else ""
49+
method_wrapper = make_method(name=name, is_internal=self.is_internal)
4950
return method_wrapper.client_method_name
5051

5152

0 commit comments

Comments
 (0)