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

Commit 4ecfbeb

Browse files
authored
fix: resolve rare issue where ValueError is not raised when both request and flattened param are set (#2258)
1 parent 259f85e commit 4ecfbeb

File tree

26 files changed

+479
-219
lines changed

26 files changed

+479
-219
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,8 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
410410
{% if method.flattened_fields %}
411411
# Quick check: If we got a request object, we should *not* have
412412
# gotten any keyword arguments that map to the request.
413-
has_flattened_params = any([{{ method.flattened_fields.values()|join(", ", attribute="name") }}])
413+
flattened_params = [{{ method.flattened_fields.values()|join(", ", attribute="name") }}]
414+
has_flattened_params = len([param for param in flattened_params if param is not None]) > 0
414415
if request is not None and has_flattened_params:
415416
raise ValueError('If the `request` argument is set, then none of '
416417
'the individual field arguments should be set.')

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@
9090
{% if method.flattened_fields %}
9191
# - Quick check: If we got a request object, we should *not* have
9292
# gotten any keyword arguments that map to the request.
93-
has_flattened_params = any([{{ method.flattened_fields.values()|join(", ", attribute="name") }}])
93+
flattened_params = [{{ method.flattened_fields.values()|join(", ", attribute="name") }}]
94+
has_flattened_params = len([param for param in flattened_params if param is not None]) > 0
9495
if request is not None and has_flattened_params:
9596
raise ValueError('If the `request` argument is set, then none of '
9697
'the individual field arguments should be set.')

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,8 @@ class {{ service.async_client_name }}:
339339
{% if method.flattened_fields %}
340340
# - Quick check: If we got a request object, we should *not* have
341341
# gotten any keyword arguments that map to the request.
342-
has_flattened_params = any([{{ method.flattened_fields.values()|join(", ", attribute="name") }}])
342+
flattened_params = [{{ method.flattened_fields.values()|join(", ", attribute="name") }}]
343+
has_flattened_params = len([param for param in flattened_params if param is not None]) > 0
343344
if request is not None and has_flattened_params:
344345
raise ValueError("If the `request` argument is set, then none of "
345346
"the individual field arguments should be set.")
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (C) 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
syntax = "proto3";
15+
16+
package google.fragment;
17+
18+
import "google/protobuf/struct.proto";
19+
import "google/api/client.proto";
20+
21+
service MyServiceWithNestedMessages {
22+
option (google.api.default_host) = "my.example.com";
23+
24+
rpc MyMethod(MethodRequest) returns (MethodResponse) {
25+
option (google.api.method_signature) = "some_message";
26+
}
27+
}
28+
29+
message AnotherMessage {};
30+
31+
message SomeMessage{
32+
AnotherMessage another_message = 1;
33+
}
34+
35+
message MethodRequest {
36+
SomeMessage some_message = 1;
37+
}
38+
39+
message MethodResponse {
40+
google.protobuf.Value result = 1;
41+
}

tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/async_client.py

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,8 @@ async def sample_list_assets():
459459
# Create or coerce a protobuf request object.
460460
# - Quick check: If we got a request object, we should *not* have
461461
# gotten any keyword arguments that map to the request.
462-
has_flattened_params = any([parent])
462+
flattened_params = [parent]
463+
has_flattened_params = len([param for param in flattened_params if param is not None]) > 0
463464
if request is not None and has_flattened_params:
464465
raise ValueError("If the `request` argument is set, then none of "
465466
"the individual field arguments should be set.")
@@ -682,7 +683,8 @@ async def sample_create_feed():
682683
# Create or coerce a protobuf request object.
683684
# - Quick check: If we got a request object, we should *not* have
684685
# gotten any keyword arguments that map to the request.
685-
has_flattened_params = any([parent])
686+
flattened_params = [parent]
687+
has_flattened_params = len([param for param in flattened_params if param is not None]) > 0
686688
if request is not None and has_flattened_params:
687689
raise ValueError("If the `request` argument is set, then none of "
688690
"the individual field arguments should be set.")
@@ -794,7 +796,8 @@ async def sample_get_feed():
794796
# Create or coerce a protobuf request object.
795797
# - Quick check: If we got a request object, we should *not* have
796798
# gotten any keyword arguments that map to the request.
797-
has_flattened_params = any([name])
799+
flattened_params = [name]
800+
has_flattened_params = len([param for param in flattened_params if param is not None]) > 0
798801
if request is not None and has_flattened_params:
799802
raise ValueError("If the `request` argument is set, then none of "
800803
"the individual field arguments should be set.")
@@ -901,7 +904,8 @@ async def sample_list_feeds():
901904
# Create or coerce a protobuf request object.
902905
# - Quick check: If we got a request object, we should *not* have
903906
# gotten any keyword arguments that map to the request.
904-
has_flattened_params = any([parent])
907+
flattened_params = [parent]
908+
has_flattened_params = len([param for param in flattened_params if param is not None]) > 0
905909
if request is not None and has_flattened_params:
906910
raise ValueError("If the `request` argument is set, then none of "
907911
"the individual field arguments should be set.")
@@ -1017,7 +1021,8 @@ async def sample_update_feed():
10171021
# Create or coerce a protobuf request object.
10181022
# - Quick check: If we got a request object, we should *not* have
10191023
# gotten any keyword arguments that map to the request.
1020-
has_flattened_params = any([feed])
1024+
flattened_params = [feed]
1025+
has_flattened_params = len([param for param in flattened_params if param is not None]) > 0
10211026
if request is not None and has_flattened_params:
10221027
raise ValueError("If the `request` argument is set, then none of "
10231028
"the individual field arguments should be set.")
@@ -1114,7 +1119,8 @@ async def sample_delete_feed():
11141119
# Create or coerce a protobuf request object.
11151120
# - Quick check: If we got a request object, we should *not* have
11161121
# gotten any keyword arguments that map to the request.
1117-
has_flattened_params = any([name])
1122+
flattened_params = [name]
1123+
has_flattened_params = len([param for param in flattened_params if param is not None]) > 0
11181124
if request is not None and has_flattened_params:
11191125
raise ValueError("If the `request` argument is set, then none of "
11201126
"the individual field arguments should be set.")
@@ -1328,7 +1334,8 @@ async def sample_search_all_resources():
13281334
# Create or coerce a protobuf request object.
13291335
# - Quick check: If we got a request object, we should *not* have
13301336
# gotten any keyword arguments that map to the request.
1331-
has_flattened_params = any([scope, query, asset_types])
1337+
flattened_params = [scope, query, asset_types]
1338+
has_flattened_params = len([param for param in flattened_params if param is not None]) > 0
13321339
if request is not None and has_flattened_params:
13331340
raise ValueError("If the `request` argument is set, then none of "
13341341
"the individual field arguments should be set.")
@@ -1523,7 +1530,8 @@ async def sample_search_all_iam_policies():
15231530
# Create or coerce a protobuf request object.
15241531
# - Quick check: If we got a request object, we should *not* have
15251532
# gotten any keyword arguments that map to the request.
1526-
has_flattened_params = any([scope, query])
1533+
flattened_params = [scope, query]
1534+
has_flattened_params = len([param for param in flattened_params if param is not None]) > 0
15271535
if request is not None and has_flattened_params:
15281536
raise ValueError("If the `request` argument is set, then none of "
15291537
"the individual field arguments should be set.")
@@ -2067,7 +2075,8 @@ async def sample_create_saved_query():
20672075
# Create or coerce a protobuf request object.
20682076
# - Quick check: If we got a request object, we should *not* have
20692077
# gotten any keyword arguments that map to the request.
2070-
has_flattened_params = any([parent, saved_query, saved_query_id])
2078+
flattened_params = [parent, saved_query, saved_query_id]
2079+
has_flattened_params = len([param for param in flattened_params if param is not None]) > 0
20712080
if request is not None and has_flattened_params:
20722081
raise ValueError("If the `request` argument is set, then none of "
20732082
"the individual field arguments should be set.")
@@ -2179,7 +2188,8 @@ async def sample_get_saved_query():
21792188
# Create or coerce a protobuf request object.
21802189
# - Quick check: If we got a request object, we should *not* have
21812190
# gotten any keyword arguments that map to the request.
2182-
has_flattened_params = any([name])
2191+
flattened_params = [name]
2192+
has_flattened_params = len([param for param in flattened_params if param is not None]) > 0
21832193
if request is not None and has_flattened_params:
21842194
raise ValueError("If the `request` argument is set, then none of "
21852195
"the individual field arguments should be set.")
@@ -2293,7 +2303,8 @@ async def sample_list_saved_queries():
22932303
# Create or coerce a protobuf request object.
22942304
# - Quick check: If we got a request object, we should *not* have
22952305
# gotten any keyword arguments that map to the request.
2296-
has_flattened_params = any([parent])
2306+
flattened_params = [parent]
2307+
has_flattened_params = len([param for param in flattened_params if param is not None]) > 0
22972308
if request is not None and has_flattened_params:
22982309
raise ValueError("If the `request` argument is set, then none of "
22992310
"the individual field arguments should be set.")
@@ -2421,7 +2432,8 @@ async def sample_update_saved_query():
24212432
# Create or coerce a protobuf request object.
24222433
# - Quick check: If we got a request object, we should *not* have
24232434
# gotten any keyword arguments that map to the request.
2424-
has_flattened_params = any([saved_query, update_mask])
2435+
flattened_params = [saved_query, update_mask]
2436+
has_flattened_params = len([param for param in flattened_params if param is not None]) > 0
24252437
if request is not None and has_flattened_params:
24262438
raise ValueError("If the `request` argument is set, then none of "
24272439
"the individual field arguments should be set.")
@@ -2522,7 +2534,8 @@ async def sample_delete_saved_query():
25222534
# Create or coerce a protobuf request object.
25232535
# - Quick check: If we got a request object, we should *not* have
25242536
# gotten any keyword arguments that map to the request.
2525-
has_flattened_params = any([name])
2537+
flattened_params = [name]
2538+
has_flattened_params = len([param for param in flattened_params if param is not None]) > 0
25262539
if request is not None and has_flattened_params:
25272540
raise ValueError("If the `request` argument is set, then none of "
25282541
"the individual field arguments should be set.")
@@ -2744,7 +2757,8 @@ async def sample_analyze_org_policies():
27442757
# Create or coerce a protobuf request object.
27452758
# - Quick check: If we got a request object, we should *not* have
27462759
# gotten any keyword arguments that map to the request.
2747-
has_flattened_params = any([scope, constraint, filter])
2760+
flattened_params = [scope, constraint, filter]
2761+
has_flattened_params = len([param for param in flattened_params if param is not None]) > 0
27482762
if request is not None and has_flattened_params:
27492763
raise ValueError("If the `request` argument is set, then none of "
27502764
"the individual field arguments should be set.")
@@ -2898,7 +2912,8 @@ async def sample_analyze_org_policy_governed_containers():
28982912
# Create or coerce a protobuf request object.
28992913
# - Quick check: If we got a request object, we should *not* have
29002914
# gotten any keyword arguments that map to the request.
2901-
has_flattened_params = any([scope, constraint, filter])
2915+
flattened_params = [scope, constraint, filter]
2916+
has_flattened_params = len([param for param in flattened_params if param is not None]) > 0
29022917
if request is not None and has_flattened_params:
29032918
raise ValueError("If the `request` argument is set, then none of "
29042919
"the individual field arguments should be set.")
@@ -3081,7 +3096,8 @@ async def sample_analyze_org_policy_governed_assets():
30813096
# Create or coerce a protobuf request object.
30823097
# - Quick check: If we got a request object, we should *not* have
30833098
# gotten any keyword arguments that map to the request.
3084-
has_flattened_params = any([scope, constraint, filter])
3099+
flattened_params = [scope, constraint, filter]
3100+
has_flattened_params = len([param for param in flattened_params if param is not None]) > 0
30853101
if request is not None and has_flattened_params:
30863102
raise ValueError("If the `request` argument is set, then none of "
30873103
"the individual field arguments should be set.")

0 commit comments

Comments
 (0)