Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle message bodies #1117

Merged
merged 2 commits into from
Dec 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions gapic/schema/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,20 @@ def map(self) -> bool:
return bool(self.repeated and self.message and self.message.map)

@utils.cached_property
def mock_value_original_type(self) -> Union[bool, str, bytes, int, float, List[Any], None]:
def mock_value_original_type(self) -> Union[bool, str, bytes, int, float, Dict[str,Any], List[Any], None]:
# Return messages as dicts and let the message ctor handle the conversion.
if self.message:
if self.map:
# Not worth the hassle, just return an empty map.
return {}

msg_dict = {
f.name: f.mock_value_original_type
for f in self.message.fields.values()
}

return [msg_dict] if self.repeated else msg_dict

answer = self.primitive_mock() or None

# If this is a repeated field, then the mock answer should
Expand Down Expand Up @@ -173,7 +186,7 @@ def primitive_mock(self, suffix: int = 0) -> Union[bool, str, bytes, int, float,
answer: Union[bool, str, bytes, int, float, List[Any], None] = None

if not isinstance(self.type, PrimitiveType):
raise TypeError(f"'inner_mock_as_original_type' can only be used for"
raise TypeError(f"'primitive_mock' can only be used for "
f"PrimitiveType, but type is {self.type}")

else:
Expand Down
53 changes: 53 additions & 0 deletions tests/fragments/test_non_primitive_body.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (C) 2021 Google LLC
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this file used? If yes, can you please explain how (I could not find any referrences to it from other parts of this PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's used in the fragment tests. For each proto in this directory, we generate a GAPIC library and run its generated unit tests. This stresses both the templates and the actual logic of the generated surface. See noxfile.py:fragment for more detail on how the fragment tests work.
This particular fragment is the minimum required reproduction for the issues that compute was having.

//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

package google.fragment;

import "google/api/client.proto";
import "google/api/annotations.proto";
import "google/api/field_behavior.proto";

service SmallCompute {
option (google.api.default_host) = "my.example.com";

rpc MyMethod(MethodRequest) returns (MethodResponse) {
option (google.api.http) = {
body: "method_body"
post: "/computation/v1/first_name/{first_name}/last_name/{last_name}"
};
};
}

message SerialNumber {
int32 number = 1;
}

message MethodRequest {
message MethodBody {
int32 mass_kg = 1;
int32 length_cm = 2;
repeated SerialNumber serial_numbers = 3;
map<string, SerialNumber> word_associations = 4;
}

string first_name = 1 [(google.api.field_behavior) = REQUIRED];
string last_name = 2 [(google.api.field_behavior) = REQUIRED];
MethodBody method_body = 3 [(google.api.field_behavior) = REQUIRED];
}

message MethodResponse {
string name = 1;
}