Skip to content

Commit 069ce31

Browse files
committed
Only import SwiftProtobuf when WKTs are used as input/output
Modification: The code generator imports SwiftProtobuf if the file being generated has a dependency on any of the protos bundled by SwiftProtobuf. This can yield unnecessary imports which may warn if used in conjunction with access levels on imports. The problem is that the grpc generator checks for dependencies on the FileDescriptor which may include messages defined in the same file as a service. If any of those messages depend on a well known type (WKT) then an import will be added even though the code being generated doesn't include message code. Modifications: - Only include a SwiftProtobuf import if an input or output type for an RPC is a bundled proto. - Fix a test where a WKT is used in a message but not as an input/output type for an RPC. Result: Fewer warnings
1 parent 1a75da3 commit 069ce31

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

Sources/GRPCProtobufCodeGen/ProtobufCodeGenParser.swift

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,26 @@ extension ProtobufCodeGenParser {
110110
var codeDependencies: [Dependency] = [
111111
Dependency(module: self.moduleNames.grpcProtobuf, accessLevel: .internal)
112112
]
113+
113114
// If there's a dependency on a bundled proto then add the SwiftProtobuf import.
114115
//
115116
// Importing SwiftProtobuf unconditionally results in warnings in the generated
116117
// code if access-levels are used on imports and no bundled protos are used.
117-
let dependsOnBundledProto = file.dependencies.contains { descriptor in
118-
SwiftProtobufInfo.isBundledProto(file: descriptor)
118+
//
119+
// The import is only needed if a bundled proto is used as the input or output type
120+
// for an RPC. The file may have a dependency on a bundled proto without requiring
121+
// the SwiftProtobuf import (e.g. a message depending on a bundle proto may be
122+
// defined in the same file as the service, the dependency will be in the '.pb.swift'
123+
// along with the message code).
124+
let needsProtobufImport = file.services.contains { service in
125+
service.methods.contains { method in
126+
let inputIsBundled = SwiftProtobufInfo.isBundledProto(file: method.inputType.file)
127+
let outputIsBundled = SwiftProtobufInfo.isBundledProto(file: method.outputType.file)
128+
return inputIsBundled || outputIsBundled
129+
}
119130
}
120131

121-
if dependsOnBundledProto {
132+
if needsProtobufImport {
122133
let dependency = Dependency(
123134
module: self.moduleNames.swiftProtobuf,
124135
accessLevel: self.accessLevel
-5 Bytes
Binary file not shown.

Tests/GRPCProtobufCodeGenTests/ProtobufCodeGenParserTests.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ struct ProtobufCodeGenParserTests {
6666
@available(gRPCSwiftProtobuf 2.0, *)
6767
func dependencies() throws {
6868
let expected: [GRPCCodeGen.Dependency] = [
69-
.init(module: "GRPCProtobuf", accessLevel: .internal), // Always an internal import
70-
.init(module: "SwiftProtobuf", accessLevel: .internal),
69+
.init(module: "GRPCProtobuf", accessLevel: .internal) // Always an internal import
7170
]
7271
#expect(try self.codeGen.dependencies == expected)
7372
}

dev/protos/local/test-service.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ syntax = "proto3";
33

44
package test;
55

6-
// Using a WKT forces an "SwiftProtobuf" to be imported in generated code.
6+
// WKT isn't used in API generated by gRPC, so no import is expected.
77
import "google/protobuf/any.proto";
88

99
// Service docs.

0 commit comments

Comments
 (0)