Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only generate SwiftProtobuf import for well-known types #17

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions Sources/GRPCProtobufCodeGen/ProtobufCodeGenParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,23 @@ extension ProtobufCodeGenParser {
file: FileDescriptor
) -> [Dependency] {
var codeDependencies: [Dependency] = [
Dependency(module: "GRPCProtobuf", accessLevel: .internal),
Dependency(module: "SwiftProtobuf", accessLevel: self.accessLevel),
Dependency(module: "GRPCProtobuf", accessLevel: .internal)
]

// If any services in the file depend on well-known Protobuf types then also import
// SwiftProtobuf. Importing SwiftProtobuf unconditionally results in warnings in the generated
// code if access-levels are used on imports and no well-known types are used.
let usesAnyWellKnownTypesInServices = file.services.contains { service in
service.methods.contains { method in
let inputIsWellKnown = method.inputType.wellKnownType != nil
let outputIsWellKnown = method.outputType.wellKnownType != nil
return inputIsWellKnown || outputIsWellKnown
}
}
if usesAnyWellKnownTypesInServices {
codeDependencies.append(Dependency(module: "SwiftProtobuf", accessLevel: self.accessLevel))
}

// Adding as dependencies the modules containing generated code or types for
// '.proto' files imported in the '.proto' file we are parsing.
codeDependencies.append(
Expand Down
Binary file not shown.
28 changes: 24 additions & 4 deletions Tests/GRPCProtobufCodeGenTests/ProtobufCodeGenParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ struct ProtobufCodeGenParserTests {
@Test("Dependencies")
func dependencies() {
let expected: [GRPCCodeGen.Dependency] = [
.init(module: "GRPCProtobuf", accessLevel: .internal), // Always an internal import
.init(module: "SwiftProtobuf", accessLevel: .internal),
.init(module: "GRPCProtobuf", accessLevel: .internal) // Always an internal import
]
#expect(self.codeGen.dependencies == expected)
}
Expand Down Expand Up @@ -174,8 +173,7 @@ struct ProtobufCodeGenParserTests {
@Test("Dependencies")
func dependencies() {
let expected: [GRPCCodeGen.Dependency] = [
.init(module: "GRPCProtobuf", accessLevel: .internal), // Always an internal import
.init(module: "SwiftProtobuf", accessLevel: .internal),
.init(module: "GRPCProtobuf", accessLevel: .internal) // Always an internal import
]
#expect(self.codeGen.dependencies == expected)
}
Expand Down Expand Up @@ -237,4 +235,26 @@ struct ProtobufCodeGenParserTests {
#expect(self.service.namespace.base == "")
}
}

@Suite("Service using 'well-known types' (wkt-service.proto)")
struct WKTService: UsesDescriptorSet {
static let descriptorSetName = "wkt-service"
static let fileDescriptorName = "wkt-service"

let codeGen: CodeGenerationRequest

init() throws {
let descriptor = try #require(try Self.fileDescriptor)
self.codeGen = try parseDescriptor(descriptor)
}

@Test("Dependencies")
func dependencies() {
let expected: [Dependency] = [
Dependency(module: "GRPCProtobuf", accessLevel: .internal),
Dependency(module: "SwiftProtobuf", accessLevel: .internal),
]
#expect(self.codeGen.dependencies == expected)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ struct ProtobufCodeGeneratorTests: UsesDescriptorSet {
import GRPCCore
import GRPCProtobuf
import SwiftProtobuf
// MARK: - test.TestService
Expand Down
12 changes: 12 additions & 0 deletions dev/protos/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,21 @@ function generate_bar_service_descriptor_set {
--include_imports
}

function generate_wkt_service_descriptor_set {
local proto proto_path output
proto="$here/local/wkt-service.proto"
proto_path="$(dirname "$proto")"
output="$root/Tests/GRPCProtobufCodeGenTests/Generated/wkt-service.pb"

invoke_protoc --descriptor_set_out="$output" "$proto" -I "$proto_path" \
--include_source_info \
--include_imports
}

#------------------------------------------------------------------------------

# Descriptor sets
generate_test_service_descriptor_set
generate_foo_service_descriptor_set
generate_bar_service_descriptor_set
generate_wkt_service_descriptor_set
10 changes: 10 additions & 0 deletions dev/protos/local/wkt-service.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Leading trivia.
syntax = "proto3";

package test;

import "google/protobuf/any.proto";

service WKTService {
rpc Method (google.protobuf.Any) returns (google.protobuf.Any) {}
}
Loading