From 2b220f8be7c80d4d51f9506f359608fce3b78ccb Mon Sep 17 00:00:00 2001
From: Makarand Dharmapurikar <makarandd@google.com>
Date: Fri, 8 Jul 2016 10:47:57 -0700
Subject: [PATCH] fixes #7219

Modified objc plugin to add two types of import statements for well
known protos.
Deleted unnecessary empty.proto as we'll be using wellknown empty.proto
Modified test to use the well known empty.proto.
---
 src/compiler/objective_c_plugin.cc            | 20 ++++++++-
 src/objective-c/tests/InteropTests.m          | 12 ++---
 .../tests/RemoteTestClient/RemoteTest.podspec |  4 +-
 .../tests/RemoteTestClient/empty.proto        | 44 -------------------
 .../tests/RemoteTestClient/test.proto         |  4 +-
 5 files changed, 29 insertions(+), 55 deletions(-)
 delete mode 100644 src/objective-c/tests/RemoteTestClient/empty.proto

diff --git a/src/compiler/objective_c_plugin.cc b/src/compiler/objective_c_plugin.cc
index 3ccfd5b037..43e4b3647d 100644
--- a/src/compiler/objective_c_plugin.cc
+++ b/src/compiler/objective_c_plugin.cc
@@ -39,6 +39,8 @@
 #include "src/compiler/objective_c_generator.h"
 #include "src/compiler/objective_c_generator_helpers.h"
 
+#include <google/protobuf/compiler/objectivec/objectivec_helpers.h>
+
 class ObjectiveCGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
  public:
   ObjectiveCGrpcGenerator() {}
@@ -72,7 +74,21 @@ class ObjectiveCGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
       for (int i = 0; i < file->dependency_count(); i++) {
         ::grpc::string header = grpc_objective_c_generator::MessageHeaderName(
             file->dependency(i));
-        proto_imports += ::grpc::string("#import \"") + header + "\"\n";
+        const grpc::protobuf::FileDescriptor *dependency = file->dependency(i);
+        if (::google::protobuf::compiler::objectivec::IsProtobufLibraryBundledProtoFile(dependency)) {
+          ::grpc::string base_name = header;
+          grpc_generator::StripPrefix(&base_name, "google/protobuf/");
+          proto_imports +=
+            ::grpc::string("#if GPB_USE_PROTOBUF_FRAMEWORK_IMPORTS\n") +
+            ::grpc::string("  #import <") +
+            ::google::protobuf::compiler::objectivec::ProtobufLibraryFrameworkName +
+                          ("/") + base_name + ">\n" +
+            ::grpc::string("#else\n") +
+            ::grpc::string("  #import \"") + header + "\"\n" +
+            ::grpc::string("#endif\n");
+       } else {
+          proto_imports += ::grpc::string("#import \"") + header + "\"\n";
+       }
       }
 
       ::grpc::string declarations;
@@ -85,7 +101,7 @@ class ObjectiveCGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
       static const ::grpc::string kNonNullEnd = "\nNS_ASSUME_NONNULL_END\n";
 
       Write(context, file_name + ".pbrpc.h",
-          imports + '\n' + proto_imports + '\n' + kNonNullBegin + 
+          imports + '\n' + proto_imports + '\n' + kNonNullBegin +
           declarations + kNonNullEnd);
     }
 
diff --git a/src/objective-c/tests/InteropTests.m b/src/objective-c/tests/InteropTests.m
index a503f02059..a2f63ac40c 100644
--- a/src/objective-c/tests/InteropTests.m
+++ b/src/objective-c/tests/InteropTests.m
@@ -110,12 +110,12 @@ static cronet_engine *cronetEngine = NULL;
   XCTAssertNotNil(self.class.host);
   __weak XCTestExpectation *expectation = [self expectationWithDescription:@"EmptyUnary"];
 
-  RMTEmpty *request = [RMTEmpty message];
+  GPBEmpty *request = [GPBEmpty message];
 
-  [_service emptyCallWithRequest:request handler:^(RMTEmpty *response, NSError *error) {
+  [_service emptyCallWithRequest:request handler:^(GPBEmpty *response, NSError *error) {
     XCTAssertNil(error, @"Finished with unexpected error: %@", error);
 
-    id expectedResponse = [RMTEmpty message];
+    id expectedResponse = [GPBEmpty message];
     XCTAssertEqualObjects(response, expectedResponse);
 
     [expectation fulfill];
@@ -343,9 +343,9 @@ static cronet_engine *cronetEngine = NULL;
   __weak XCTestExpectation *expectation =
       [self expectationWithDescription:@"RPC after closing connection"];
 
-  RMTEmpty *request = [RMTEmpty message];
+  GPBEmpty *request = [GPBEmpty message];
 
-  [_service emptyCallWithRequest:request handler:^(RMTEmpty *response, NSError *error) {
+  [_service emptyCallWithRequest:request handler:^(GPBEmpty *response, NSError *error) {
     XCTAssertNil(error, @"First RPC finished with unexpected error: %@", error);
 
 #pragma clang diagnostic push
@@ -353,7 +353,7 @@ static cronet_engine *cronetEngine = NULL;
     [GRPCCall closeOpenConnections];
 #pragma clang diagnostic pop
 
-    [_service emptyCallWithRequest:request handler:^(RMTEmpty *response, NSError *error) {
+    [_service emptyCallWithRequest:request handler:^(GPBEmpty *response, NSError *error) {
       XCTAssertNil(error, @"Second RPC finished with unexpected error: %@", error);
       [expectation fulfill];
     }];
diff --git a/src/objective-c/tests/RemoteTestClient/RemoteTest.podspec b/src/objective-c/tests/RemoteTestClient/RemoteTest.podspec
index 887380eb55..25c9c7f841 100644
--- a/src/objective-c/tests/RemoteTestClient/RemoteTest.podspec
+++ b/src/objective-c/tests/RemoteTestClient/RemoteTest.podspec
@@ -15,7 +15,9 @@ Pod::Spec.new do |s|
     BINDIR=../../../../bins/$CONFIG
     PROTOC=$BINDIR/protobuf/protoc
     PLUGIN=$BINDIR/grpc_objective_c_plugin
-    $PROTOC --plugin=protoc-gen-grpc=$PLUGIN --objc_out=. --grpc_out=. *.proto
+    # we use this path to locate well-known proto files
+    PROTO_SRC=../../../../third_party/protobuf/src
+    $PROTOC --plugin=protoc-gen-grpc=$PLUGIN --objc_out=. --grpc_out=. *.proto -I $PROTO_SRC -I .
   CMD
 
   s.subspec "Messages" do |ms|
diff --git a/src/objective-c/tests/RemoteTestClient/empty.proto b/src/objective-c/tests/RemoteTestClient/empty.proto
deleted file mode 100644
index a678048289..0000000000
--- a/src/objective-c/tests/RemoteTestClient/empty.proto
+++ /dev/null
@@ -1,44 +0,0 @@
-// Copyright 2015, Google Inc.
-// All rights reserved.
-//
-// Redistribution and use in source and binary forms, with or without
-// modification, are permitted provided that the following conditions are
-// met:
-//
-//     * Redistributions of source code must retain the above copyright
-// notice, this list of conditions and the following disclaimer.
-//     * Redistributions in binary form must reproduce the above
-// copyright notice, this list of conditions and the following disclaimer
-// in the documentation and/or other materials provided with the
-// distribution.
-//     * Neither the name of Google Inc. nor the names of its
-// contributors may be used to endorse or promote products derived from
-// this software without specific prior written permission.
-//
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-syntax = "proto3";
-
-package grpc.testing;
-
-option objc_class_prefix = "RMT";
-
-// An empty message that you can re-use to avoid defining duplicated empty
-// messages in your project. A typical example is to use it as argument or the
-// return value of a service API. For instance:
-//
-//   service Foo {
-//     rpc Bar (grpc.testing.Empty) returns (grpc.testing.Empty) { };
-//   };
-//
-message Empty {}
diff --git a/src/objective-c/tests/RemoteTestClient/test.proto b/src/objective-c/tests/RemoteTestClient/test.proto
index 514c3b8095..5c359c5c12 100644
--- a/src/objective-c/tests/RemoteTestClient/test.proto
+++ b/src/objective-c/tests/RemoteTestClient/test.proto
@@ -31,7 +31,7 @@
 // of unary/streaming requests/responses.
 syntax = "proto3";
 
-import "empty.proto";
+import "google/protobuf/empty.proto";
 import "messages.proto";
 
 package grpc.testing;
@@ -42,7 +42,7 @@ option objc_class_prefix = "RMT";
 // performance with various types of payload.
 service TestService {
   // One empty request followed by one empty response.
-  rpc EmptyCall(grpc.testing.Empty) returns (grpc.testing.Empty);
+  rpc EmptyCall(google.protobuf.Empty) returns (google.protobuf.Empty);
 
   // One request followed by one response.
   rpc UnaryCall(SimpleRequest) returns (SimpleResponse);
-- 
GitLab