aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBo Yang <teboring@google.com>2017-01-10 01:02:35 +0000
committerBo Yang <teboring@google.com>2017-01-10 01:02:35 +0000
commite259b515a5baae2d7d7623af62ea87079a5e91a0 (patch)
tree218bd84998ce230182a06f191b4dce4eb47983a9
parent83fb8c7d29a221851817fd889a716654f6bfb96c (diff)
downloadprotobuf-e259b515a5baae2d7d7623af62ea87079a5e91a0.tar.gz
protobuf-e259b515a5baae2d7d7623af62ea87079a5e91a0.tar.bz2
protobuf-e259b515a5baae2d7d7623af62ea87079a5e91a0.zip
Fix generated code when there is no namespace but there is enum definition.
-rw-r--r--php/tests/generated_class_test.php11
-rw-r--r--php/tests/proto/test_no_namespace.proto7
-rw-r--r--src/google/protobuf/compiler/php/php_generator.cc7
3 files changed, 19 insertions, 6 deletions
diff --git a/php/tests/generated_class_test.php b/php/tests/generated_class_test.php
index 27912ecd..6381419d 100644
--- a/php/tests/generated_class_test.php
+++ b/php/tests/generated_class_test.php
@@ -1,6 +1,7 @@
<?php
-require_once('generated/NoNameSpace.php');
+require_once('generated/NoNameSpaceEnum.php');
+require_once('generated/NoNameSpaceMessage.php');
require_once('test_util.php');
use Google\Protobuf\Internal\RepeatedField;
@@ -601,10 +602,14 @@ class GeneratedClassTest extends PHPUnit_Framework_TestCase
}
#########################################################
- # Test oneof field.
+ # Test message/enum without namespace.
#########################################################
public function testMessageWithoutNamespace() {
- $m = new NoNameSpace();
+ $m = new NoNameSpaceMessage();
+ }
+
+ public function testEnumWithoutNamespace() {
+ $m = new NoNameSpaceEnum();
}
}
diff --git a/php/tests/proto/test_no_namespace.proto b/php/tests/proto/test_no_namespace.proto
index 4331aeab..b8c4fdf2 100644
--- a/php/tests/proto/test_no_namespace.proto
+++ b/php/tests/proto/test_no_namespace.proto
@@ -1,5 +1,10 @@
syntax = "proto3";
-message NoNameSpace {
+message NoNameSpaceMessage {
int32 a = 1;
}
+
+enum NoNameSpaceEnum {
+ VALUE_A = 0;
+ VALUE_B = 1;
+}
diff --git a/src/google/protobuf/compiler/php/php_generator.cc b/src/google/protobuf/compiler/php/php_generator.cc
index be2739ff..b0605781 100644
--- a/src/google/protobuf/compiler/php/php_generator.cc
+++ b/src/google/protobuf/compiler/php/php_generator.cc
@@ -757,12 +757,15 @@ void GenerateEnumFile(const FileDescriptor* file, const EnumDescriptor* en,
std::string fullname = FilenameToClassname(filename);
int lastindex = fullname.find_last_of("\\");
- GenerateEnumDocComment(&printer, en);
- if (lastindex != string::npos) {
+ if (!file->package().empty()) {
printer.Print(
"namespace ^name^;\n\n",
"name", fullname.substr(0, lastindex));
+ }
+ GenerateEnumDocComment(&printer, en);
+
+ if (lastindex != string::npos) {
printer.Print(
"class ^name^\n"
"{\n",