Эх сурвалжийг харах

Fix generated code when there is no namespace but there is enum definition.

Bo Yang 8 жил өмнө
parent
commit
e259b515a5

+ 8 - 3
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();
     }
 }

+ 6 - 1
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;
+}

+ 5 - 2
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",