Skip to content

Commit 842e324

Browse files
authored
fix: add default library settings for incorrect lib version (#2212)
1 parent 4da2bf1 commit 842e324

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

packages/gapic-generator/gapic/schema/api.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -706,13 +706,14 @@ def all_library_settings(
706706
for library_setting in self.service_yaml_config.publishing.library_settings
707707
}
708708

709-
# Add default settings for the current proto package
710-
if not result:
711-
result = {
712-
self.naming.proto_package: client_pb2.ClientLibrarySettings(
713-
version=self.naming.proto_package
709+
# NOTE: Add default settings for the current proto package
710+
# for the following cases:
711+
# - if library settings are not specified in the service config.
712+
# - if library_settings.version != self.naming.proto_package (proto package name)
713+
if self.naming.proto_package not in result:
714+
result[self.naming.proto_package] = client_pb2.ClientLibrarySettings(
715+
version=self.naming.proto_package
714716
)
715-
}
716717

717718
return result
718719

packages/gapic-generator/tests/unit/schema/test_api.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2741,6 +2741,35 @@ def test_read_empty_python_settings_from_service_yaml():
27412741
== client_pb2.PythonSettings.ExperimentalFeatures()
27422742
assert api_schema.all_library_settings["google.example.v1beta1"].python_settings.experimental_features.rest_async_io_enabled \
27432743
== False
2744+
assert api_schema.all_library_settings[api_schema.naming.proto_package].python_settings \
2745+
== client_pb2.PythonSettings()
2746+
2747+
2748+
def test_incorrect_library_settings_version():
2749+
# NOTE: This test case ensures that the generator is able to read
2750+
# from the default library settings if the version specified against the
2751+
# library settings in the service yaml of an API differs from the version
2752+
# of the API.
2753+
service_yaml_config = {
2754+
"apis": [
2755+
{"name": "google.example.v1beta1.ServiceOne.Example1"},
2756+
],
2757+
"publishing": {
2758+
"library_settings": [
2759+
{
2760+
"version": "google.example.v1",
2761+
"python_settings": {
2762+
"experimental_features": {"rest_async_io_enabled": True},
2763+
},
2764+
}
2765+
]
2766+
},
2767+
}
2768+
cli_options = Options(service_yaml_config=service_yaml_config)
2769+
fd = get_file_descriptor_proto_for_tests(fields=[])
2770+
api_schema = api.API.build(fd, "google.example.v1beta1", opts=cli_options)
2771+
assert api_schema.all_library_settings[api_schema.naming.proto_package].python_settings \
2772+
== client_pb2.PythonSettings()
27442773

27452774

27462775
def test_python_settings_duplicate_version_raises_error():

0 commit comments

Comments
 (0)