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(snippetgen): don't create duplicate requests for required oneofs #1088

Merged
merged 3 commits into from Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions gapic/samplegen/samplegen.py
Expand Up @@ -30,7 +30,7 @@
from gapic.schema import wrappers

from collections import defaultdict, namedtuple, ChainMap as chainmap
from typing import Any, ChainMap, Dict, FrozenSet, Generator, List, Mapping, Optional, Tuple, Sequence
from typing import Any, ChainMap, Dict, FrozenSet, Generator, List, Mapping, Optional, Sequence

# There is no library stub file for this module, so ignore it.
from google.api import resource_pb2 # type: ignore
Expand Down Expand Up @@ -976,10 +976,16 @@ def generate_request_object(api_schema: api.API, service: wrappers.Service, mess

request_fields: List[wrappers.Field] = []

# Choose the first option for each oneof
# There is no standard syntax to mark a oneof as "required" in protos.
# Assume every oneof is required and pick the first option
# in each oneof.
selected_oneofs: List[wrappers.Field] = [oneof_fields[0]
for oneof_fields in message.oneof_fields().values()]
request_fields = selected_oneofs + message.required_fields

# Don't add required fields if they're also marked as oneof
required_fields = [
field for field in message.required_fields if not field.oneof]
request_fields = selected_oneofs + required_fields

for field in request_fields:
# TransformedRequest expects nested fields to be referenced like
Expand Down
@@ -0,0 +1,48 @@
# -*- coding: utf-8 -*-
# Copyright 2020 Google LLC
#
# 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.
#
# Generated code. DO NOT EDIT!
#
# Snippet for OneOfMethodRequiredField
# NOTE: This snippet has been automatically generated for illustrative purposes only.
# It may require modifications to work in your environment.

# To install the latest published package dependency, execute the following:
# python3 -m pip install animalia-mollusca


# [START mollusca_generated_mollusca_v1_Snippets_OneOfMethodRequiredField_async]
from animalia import mollusca_v1


async def sample_one_of_method_required_field():
"""Snippet for one_of_method_required_field"""

# Create a client
client = mollusca_v1.SnippetsAsyncClient()

# Initialize request argument(s)
request = mollusca_v1.OneOfRequestWithRequiredField(
my_string="my_string_value",
non_one_of_string="non_one_of_string_value",
)

# Make the request
response = await client.one_of_method_required_field(request=request)

# Handle response
print(response)

# [END mollusca_generated_mollusca_v1_Snippets_OneOfMethodRequiredField_async]
@@ -0,0 +1,48 @@
# -*- coding: utf-8 -*-
# Copyright 2020 Google LLC
#
# 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.
#
# Generated code. DO NOT EDIT!
#
# Snippet for OneOfMethodRequiredField
# NOTE: This snippet has been automatically generated for illustrative purposes only.
# It may require modifications to work in your environment.

# To install the latest published package dependency, execute the following:
# python3 -m pip install animalia-mollusca


# [START mollusca_generated_mollusca_v1_Snippets_OneOfMethodRequiredField_sync]
from animalia import mollusca_v1


def sample_one_of_method_required_field():
"""Snippet for one_of_method_required_field"""

# Create a client
client = mollusca_v1.SnippetsClient()

# Initialize request argument(s)
request = mollusca_v1.OneOfRequestWithRequiredField(
my_string="my_string_value",
non_one_of_string="non_one_of_string_value",
)

# Make the request
response = client.one_of_method_required_field(request=request)

# Handle response
print(response)

# [END mollusca_generated_mollusca_v1_Snippets_OneOfMethodRequiredField_sync]
13 changes: 13 additions & 0 deletions tests/snippetgen/snippets.proto
Expand Up @@ -55,6 +55,8 @@ service Snippets {
rpc MethodBidiStreaming(stream SignatureRequestOneRequiredField) returns (stream Response);

rpc OneOfMethod(OneOfRequest) returns (Response);

rpc OneOfMethodRequiredField(OneOfRequestWithRequiredField) returns (Response);
}

enum Enum {
Expand Down Expand Up @@ -136,3 +138,14 @@ message OneOfRequest {
int32 my_number = 3;
}
}


message OneOfRequestWithRequiredField {
string non_one_of_string = 1 [(google.api.field_behavior) = REQUIRED];

// Some APIs mark every field in a "required" oneof as required
oneof my_one_of {
string my_string = 2 [(google.api.field_behavior) = REQUIRED];
int32 my_number = 3 [(google.api.field_behavior) = REQUIRED];
}
}