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

Update Protobuf #410

Merged
merged 5 commits into from
Jul 4, 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
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
Loading