Skip to content

Commit

Permalink
Update Protobuf (#410)
Browse files Browse the repository at this point in the history
* Update Protobuf

* migrate to v4

* fix protobuf generation

* re-add protoc management

* extract protoVersion
  • Loading branch information
Jan Steinke authored Jul 4, 2024
1 parent d01321a commit 21e9326
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 16 deletions.
17 changes: 8 additions & 9 deletions router-protobuf/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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"))

Expand All @@ -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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions router-protobuf/src/test/proto/Sample.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 21e9326

Please sign in to comment.