From 31bceee00bcaf0d1ac5352facc29ba3481f17bf8 Mon Sep 17 00:00:00 2001 From: Jan Steinke Date: Thu, 4 Jul 2024 15:15:53 +0200 Subject: [PATCH 1/5] Update Protobuf --- router-protobuf/build.gradle.kts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/router-protobuf/build.gradle.kts b/router-protobuf/build.gradle.kts index 6d422cc..8b187b8 100644 --- a/router-protobuf/build.gradle.kts +++ b/router-protobuf/build.gradle.kts @@ -14,8 +14,8 @@ dependencies { implementation(kotlin("reflect")) implementation("org.slf4j:slf4j-api:2.0.13") - api("com.google.protobuf:protobuf-java:3.25.3") - api("com.google.protobuf:protobuf-java-util:3.25.3") + api("com.google.protobuf:protobuf-java:4.27.0") + api("com.google.protobuf:protobuf-java-util:4.27.0") implementation("com.google.guava:guava:33.2.1-jre") api(project(":router")) @@ -31,6 +31,6 @@ dependencies { protobuf { protoc { // The artifact spec for the Protobuf Compiler - artifact = "com.google.protobuf:protoc:3.25.3" + artifact = "com.google.protobuf:protoc:4.27.0" } -} \ No newline at end of file +} From 9b6a92725d4126cb41353837263bd6eb173bfb96 Mon Sep 17 00:00:00 2001 From: Jan Steinke Date: Thu, 4 Jul 2024 15:38:58 +0200 Subject: [PATCH 2/5] migrate to v4 --- router-protobuf/build.gradle.kts | 6 +++--- .../src/main/kotlin/io/moia/router/proto/ProtoBufUtils.kt | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/router-protobuf/build.gradle.kts b/router-protobuf/build.gradle.kts index 8b187b8..b43c9bb 100644 --- a/router-protobuf/build.gradle.kts +++ b/router-protobuf/build.gradle.kts @@ -14,8 +14,8 @@ dependencies { implementation(kotlin("reflect")) implementation("org.slf4j:slf4j-api:2.0.13") - api("com.google.protobuf:protobuf-java:4.27.0") - api("com.google.protobuf:protobuf-java-util:4.27.0") + api("com.google.protobuf:protobuf-java:4.27.2") + api("com.google.protobuf:protobuf-java-util:4.27.2") implementation("com.google.guava:guava:33.2.1-jre") api(project(":router")) @@ -31,6 +31,6 @@ dependencies { protobuf { protoc { // The artifact spec for the Protobuf Compiler - artifact = "com.google.protobuf:protoc:4.27.0" + artifact = "com.google.protobuf:protoc:4.27.2" } } diff --git a/router-protobuf/src/main/kotlin/io/moia/router/proto/ProtoBufUtils.kt b/router-protobuf/src/main/kotlin/io/moia/router/proto/ProtoBufUtils.kt index e07ecf6..beafb8e 100644 --- a/router-protobuf/src/main/kotlin/io/moia/router/proto/ProtoBufUtils.kt +++ b/router-protobuf/src/main/kotlin/io/moia/router/proto/ProtoBufUtils.kt @@ -4,12 +4,12 @@ import com.fasterxml.jackson.databind.JsonNode import com.fasterxml.jackson.databind.node.ArrayNode import com.fasterxml.jackson.databind.node.ObjectNode import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper -import com.google.protobuf.GeneratedMessageV3 +import com.google.protobuf.GeneratedMessage import com.google.protobuf.util.JsonFormat object ProtoBufUtils { - fun toJsonWithoutWrappers(proto: GeneratedMessageV3): String { - val message = JsonFormat.printer().omittingInsignificantWhitespace().includingDefaultValueFields().print(proto) + fun toJsonWithoutWrappers(proto: GeneratedMessage): String { + val message = JsonFormat.printer().omittingInsignificantWhitespace().alwaysPrintFieldsWithNoPresence().print(proto) return removeWrapperObjects(message) } From eeb839c2099d0a1b7bffb23beadfb0e62237fe80 Mon Sep 17 00:00:00 2001 From: Jan Steinke Date: Thu, 4 Jul 2024 16:18:23 +0200 Subject: [PATCH 3/5] fix protobuf generation --- router-protobuf/build.gradle.kts | 15 ++------------- .../router/proto/ProtoDeserializationHandler.kt | 2 +- .../router/proto/ProtoSerializationHandler.kt | 6 +++--- router-protobuf/src/test/proto/Sample.proto | 4 ++++ 4 files changed, 10 insertions(+), 17 deletions(-) diff --git a/router-protobuf/build.gradle.kts b/router-protobuf/build.gradle.kts index b43c9bb..dfdd730 100644 --- a/router-protobuf/build.gradle.kts +++ b/router-protobuf/build.gradle.kts @@ -1,8 +1,5 @@ -import com.google.protobuf.gradle.protobuf -import com.google.protobuf.gradle.protoc - plugins { - id("com.google.protobuf") version "0.8.19" + id("com.google.protobuf") version "0.9.4" } repositories { @@ -25,12 +22,4 @@ dependencies { testImplementation("io.mockk:mockk:1.13.11") testImplementation("org.slf4j:slf4j-simple:2.0.13") testImplementation("com.jayway.jsonpath:json-path:2.9.0") -} - - -protobuf { - protoc { - // The artifact spec for the Protobuf Compiler - artifact = "com.google.protobuf:protoc:4.27.2" - } -} +} \ No newline at end of file diff --git a/router-protobuf/src/main/kotlin/io/moia/router/proto/ProtoDeserializationHandler.kt b/router-protobuf/src/main/kotlin/io/moia/router/proto/ProtoDeserializationHandler.kt index 36a163a..40ff196 100644 --- a/router-protobuf/src/main/kotlin/io/moia/router/proto/ProtoDeserializationHandler.kt +++ b/router-protobuf/src/main/kotlin/io/moia/router/proto/ProtoDeserializationHandler.kt @@ -19,7 +19,7 @@ class ProtoDeserializationHandler : DeserializationHandler { if (input.contentType() == null) { false } else { - MediaType.parse(input.contentType()).let { proto.isCompatibleWith(it) || protoStructuredSuffixWildcard.isCompatibleWith(it) } + MediaType.parse(input.contentType()!!).let { proto.isCompatibleWith(it) || protoStructuredSuffixWildcard.isCompatibleWith(it) } } override fun deserialize( diff --git a/router-protobuf/src/main/kotlin/io/moia/router/proto/ProtoSerializationHandler.kt b/router-protobuf/src/main/kotlin/io/moia/router/proto/ProtoSerializationHandler.kt index e6fd932..4a041bd 100644 --- a/router-protobuf/src/main/kotlin/io/moia/router/proto/ProtoSerializationHandler.kt +++ b/router-protobuf/src/main/kotlin/io/moia/router/proto/ProtoSerializationHandler.kt @@ -1,7 +1,7 @@ package io.moia.router.proto import com.google.common.net.MediaType -import com.google.protobuf.GeneratedMessageV3 +import com.google.protobuf.GeneratedMessage import io.moia.router.SerializationHandler import isCompatibleWith import java.util.Base64 @@ -13,13 +13,13 @@ class ProtoSerializationHandler : SerializationHandler { override fun supports( acceptHeader: MediaType, body: Any, - ): Boolean = body is GeneratedMessageV3 + ): Boolean = body is GeneratedMessage override fun serialize( acceptHeader: MediaType, body: Any, ): String { - val message = body as GeneratedMessageV3 + val message = body as GeneratedMessage return if (json.isCompatibleWith(acceptHeader) || jsonStructuredSuffixWildcard.isCompatibleWith(acceptHeader)) { ProtoBufUtils.toJsonWithoutWrappers(message) } else { diff --git a/router-protobuf/src/test/proto/Sample.proto b/router-protobuf/src/test/proto/Sample.proto index 95c164b..7fb40b7 100644 --- a/router-protobuf/src/test/proto/Sample.proto +++ b/router-protobuf/src/test/proto/Sample.proto @@ -2,6 +2,10 @@ syntax = "proto3"; package io.moia.router.proto.sample; +option java_multiple_files = false; +option java_outer_classname = "SampleOuterClass"; +option java_package = "io.moia.router.proto.sample"; + import "google/protobuf/wrappers.proto"; message Sample { From 7d03618bab0b95b5ef506b76ce73b8a1745c48fe Mon Sep 17 00:00:00 2001 From: Jan Steinke Date: Thu, 4 Jul 2024 16:23:03 +0200 Subject: [PATCH 4/5] re-add protoc management --- router-protobuf/build.gradle.kts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/router-protobuf/build.gradle.kts b/router-protobuf/build.gradle.kts index dfdd730..da4129f 100644 --- a/router-protobuf/build.gradle.kts +++ b/router-protobuf/build.gradle.kts @@ -22,4 +22,12 @@ dependencies { testImplementation("io.mockk:mockk:1.13.11") testImplementation("org.slf4j:slf4j-simple:2.0.13") testImplementation("com.jayway.jsonpath:json-path:2.9.0") +} + +protobuf { + // Configure the protoc executable + protoc { + // Download from repositories + artifact = "com.google.protobuf:protoc:4.27.2" + } } \ No newline at end of file From af5557e152c446e4bec3bad2257d07c1ba402867 Mon Sep 17 00:00:00 2001 From: Jan Steinke Date: Thu, 4 Jul 2024 16:25:26 +0200 Subject: [PATCH 5/5] extract protoVersion --- router-protobuf/build.gradle.kts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/router-protobuf/build.gradle.kts b/router-protobuf/build.gradle.kts index da4129f..8c79fc5 100644 --- a/router-protobuf/build.gradle.kts +++ b/router-protobuf/build.gradle.kts @@ -6,13 +6,15 @@ repositories { mavenCentral() } +val protoVersion = "4.27.2" + dependencies { implementation(kotlin("stdlib")) implementation(kotlin("reflect")) implementation("org.slf4j:slf4j-api:2.0.13") - api("com.google.protobuf:protobuf-java:4.27.2") - api("com.google.protobuf:protobuf-java-util:4.27.2") + api("com.google.protobuf:protobuf-java:$protoVersion") + api("com.google.protobuf:protobuf-java-util:$protoVersion") implementation("com.google.guava:guava:33.2.1-jre") api(project(":router")) @@ -28,6 +30,6 @@ protobuf { // Configure the protoc executable protoc { // Download from repositories - artifact = "com.google.protobuf:protoc:4.27.2" + artifact = "com.google.protobuf:protoc:$protoVersion" } } \ No newline at end of file