Skip to content

Commit 740c4b0

Browse files
authored
Update ruby_generator.cc to allow proto2 imports in proto3 (#9003)
* Update ruby_generator.cc to allow proto2 imports in proto3, with updated unit tests * Update Makefile.am with new ruby_generated_code_proto2_import.proto * Fix ruby_generator unit test to use temporary test directory for imported protos * Add test for imported proto2 to ruby/tests * Fix proto_path, restore to ../src/protoc, and fix/cleanup unit test. * Rename Proto2TestMessage to TestImportedMessage for consistency, for ruby compiler tests
1 parent 89b14b1 commit 740c4b0

11 files changed

Lines changed: 49 additions & 44 deletions

ruby/Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ unless ENV['IN_DOCKER'] == 'true'
6161
output_file = proto_file.sub(/\.proto$/, "_pb.rb")
6262
genproto_output << output_file
6363
file output_file => proto_file do |file_task|
64-
sh "#{protoc_command} -I../src -I. --ruby_out=. #{proto_file}"
64+
sh "#{protoc_command} -I../src -I. -I./tests --ruby_out=. #{proto_file}"
6565
end
6666
end
6767
end

ruby/tests/basic.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,17 @@ def test_set_clear_defaults
168168
assert_equal nil, m.singular_msg
169169
end
170170

171+
def test_import_proto2
172+
m = TestMessage.new
173+
assert !m.has_optional_proto2_submessage?
174+
m.optional_proto2_submessage = ::FooBar::Proto2::TestImportedMessage.new
175+
assert m.has_optional_proto2_submessage?
176+
assert TestMessage.descriptor.lookup('optional_proto2_submessage').has?(m)
177+
178+
m.clear_optional_proto2_submessage
179+
assert !m.has_optional_proto2_submessage?
180+
end
181+
171182
def test_clear_repeated_fields
172183
m = TestMessage.new
173184

@@ -487,6 +498,7 @@ def test_to_h
487498
:optional_int64=>0,
488499
:optional_msg=>nil,
489500
:optional_msg2=>nil,
501+
:optional_proto2_submessage=>nil,
490502
:optional_string=>"foo",
491503
:optional_uint32=>0,
492504
:optional_uint64=>0,

ruby/tests/basic_test.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import "google/protobuf/wrappers.proto";
66
import "google/protobuf/timestamp.proto";
77
import "google/protobuf/duration.proto";
88
import "google/protobuf/struct.proto";
9+
import "test_import_proto2.proto";
910

1011
message Foo {
1112
Bar bar = 1;
@@ -32,6 +33,7 @@ message TestMessage {
3233
optional bytes optional_bytes = 9;
3334
optional TestMessage2 optional_msg = 10;
3435
optional TestEnum optional_enum = 11;
36+
optional foo_bar.proto2.TestImportedMessage optional_proto2_submessage = 24;
3537

3638
repeated int32 repeated_int32 = 12;
3739
repeated int64 repeated_int64 = 13;

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ EXTRA_DIST = \
546546
google/protobuf/compiler/package_info.h \
547547
google/protobuf/compiler/ruby/ruby_generated_code.proto \
548548
google/protobuf/compiler/ruby/ruby_generated_code_pb.rb \
549+
google/protobuf/compiler/ruby/ruby_generated_code_proto2_import.proto \
549550
google/protobuf/compiler/ruby/ruby_generated_code_proto2.proto \
550551
google/protobuf/compiler/ruby/ruby_generated_code_proto2_pb.rb \
551552
google/protobuf/compiler/ruby/ruby_generated_pkg_explicit.proto \

src/google/protobuf/compiler/ruby/ruby_generated_code.proto

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ syntax = "proto3";
22

33
package A.B.C;
44

5+
import "ruby_generated_code_proto2_import.proto";
6+
57
message TestMessage {
68
int32 optional_int32 = 1;
79
int64 optional_int64 = 2;
@@ -14,6 +16,7 @@ message TestMessage {
1416
bytes optional_bytes = 9;
1517
TestEnum optional_enum = 10;
1618
TestMessage optional_msg = 11;
19+
TestImportedMessage optional_proto2_submessage = 12;
1720

1821
repeated int32 repeated_int32 = 21;
1922
repeated int64 repeated_int64 = 22;

src/google/protobuf/compiler/ruby/ruby_generated_code_pb.rb

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

src/google/protobuf/compiler/ruby/ruby_generated_code_proto2.proto

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ syntax = "proto2";
22

33
package A.B.C;
44

5+
import "ruby_generated_code_proto2_import.proto";
6+
57
message TestMessage {
68
optional int32 optional_int32 = 1 [default = 1];
79
optional int64 optional_int64 = 2 [default = 2];
@@ -14,6 +16,7 @@ message TestMessage {
1416
optional bytes optional_bytes = 9 [default = "\0\1\2\100fubar"];
1517
optional TestEnum optional_enum = 10 [default = A];
1618
optional TestMessage optional_msg = 11;
19+
optional TestImportedMessage optional_proto2_submessage = 12;
1720

1821
repeated int32 repeated_int32 = 21;
1922
repeated int64 repeated_int64 = 22;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
syntax = "proto2";
2+
3+
package A.B.C;
4+
5+
message TestImportedMessage {}

src/google/protobuf/compiler/ruby/ruby_generated_code_proto2_pb.rb

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

src/google/protobuf/compiler/ruby/ruby_generator.cc

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -490,43 +490,6 @@ bool UsesTypeFromFile(const Descriptor* message, const FileDescriptor* file,
490490
return false;
491491
}
492492

493-
// Ruby doesn't currently support proto2. This causes a failure even for proto3
494-
// files that import proto2. But in some cases, the proto2 file is only being
495-
// imported to extend another proto2 message. The prime example is declaring
496-
// custom options by extending FileOptions/FieldOptions/etc.
497-
//
498-
// If the proto3 messages don't have any proto2 submessages, it is safe to omit
499-
// the dependency completely. Users won't be able to use any proto2 extensions,
500-
// but they already couldn't because proto2 messages aren't supported.
501-
//
502-
// If/when we add proto2 support, we should remove this.
503-
bool MaybeEmitDependency(const FileDescriptor* import,
504-
const FileDescriptor* from,
505-
io::Printer* printer,
506-
std::string* error) {
507-
if (from->syntax() == FileDescriptor::SYNTAX_PROTO3 &&
508-
import->syntax() == FileDescriptor::SYNTAX_PROTO2) {
509-
for (int i = 0; i < from->message_type_count(); i++) {
510-
if (UsesTypeFromFile(from->message_type(i), import, error)) {
511-
// Error text was already set by UsesTypeFromFile().
512-
return false;
513-
}
514-
}
515-
516-
// Ok to omit this proto2 dependency -- so we won't print anything.
517-
GOOGLE_LOG(WARNING) << "Omitting proto2 dependency '" << import->name()
518-
<< "' from proto3 output file '"
519-
<< GetOutputFilename(from->name())
520-
<< "' because we don't support proto2 and no proto2 "
521-
"types from that file are being used.";
522-
return true;
523-
} else {
524-
printer->Print(
525-
"require '$name$'\n", "name", GetRequireName(import->name()));
526-
return true;
527-
}
528-
}
529-
530493
bool GenerateDslDescriptor(const FileDescriptor* file, io::Printer* printer,
531494
std::string* error) {
532495
printer->Print(
@@ -572,9 +535,7 @@ bool GenerateFile(const FileDescriptor* file, io::Printer* printer,
572535
"filename", file->name());
573536

574537
for (int i = 0; i < file->dependency_count(); i++) {
575-
if (!MaybeEmitDependency(file->dependency(i), file, printer, error)) {
576-
return false;
577-
}
538+
printer->Print("require '$name$'\n", "name", GetRequireName(file->dependency(i)->name()));
578539
}
579540

580541
// TODO: Remove this when ruby supports extensions for proto2 syntax.

0 commit comments

Comments
 (0)