Skip to content

Commit f656d40

Browse files
authored
fix: snippetgen handling of repeated enum field (#1443)
* fix: snippetgen handling of repeated enum field
1 parent fdf2313 commit f656d40

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

packages/gapic-generator/gapic/samplegen/samplegen.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,8 @@ def _normal_request_setup(self, base_param_to_attrs, val, request, field):
364364
elif attr.enum:
365365
# A little bit hacky, but 'values' is a list, and this is the easiest
366366
# way to verify that the value is a valid enum variant.
367-
witness = any(e.name == val for e in attr.enum.values)
367+
# Here val could be a list of a single enum value name.
368+
witness = any(e.name in val for e in attr.enum.values)
368369
if not witness:
369370
raise types.InvalidEnumVariant(
370371
"Invalid variant for enum {}: '{}'".format(attr, val)
@@ -974,8 +975,14 @@ def generate_request_object(api_schema: api.API, service: wrappers.Service, mess
974975
{"field": field_name, "value": field.mock_value_original_type})
975976
elif field.enum:
976977
# Choose the last enum value in the list since index 0 is often "unspecified"
978+
enum_value = field.enum.values[-1].name
979+
if field.repeated:
980+
field_value = [enum_value]
981+
else:
982+
field_value = enum_value
983+
977984
request.append(
978-
{"field": field_name, "value": field.enum.values[-1].name})
985+
{"field": field_name, "value": field_value})
979986
else:
980987
# This is a message type, recurse
981988
# TODO(busunkim): Some real world APIs have
@@ -1023,7 +1030,7 @@ def generate_sample_specs(api_schema: api.API, *, opts) -> Generator[Dict[str, A
10231030
spec = {
10241031
"rpc": rpc_name,
10251032
"transport": transport,
1026-
# `request` and `response` is populated in `preprocess_sample`
1033+
# `request` and `response` are populated in `preprocess_sample`
10271034
"service": f"{api_schema.naming.proto_package}.{service_name}",
10281035
"region_tag": region_tag,
10291036
"description": f"Snippet for {utils.to_snake_case(rpc_name)}"

packages/gapic-generator/tests/unit/samplegen/test_samplegen.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,9 @@ def test_preprocess_sample():
203203
]
204204

205205

206-
def test_preprocess_sample_with_enum_field():
206+
@pytest.mark.parametrize(
207+
'repeated_enum,expected', [(False, "TYPE_2"), (True, ["TYPE_2"])])
208+
def test_preprocess_sample_with_enum_field(repeated_enum, expected):
207209
# Verify that the default response is added.
208210
sample = {"service": "Mollusc", "rpc": "Classify"}
209211

@@ -212,6 +214,7 @@ def test_preprocess_sample_with_enum_field():
212214
"type": DummyField(
213215
name="type",
214216
required=True,
217+
repeated=repeated_enum,
215218
type=enum_factory("type", ["TYPE_1", "TYPE_2"]),
216219
enum=enum_factory("type", ["TYPE_1", "TYPE_2"])
217220
)
@@ -255,7 +258,7 @@ def test_preprocess_sample_with_enum_field():
255258
assert sample["request"] == [
256259
{
257260
"field": "type",
258-
"value": "TYPE_2"
261+
"value": expected
259262
}
260263
]
261264

0 commit comments

Comments
 (0)