Skip to content

Commit

Permalink
fix: fix regression in REST unit test (#1798)
Browse files Browse the repository at this point in the history
  • Loading branch information
parthea authored Oct 12, 2023
1 parent 14eec93 commit 0cee3c2
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1019,23 +1019,31 @@ def test_{{ method.name|snake_case }}_rest(request_type):
# See https://github.com/googleapis/gapic-generator-python/issues/1748

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

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

subfields_not_in_runtime = []
if hasattr(field, "message") and field.message:
is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR")

if is_field_type_proto_plus_type:
message_fields = field.message.meta.fields.values()
else:
message_fields = field.message.DESCRIPTOR.fields
return message_fields

# Get all subfields for the message
runtime_nested_fields = [
(field.name, subfield.name)
for field in message_fields
if hasattr(field, "message_type") and field.message_type
for subfield in field.message_type.fields
(field.name, nested_field.name)
for field in get_message_fields(test_field)
for nested_field in get_message_fields(field)
]

subfields_not_in_runtime = []

# For each item in the sample request, create a list of sub fields which are not present at runtime
for field, value in request_init["{{ field.name }}"].items():
result = None
Expand Down
30 changes: 19 additions & 11 deletions gapic/templates/tests/unit/gapic/%name_%version/%sub/test_macros.j2
Original file line number Diff line number Diff line change
Expand Up @@ -892,23 +892,31 @@ def test_{{ method_name }}_rest(request_type):
# See https://github.com/googleapis/gapic-generator-python/issues/1748

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

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

subfields_not_in_runtime = []
if hasattr(field, "message") and field.message:
is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR")

if is_field_type_proto_plus_type:
message_fields = field.message.meta.fields.values()
else:
message_fields = field.message.DESCRIPTOR.fields
return message_fields

# Get all subfields for the message
runtime_nested_fields = [
(field.name, subfield.name)
for field in message_fields
if hasattr(field, "message_type") and field.message_type
for subfield in field.message_type.fields
(field.name, nested_field.name)
for field in get_message_fields(test_field)
for nested_field in get_message_fields(field)
]

subfields_not_in_runtime = []

# For each item in the sample request, create a list of sub fields which are not present at runtime
for field, value in request_init["{{ field.name }}"].items():
result = None
Expand Down
11 changes: 9 additions & 2 deletions tests/fragments/test_google_protobuf_type.proto
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,27 @@ service MyServiceWithProtobufType {
rpc MyMethod(MethodRequestWithProtobufType)
returns (MethodResponseWithProtobufType) {
option (google.api.http) = {
post: "/v1/services/{service_name}/configs"
post: "/v1/services/{service_name}/configs/{test_message.another_message.another_field}"
body: "test_message"
};
option (google.api.method_signature) = "service_name,test_message";
option (google.api.method_signature) = "service_name,test_message,another_string";
}
}

message MethodRequestWithProtobufType {
string service_name = 1 [(google.api.field_behavior) = REQUIRED];
TestMessage test_message = 2 [(google.api.field_behavior) = REQUIRED];
string another_string = 3 [(google.api.field_behavior) = REQUIRED];
}

message TestMessage {
string name = 1 [(google.api.field_behavior) = REQUIRED];
repeated google.protobuf.Type types = 2 [(google.api.field_behavior) = REQUIRED];
AnotherTestMessage another_message = 3 [(google.api.field_behavior) = REQUIRED];
}

message AnotherTestMessage {
string another_field = 1;
}

message MethodResponseWithProtobufType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9998,23 +9998,31 @@ def test_create_saved_query_rest(request_type):
# See https://github.com/googleapis/gapic-generator-python/issues/1748

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

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

subfields_not_in_runtime = []
if hasattr(field, "message") and field.message:
is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR")

if is_field_type_proto_plus_type:
message_fields = field.message.meta.fields.values()
else:
message_fields = field.message.DESCRIPTOR.fields
return message_fields

# Get all subfields for the message
runtime_nested_fields = [
(field.name, subfield.name)
for field in message_fields
if hasattr(field, "message_type") and field.message_type
for subfield in field.message_type.fields
(field.name, nested_field.name)
for field in get_message_fields(test_field)
for nested_field in get_message_fields(field)
]

subfields_not_in_runtime = []

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

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

if is_message_proto_plus_type:
message_fields = asset_service.UpdateSavedQueryRequest.meta.fields["saved_query"].message.meta.fields
else:
message_fields = asset_service.UpdateSavedQueryRequest.meta.fields["saved_query"].message.DESCRIPTOR.fields
def get_message_fields(field):
# Given a field which is a message (composite type), return a list with
# all the fields of the message.
# If the field is not a composite type, return an empty list.
message_fields = []

subfields_not_in_runtime = []
if hasattr(field, "message") and field.message:
is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR")

if is_field_type_proto_plus_type:
message_fields = field.message.meta.fields.values()
else:
message_fields = field.message.DESCRIPTOR.fields
return message_fields

# Get all subfields for the message
runtime_nested_fields = [
(field.name, subfield.name)
for field in message_fields
if hasattr(field, "message_type") and field.message_type
for subfield in field.message_type.fields
(field.name, nested_field.name)
for field in get_message_fields(test_field)
for nested_field in get_message_fields(field)
]

subfields_not_in_runtime = []

# For each item in the sample request, create a list of sub fields which are not present at runtime
for field, value in request_init["saved_query"].items():
result = None
Expand Down
Loading

0 comments on commit 0cee3c2

Please sign in to comment.