Skip to content

Commit 7fa9eec

Browse files
fix: no longer generate cross-package references for mixin objects (#1211)
* fix: no longer generate cross-package references for mixin elements
1 parent 763ea0e commit 7fa9eec

File tree

14 files changed

+173
-4
lines changed

14 files changed

+173
-4
lines changed

gapic-generator/lib/gapic/formatting_utils.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,12 @@ def escape_line_braces line
116116
def format_line_xrefs api, line, disable_xrefs, transport
117117
while (m = @xref_detector.match line)
118118
entity = api.lookup m[:addr]
119-
return line if entity.nil?
119+
is_mixin_field_addr = Gapic::Model::Mixins.mixin_message_field_address?(
120+
m[:addr],
121+
gem_name: api.configuration.fetch(:gem, nil)&.fetch(:name, "")
122+
)
123+
return line if entity.nil? || is_mixin_field_addr
124+
120125
text = m[:text]
121126
yard_link = disable_xrefs ? text : yard_link_for_entity(entity, text, transport)
122127
return line if yard_link.nil?

gapic-generator/lib/gapic/model/mixins.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,25 @@ def self.mixin_service_address? service_address, gem_name: nil
162162
MIXIN_GEM_NAMES.include?(service_address) && gem_name != MIXIN_GEM_NAMES[service_address]
163163
end
164164

165+
##
166+
# Returns true if the given service address is a mixin.
167+
# This just checks the service against a (hard-coded) set of known mixins.
168+
# If `gem_name` is provided, objects from the package of the service
169+
# that corresponds to that gem_name are not considered mixins.
170+
#
171+
# @param message_field_address [String,Array<String>] The address (either array
172+
# or dot-delimited) of the message or field to check.
173+
# @param gem_name [String] The name of the gem.
174+
# @return [boolean]
175+
#
176+
def self.mixin_message_field_address? message_field_address, gem_name: nil
177+
message_field_address = message_field_address.join "." unless message_field_address.is_a? String
178+
# NB: messages are checked against package, not service
179+
service_address = MIXIN_GEM_NAMES.keys.find { |sn| message_field_address.start_with? MIXIN_PACKAGE_NAMES[sn] }
180+
181+
!service_address.nil? && gem_name != MIXIN_GEM_NAMES[service_address]
182+
end
183+
165184
private
166185

167186
# @return [Enumerable<String>] Names of all services that are specified
@@ -179,6 +198,13 @@ def services_in_config
179198
}.freeze
180199
private_constant :MIXIN_GEM_NAMES
181200

201+
MIXIN_PACKAGE_NAMES = {
202+
LOCATIONS_SERVICE => "google.cloud.location",
203+
IAM_SERVICE => "google.iam.v1",
204+
LRO_SERVICE => "google.longrunning.operations"
205+
}.freeze
206+
private_constant :MIXIN_PACKAGE_NAMES
207+
182208
# Since mixins are scope-limited to a couple of services, it is easier to
183209
# have these in lookup tables than to construct a ServicePresenter
184210

gapic-generator/test/gapic/mixins/mixins_test.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,25 @@ def test_mixin_service_address_checker
4545
refute Gapic::Model::Mixins.mixin_service_address? "testing.mixins.ServiceWithLoc"
4646
end
4747

48+
def test_mixin_message_field_address_checker
49+
# sic. no `s` at the end, this is a message named `Location`, not service named `Locations`
50+
assert Gapic::Model::Mixins.mixin_message_field_address? "google.cloud.location.Location"
51+
assert Gapic::Model::Mixins.mixin_message_field_address? "google.cloud.location.Location.metadata"
52+
53+
assert Gapic::Model::Mixins.mixin_message_field_address? "google.cloud.location.Location",
54+
gem_name: "google-cloud-something-else"
55+
56+
refute Gapic::Model::Mixins.mixin_message_field_address? "google.cloud.location.Location",
57+
gem_name: "google-cloud-location"
58+
refute Gapic::Model::Mixins.mixin_message_field_address? "google.cloud.location.Location.metadata",
59+
gem_name: "google-cloud-location"
60+
61+
assert Gapic::Model::Mixins.mixin_message_field_address? ["google", "iam", "v1", "Policy"]
62+
assert Gapic::Model::Mixins.mixin_message_field_address? ["google", "iam", "v1", "Policy", "bindings"]
63+
64+
refute Gapic::Model::Mixins.mixin_message_field_address? "testing.mixins.Request"
65+
end
66+
4867
# Test the `Garbage` library, which does NOT have mixins specified
4968
# in its service.yaml (or service.yaml at all)
5069
def test_garbage_mixins

gapic-generator/test/test_helper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,10 @@ def fix_namespace name
344344
def fix_service_name name
345345
@service_mapping[name] || name
346346
end
347+
348+
def configuration
349+
{}
350+
end
347351
end
348352

349353
# A fake request builder

shared/gem_defaults.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ def gem_defaults
156156
"testing/mixins/mixins.proto",
157157
"testing/routing_headers/routing_headers.proto",
158158
"testing/nonstandard_lro_grpc/nonstandard_lro_grpc.proto",
159+
# `locations.proto` is included here because it is often
160+
# included in the real world libraries
161+
# as part of `files_to_generate` due to protoc limitations
162+
"google/cloud/location/locations.proto",
159163
],
160164
grpc_service_config: [
161165
"../shared/protos/testing/grpc_service_config/grpc_service_config.json",

shared/input/testing_desc.bin

9.21 KB
Binary file not shown.

shared/output/cloud/secretmanager_v1beta1/proto_docs/google/iam/v1/policy.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ class AuditConfig
313313
# Specifies the identities that do not cause logging for this type of
314314
# permission.
315315
# Follows the same format of
316-
# {::Google::Iam::V1::Binding#members Binding.members}.
316+
# [Binding.members][google.iam.v1.Binding.members].
317317
class AuditLogConfig
318318
include ::Google::Protobuf::MessageExts
319319
extend ::Google::Protobuf::MessageExts::ClassMethods

shared/output/gapic/templates/garbage/proto_docs/google/iam/v1/policy.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ class AuditConfig
321321
# Specifies the identities that do not cause logging for this type of
322322
# permission.
323323
# Follows the same format of
324-
# {::Google::Iam::V1::Binding#members Binding.members}.
324+
# [Binding.members][google.iam.v1.Binding.members].
325325
class AuditLogConfig
326326
include ::Google::Protobuf::MessageExts
327327
extend ::Google::Protobuf::MessageExts::ClassMethods

shared/output/gapic/templates/testing/lib/google/cloud/location/locations_pb.rb

Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

shared/output/gapic/templates/testing/lib/google/cloud/location/locations_services_pb.rb

Lines changed: 47 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)