Skip to content

Commit 66dd984

Browse files
authored
fix: fix regression in REST unit test (#1798)
1 parent e3b75b3 commit 66dd984

File tree

6 files changed

+237
-134
lines changed

6 files changed

+237
-134
lines changed

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,23 +1019,31 @@ def test_{{ method.name|snake_case }}_rest(request_type):
10191019
# See https://github.com/googleapis/gapic-generator-python/issues/1748
10201020

10211021
# Determine if the message type is proto-plus or protobuf
1022-
is_message_proto_plus_type = not hasattr({{ method.input.ident }}.meta.fields["{{ field.name }}"].message, "DESCRIPTOR")
1022+
test_field = {{ method.input.ident }}.meta.fields["{{ field.name }}"]
10231023

1024-
if is_message_proto_plus_type:
1025-
message_fields = {{ method.input.ident }}.meta.fields["{{ field.name }}"].message.meta.fields
1026-
else:
1027-
message_fields = {{ method.input.ident }}.meta.fields["{{ field.name }}"].message.DESCRIPTOR.fields
1024+
def get_message_fields(field):
1025+
# Given a field which is a message (composite type), return a list with
1026+
# all the fields of the message.
1027+
# If the field is not a composite type, return an empty list.
1028+
message_fields = []
10281029

1029-
subfields_not_in_runtime = []
1030+
if hasattr(field, "message") and field.message:
1031+
is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR")
1032+
1033+
if is_field_type_proto_plus_type:
1034+
message_fields = field.message.meta.fields.values()
1035+
else:
1036+
message_fields = field.message.DESCRIPTOR.fields
1037+
return message_fields
10301038

1031-
# Get all subfields for the message
10321039
runtime_nested_fields = [
1033-
(field.name, subfield.name)
1034-
for field in message_fields
1035-
if hasattr(field, "message_type") and field.message_type
1036-
for subfield in field.message_type.fields
1040+
(field.name, nested_field.name)
1041+
for field in get_message_fields(test_field)
1042+
for nested_field in get_message_fields(field)
10371043
]
10381044

1045+
subfields_not_in_runtime = []
1046+
10391047
# For each item in the sample request, create a list of sub fields which are not present at runtime
10401048
for field, value in request_init["{{ field.name }}"].items():
10411049
result = None

packages/gapic-generator/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_macros.j2

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -892,23 +892,31 @@ def test_{{ method_name }}_rest(request_type):
892892
# See https://github.com/googleapis/gapic-generator-python/issues/1748
893893

894894
# Determine if the message type is proto-plus or protobuf
895-
is_message_proto_plus_type = not hasattr({{ method.input.ident }}.meta.fields["{{ field.name }}"].message, "DESCRIPTOR")
895+
test_field = {{ method.input.ident }}.meta.fields["{{ field.name }}"]
896896

897-
if is_message_proto_plus_type:
898-
message_fields = {{ method.input.ident }}.meta.fields["{{ field.name }}"].message.meta.fields
899-
else:
900-
message_fields = {{ method.input.ident }}.meta.fields["{{ field.name }}"].message.DESCRIPTOR.fields
897+
def get_message_fields(field):
898+
# Given a field which is a message (composite type), return a list with
899+
# all the fields of the message.
900+
# If the field is not a composite type, return an empty list.
901+
message_fields = []
901902

902-
subfields_not_in_runtime = []
903+
if hasattr(field, "message") and field.message:
904+
is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR")
905+
906+
if is_field_type_proto_plus_type:
907+
message_fields = field.message.meta.fields.values()
908+
else:
909+
message_fields = field.message.DESCRIPTOR.fields
910+
return message_fields
903911

904-
# Get all subfields for the message
905912
runtime_nested_fields = [
906-
(field.name, subfield.name)
907-
for field in message_fields
908-
if hasattr(field, "message_type") and field.message_type
909-
for subfield in field.message_type.fields
913+
(field.name, nested_field.name)
914+
for field in get_message_fields(test_field)
915+
for nested_field in get_message_fields(field)
910916
]
911917

918+
subfields_not_in_runtime = []
919+
912920
# For each item in the sample request, create a list of sub fields which are not present at runtime
913921
for field, value in request_init["{{ field.name }}"].items():
914922
result = None

packages/gapic-generator/tests/fragments/test_google_protobuf_type.proto

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,27 @@ service MyServiceWithProtobufType {
3434
rpc MyMethod(MethodRequestWithProtobufType)
3535
returns (MethodResponseWithProtobufType) {
3636
option (google.api.http) = {
37-
post: "/v1/services/{service_name}/configs"
37+
post: "/v1/services/{service_name}/configs/{test_message.another_message.another_field}"
3838
body: "test_message"
3939
};
40-
option (google.api.method_signature) = "service_name,test_message";
40+
option (google.api.method_signature) = "service_name,test_message,another_string";
4141
}
4242
}
4343

4444
message MethodRequestWithProtobufType {
4545
string service_name = 1 [(google.api.field_behavior) = REQUIRED];
4646
TestMessage test_message = 2 [(google.api.field_behavior) = REQUIRED];
47+
string another_string = 3 [(google.api.field_behavior) = REQUIRED];
4748
}
4849

4950
message TestMessage {
51+
string name = 1 [(google.api.field_behavior) = REQUIRED];
5052
repeated google.protobuf.Type types = 2 [(google.api.field_behavior) = REQUIRED];
53+
AnotherTestMessage another_message = 3 [(google.api.field_behavior) = REQUIRED];
54+
}
55+
56+
message AnotherTestMessage {
57+
string another_field = 1;
5158
}
5259

5360
message MethodResponseWithProtobufType {

packages/gapic-generator/tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9998,23 +9998,31 @@ def test_create_saved_query_rest(request_type):
99989998
# See https://github.com/googleapis/gapic-generator-python/issues/1748
99999999

1000010000
# Determine if the message type is proto-plus or protobuf
10001-
is_message_proto_plus_type = not hasattr(asset_service.CreateSavedQueryRequest.meta.fields["saved_query"].message, "DESCRIPTOR")
10001+
test_field = asset_service.CreateSavedQueryRequest.meta.fields["saved_query"]
1000210002

10003-
if is_message_proto_plus_type:
10004-
message_fields = asset_service.CreateSavedQueryRequest.meta.fields["saved_query"].message.meta.fields
10005-
else:
10006-
message_fields = asset_service.CreateSavedQueryRequest.meta.fields["saved_query"].message.DESCRIPTOR.fields
10003+
def get_message_fields(field):
10004+
# Given a field which is a message (composite type), return a list with
10005+
# all the fields of the message.
10006+
# If the field is not a composite type, return an empty list.
10007+
message_fields = []
1000710008

10008-
subfields_not_in_runtime = []
10009+
if hasattr(field, "message") and field.message:
10010+
is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR")
10011+
10012+
if is_field_type_proto_plus_type:
10013+
message_fields = field.message.meta.fields.values()
10014+
else:
10015+
message_fields = field.message.DESCRIPTOR.fields
10016+
return message_fields
1000910017

10010-
# Get all subfields for the message
1001110018
runtime_nested_fields = [
10012-
(field.name, subfield.name)
10013-
for field in message_fields
10014-
if hasattr(field, "message_type") and field.message_type
10015-
for subfield in field.message_type.fields
10019+
(field.name, nested_field.name)
10020+
for field in get_message_fields(test_field)
10021+
for nested_field in get_message_fields(field)
1001610022
]
1001710023

10024+
subfields_not_in_runtime = []
10025+
1001810026
# For each item in the sample request, create a list of sub fields which are not present at runtime
1001910027
for field, value in request_init["saved_query"].items():
1002010028
result = None
@@ -10843,23 +10851,31 @@ def test_update_saved_query_rest(request_type):
1084310851
# See https://github.com/googleapis/gapic-generator-python/issues/1748
1084410852

1084510853
# Determine if the message type is proto-plus or protobuf
10846-
is_message_proto_plus_type = not hasattr(asset_service.UpdateSavedQueryRequest.meta.fields["saved_query"].message, "DESCRIPTOR")
10854+
test_field = asset_service.UpdateSavedQueryRequest.meta.fields["saved_query"]
1084710855

10848-
if is_message_proto_plus_type:
10849-
message_fields = asset_service.UpdateSavedQueryRequest.meta.fields["saved_query"].message.meta.fields
10850-
else:
10851-
message_fields = asset_service.UpdateSavedQueryRequest.meta.fields["saved_query"].message.DESCRIPTOR.fields
10856+
def get_message_fields(field):
10857+
# Given a field which is a message (composite type), return a list with
10858+
# all the fields of the message.
10859+
# If the field is not a composite type, return an empty list.
10860+
message_fields = []
1085210861

10853-
subfields_not_in_runtime = []
10862+
if hasattr(field, "message") and field.message:
10863+
is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR")
10864+
10865+
if is_field_type_proto_plus_type:
10866+
message_fields = field.message.meta.fields.values()
10867+
else:
10868+
message_fields = field.message.DESCRIPTOR.fields
10869+
return message_fields
1085410870

10855-
# Get all subfields for the message
1085610871
runtime_nested_fields = [
10857-
(field.name, subfield.name)
10858-
for field in message_fields
10859-
if hasattr(field, "message_type") and field.message_type
10860-
for subfield in field.message_type.fields
10872+
(field.name, nested_field.name)
10873+
for field in get_message_fields(test_field)
10874+
for nested_field in get_message_fields(field)
1086110875
]
1086210876

10877+
subfields_not_in_runtime = []
10878+
1086310879
# For each item in the sample request, create a list of sub fields which are not present at runtime
1086410880
for field, value in request_init["saved_query"].items():
1086510881
result = None

0 commit comments

Comments
 (0)