diff --git a/router-protobuf/build.gradle.kts b/router-protobuf/build.gradle.kts index 6d422ccc..8c79fc55 100644 --- a/router-protobuf/build.gradle.kts +++ b/router-protobuf/build.gradle.kts @@ -1,21 +1,20 @@ -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 { 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:3.25.3") - api("com.google.protobuf:protobuf-java-util:3.25.3") + 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")) @@ -27,10 +26,10 @@ dependencies { testImplementation("com.jayway.jsonpath:json-path:2.9.0") } - protobuf { + // Configure the protoc executable protoc { - // The artifact spec for the Protobuf Compiler - artifact = "com.google.protobuf:protoc:3.25.3" + // Download from repositories + artifact = "com.google.protobuf:protoc:$protoVersion" } } \ No newline at end of file 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 e07ecf63..beafb8e6 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) } 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 36a163aa..40ff196a 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 e6fd932c..4a041bd0 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 95c164ba..7fb40b79 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 {