From a521d17abb8bf56839d5fc4c33df678daaf2e500 Mon Sep 17 00:00:00 2001 From: vam-google Date: Tue, 26 Oct 2021 18:19:02 -0700 Subject: [PATCH 1/2] fix: more fixes for rest transport This includes: 1) Implicit template support for grpc transcodding (accept `/v1/{project}/stuff` whichis equivalent to `/v1/{project=*}/stuff`) 2) Proper request message construction for paginated methods test 3) Depend on `google-api-core 2.2.0` for rest to accommodate the `int64` support fixes --- gapic/templates/setup.py.j2 | 2 +- .../unit/gapic/%name_%version/%sub/test_%service.py.j2 | 7 +++++++ gapic/utils/uri_sample.py | 4 +++- setup.py | 2 +- tests/unit/schema/wrappers/test_method.py | 10 ++++++++++ 5 files changed, 22 insertions(+), 3 deletions(-) diff --git a/gapic/templates/setup.py.j2 b/gapic/templates/setup.py.j2 index 7606bc71d8..7351e42968 100644 --- a/gapic/templates/setup.py.j2 +++ b/gapic/templates/setup.py.j2 @@ -29,7 +29,7 @@ setuptools.setup( install_requires=( {# TODO(dovs): remove when 1.x deprecation is complete #} {% if 'rest' in opts.transport %} - 'google-api-core[grpc] >= 2.1.0, < 3.0.0dev', + 'google-api-core[grpc] >= 2.2.0, < 3.0.0dev', {% else %} 'google-api-core[grpc] >= 1.28.0, < 3.0.0dev', {% endif %} diff --git a/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 b/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 index 53ef176529..ca0257b085 100644 --- a/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 +++ b/gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2 @@ -1337,6 +1337,13 @@ def test_{{ method.name|snake_case }}_rest_pager(): req.side_effect = return_values sample_request = {{ method.http_options[0].sample_request }} + {% for field in method.body_fields.values() %} + {% if not field.oneof or field.proto3_optional %} + {# ignore oneof fields that might conflict with sample_request #} + sample_request["{{ field.name }}"] = {{ field.mock_value }} + {% endif %} + {% endfor %} + pager = client.{{ method.name|snake_case }}(request=sample_request) {% if method.paged_result_field.map %} diff --git a/gapic/utils/uri_sample.py b/gapic/utils/uri_sample.py index 43b8865abf..0eba82220f 100644 --- a/gapic/utils/uri_sample.py +++ b/gapic/utils/uri_sample.py @@ -71,6 +71,8 @@ def sample_from_path_fields(paths: List[Tuple[str, str]]) -> Dict[Any, Any]: for path, template in paths: sample_value = re.sub( - r"(\*\*|\*)", lambda n: next(sample_names), template) + r"(\*\*|\*)", + lambda n: next(sample_names), template if template else '*' + ) add_field(request, path, sample_value) return request diff --git a/setup.py b/setup.py index 27b6c1758f..76e590d457 100644 --- a/setup.py +++ b/setup.py @@ -44,7 +44,7 @@ include_package_data=True, install_requires=( "click >= 6.7", - "google-api-core >= 2.1.0", + "google-api-core >= 2.2.0", "googleapis-common-protos >= 1.53.0", "grpcio >= 1.24.3", "jinja2 >= 2.10", diff --git a/tests/unit/schema/wrappers/test_method.py b/tests/unit/schema/wrappers/test_method.py index 72766d453e..a2d557db5d 100644 --- a/tests/unit/schema/wrappers/test_method.py +++ b/tests/unit/schema/wrappers/test_method.py @@ -475,6 +475,16 @@ def test_method_http_options_generate_sample(): 'id': 'projects/sample1/regions/sample2/id/sample3'}} +def test_method_http_options_generate_sample_immplicit_template(): + http_rule = http_pb2.HttpRule( + get='/v1/{resource.id}/stuff', + ) + method = make_method('DoSomething', http_rule=http_rule) + sample = method.http_options[0].sample_request + assert json.loads(sample) == {'resource': { + 'id': 'sample1'}} + + def test_method_query_params(): # tests only the basic case of grpc transcoding http_rule = http_pb2.HttpRule( From 29723e60d47a3e31817e9bf241922b579c412694 Mon Sep 17 00:00:00 2001 From: vam-google Date: Wed, 27 Oct 2021 12:15:53 -0700 Subject: [PATCH 2/2] fix a typo in test method name --- tests/unit/schema/wrappers/test_method.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/schema/wrappers/test_method.py b/tests/unit/schema/wrappers/test_method.py index a2d557db5d..42198e09eb 100644 --- a/tests/unit/schema/wrappers/test_method.py +++ b/tests/unit/schema/wrappers/test_method.py @@ -475,7 +475,7 @@ def test_method_http_options_generate_sample(): 'id': 'projects/sample1/regions/sample2/id/sample3'}} -def test_method_http_options_generate_sample_immplicit_template(): +def test_method_http_options_generate_sample_implicit_template(): http_rule = http_pb2.HttpRule( get='/v1/{resource.id}/stuff', )