Skip to content

Commit 21e9326

Browse files
author
Jan Steinke
authored
Update Protobuf (#410)
* Update Protobuf * migrate to v4 * fix protobuf generation * re-add protoc management * extract protoVersion
1 parent d01321a commit 21e9326

File tree

5 files changed

+19
-16
lines changed

5 files changed

+19
-16
lines changed

Diff for: router-protobuf/build.gradle.kts

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
import com.google.protobuf.gradle.protobuf
2-
import com.google.protobuf.gradle.protoc
3-
41
plugins {
5-
id("com.google.protobuf") version "0.8.19"
2+
id("com.google.protobuf") version "0.9.4"
63
}
74

85
repositories {
96
mavenCentral()
107
}
118

9+
val protoVersion = "4.27.2"
10+
1211
dependencies {
1312
implementation(kotlin("stdlib"))
1413
implementation(kotlin("reflect"))
1514

1615
implementation("org.slf4j:slf4j-api:2.0.13")
17-
api("com.google.protobuf:protobuf-java:3.25.3")
18-
api("com.google.protobuf:protobuf-java-util:3.25.3")
16+
api("com.google.protobuf:protobuf-java:$protoVersion")
17+
api("com.google.protobuf:protobuf-java-util:$protoVersion")
1918
implementation("com.google.guava:guava:33.2.1-jre")
2019
api(project(":router"))
2120

@@ -27,10 +26,10 @@ dependencies {
2726
testImplementation("com.jayway.jsonpath:json-path:2.9.0")
2827
}
2928

30-
3129
protobuf {
30+
// Configure the protoc executable
3231
protoc {
33-
// The artifact spec for the Protobuf Compiler
34-
artifact = "com.google.protobuf:protoc:3.25.3"
32+
// Download from repositories
33+
artifact = "com.google.protobuf:protoc:$protoVersion"
3534
}
3635
}

Diff for: router-protobuf/src/main/kotlin/io/moia/router/proto/ProtoBufUtils.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import com.fasterxml.jackson.databind.JsonNode
44
import com.fasterxml.jackson.databind.node.ArrayNode
55
import com.fasterxml.jackson.databind.node.ObjectNode
66
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
7-
import com.google.protobuf.GeneratedMessageV3
7+
import com.google.protobuf.GeneratedMessage
88
import com.google.protobuf.util.JsonFormat
99

1010
object ProtoBufUtils {
11-
fun toJsonWithoutWrappers(proto: GeneratedMessageV3): String {
12-
val message = JsonFormat.printer().omittingInsignificantWhitespace().includingDefaultValueFields().print(proto)
11+
fun toJsonWithoutWrappers(proto: GeneratedMessage): String {
12+
val message = JsonFormat.printer().omittingInsignificantWhitespace().alwaysPrintFieldsWithNoPresence().print(proto)
1313
return removeWrapperObjects(message)
1414
}
1515

Diff for: router-protobuf/src/main/kotlin/io/moia/router/proto/ProtoDeserializationHandler.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ProtoDeserializationHandler : DeserializationHandler {
1919
if (input.contentType() == null) {
2020
false
2121
} else {
22-
MediaType.parse(input.contentType()).let { proto.isCompatibleWith(it) || protoStructuredSuffixWildcard.isCompatibleWith(it) }
22+
MediaType.parse(input.contentType()!!).let { proto.isCompatibleWith(it) || protoStructuredSuffixWildcard.isCompatibleWith(it) }
2323
}
2424

2525
override fun deserialize(

Diff for: router-protobuf/src/main/kotlin/io/moia/router/proto/ProtoSerializationHandler.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.moia.router.proto
22

33
import com.google.common.net.MediaType
4-
import com.google.protobuf.GeneratedMessageV3
4+
import com.google.protobuf.GeneratedMessage
55
import io.moia.router.SerializationHandler
66
import isCompatibleWith
77
import java.util.Base64
@@ -13,13 +13,13 @@ class ProtoSerializationHandler : SerializationHandler {
1313
override fun supports(
1414
acceptHeader: MediaType,
1515
body: Any,
16-
): Boolean = body is GeneratedMessageV3
16+
): Boolean = body is GeneratedMessage
1717

1818
override fun serialize(
1919
acceptHeader: MediaType,
2020
body: Any,
2121
): String {
22-
val message = body as GeneratedMessageV3
22+
val message = body as GeneratedMessage
2323
return if (json.isCompatibleWith(acceptHeader) || jsonStructuredSuffixWildcard.isCompatibleWith(acceptHeader)) {
2424
ProtoBufUtils.toJsonWithoutWrappers(message)
2525
} else {

Diff for: router-protobuf/src/test/proto/Sample.proto

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ syntax = "proto3";
22

33
package io.moia.router.proto.sample;
44

5+
option java_multiple_files = false;
6+
option java_outer_classname = "SampleOuterClass";
7+
option java_package = "io.moia.router.proto.sample";
8+
59
import "google/protobuf/wrappers.proto";
610

711
message Sample {

0 commit comments

Comments
 (0)