From 1d3652d194c898c059bc381643e210ec86bd62e5 Mon Sep 17 00:00:00 2001 From: Tim Ortel <100865202+TimOrtel@users.noreply.github.com> Date: Tue, 7 Mar 2023 21:01:51 +0100 Subject: [PATCH] Fix issue with messages with no fields. --- buildSrc/src/main/kotlin/TestServer.kt | 12 +++++++++ .../src/commonMain/proto/basicMessages.proto | 7 +++++ .../test/EqTest.kt | 6 +++++ .../test/RpcTest.kt | 27 ++++++++++++++++--- grpc-multiplatform-lib/build.gradle.kts | 2 +- .../KMChannel.kt | 2 -- plugin/build.gradle.kts | 2 +- .../proto_file/IosJvmProtoFileWriteBase.kt | 10 ++++++- readme.md | 6 ++++- 9 files changed, 64 insertions(+), 10 deletions(-) diff --git a/buildSrc/src/main/kotlin/TestServer.kt b/buildSrc/src/main/kotlin/TestServer.kt index da37ce6..c25ecf2 100644 --- a/buildSrc/src/main/kotlin/TestServer.kt +++ b/buildSrc/src/main/kotlin/TestServer.kt @@ -15,6 +15,10 @@ object TestServer { server = NettyServerBuilder .forPort(17888) .addService(object : TestServiceGrpcKt.TestServiceCoroutineImplBase() { + override suspend fun emptyRpc(request: EmptyMessage): EmptyMessage { + return request + } + override suspend fun simpleRpc(request: SimpleMessage): SimpleMessage { return request } @@ -27,6 +31,14 @@ object TestServer { return request } + override fun emptyStream(request: EmptyMessage): Flow { + return flow { + emit(request) + emit(request) + emit(request) + } + } + override fun simpleStreamingRpc(request: SimpleMessage): Flow { return flow { emit(request) diff --git a/grpc-mp-test/src/commonMain/proto/basicMessages.proto b/grpc-mp-test/src/commonMain/proto/basicMessages.proto index 21e7164..9373061 100644 --- a/grpc-mp-test/src/commonMain/proto/basicMessages.proto +++ b/grpc-mp-test/src/commonMain/proto/basicMessages.proto @@ -4,6 +4,10 @@ package io.github.timortel.kotlin_multiplatform_grpc_plugin.test.basic_messages; option java_multiple_files = true; +message EmptyMessage { + +} + message SimpleMessage { string field1 = 1; } @@ -112,10 +116,13 @@ message OneOfMessage { } service TestService { + rpc emptyRpc (EmptyMessage) returns (EmptyMessage); + rpc simpleRpc (SimpleMessage) returns (SimpleMessage); rpc scalarRpc (ScalarTypes) returns (ScalarTypes); rpc everythingRpc (MessageWithEverything) returns (MessageWithEverything); + rpc emptyStream (EmptyMessage) returns (stream EmptyMessage); rpc simpleStreamingRpc (SimpleMessage) returns (stream SimpleMessage); rpc everythingStreamingRpc (MessageWithEverything) returns (stream MessageWithEverything); } \ No newline at end of file diff --git a/grpc-mp-test/src/commonTest/kotlin/io/github/timortel/kotlin_multiplatform_grpc_plugin/test/EqTest.kt b/grpc-mp-test/src/commonTest/kotlin/io/github/timortel/kotlin_multiplatform_grpc_plugin/test/EqTest.kt index 1c30446..51cbce5 100644 --- a/grpc-mp-test/src/commonTest/kotlin/io/github/timortel/kotlin_multiplatform_grpc_plugin/test/EqTest.kt +++ b/grpc-mp-test/src/commonTest/kotlin/io/github/timortel/kotlin_multiplatform_grpc_plugin/test/EqTest.kt @@ -1,5 +1,6 @@ package io.github.timortel.kotlin_multiplatform_grpc_plugin.test +import io.github.timortel.kotlin_multiplatform_grpc_plugin.test.basic_messages.kmEmptyMessage import io.github.timortel.kotlin_multiplatform_grpc_plugin.test.basic_messages.kmSimpleMessage import kotlin.test.Test import kotlin.test.assertEquals @@ -7,6 +8,11 @@ import kotlin.test.assertNotEquals class EqTest { + @Test + fun emptyMessageEquals() { + assertEquals(kmEmptyMessage { }, kmEmptyMessage { }) + } + @Test fun scalarMessageEquals() { assertEquals(createScalarMessage(), createScalarMessage()) diff --git a/grpc-mp-test/src/commonTest/kotlin/io/github/timortel/kotlin_multiplatform_grpc_plugin/test/RpcTest.kt b/grpc-mp-test/src/commonTest/kotlin/io/github/timortel/kotlin_multiplatform_grpc_plugin/test/RpcTest.kt index ff228d0..54de2c1 100644 --- a/grpc-mp-test/src/commonTest/kotlin/io/github/timortel/kotlin_multiplatform_grpc_plugin/test/RpcTest.kt +++ b/grpc-mp-test/src/commonTest/kotlin/io/github/timortel/kotlin_multiplatform_grpc_plugin/test/RpcTest.kt @@ -1,10 +1,7 @@ package io.github.timortel.kotlin_multiplatform_grpc_plugin.test import io.github.timortel.kotlin_multiplatform_grpc_lib.KMChannel -import io.github.timortel.kotlin_multiplatform_grpc_plugin.test.basic_messages.KMMessageWithEverything -import io.github.timortel.kotlin_multiplatform_grpc_plugin.test.basic_messages.KMSimpleMessage -import io.github.timortel.kotlin_multiplatform_grpc_plugin.test.basic_messages.KMTestServiceStub -import io.github.timortel.kotlin_multiplatform_grpc_plugin.test.basic_messages.kmSimpleMessage +import io.github.timortel.kotlin_multiplatform_grpc_plugin.test.basic_messages.* import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.toCollection @@ -22,6 +19,17 @@ class RpcTest { private val stub = KMTestServiceStub(channel) + @Test + fun testEmpty() { + runTest { + val message = kmEmptyMessage { } + val response = stub + .emptyRpc(message) + + assertEquals(message, response) + } + } + @Test fun testSimple() { runTest { @@ -55,6 +63,17 @@ class RpcTest { } } + @Test + fun testStreamEmpty() { + runTest { + val message = kmEmptyMessage { } + val flow: Flow = stub + .emptyStream(message) + + assertEquals(listOf(message, message, message), flow.toList()) + } + } + @Test fun testStreamSimple() { runTest { diff --git a/grpc-multiplatform-lib/build.gradle.kts b/grpc-multiplatform-lib/build.gradle.kts index 3f4801b..d8d25b9 100644 --- a/grpc-multiplatform-lib/build.gradle.kts +++ b/grpc-multiplatform-lib/build.gradle.kts @@ -6,7 +6,7 @@ plugins { } group = "io.github.timortel" -version = "0.2.1" +version = "0.3.0" repositories { mavenCentral() diff --git a/grpc-multiplatform-lib/src/commonMain/kotlin/io/github/timortel/kotlin_multiplatform_grpc_lib/KMChannel.kt b/grpc-multiplatform-lib/src/commonMain/kotlin/io/github/timortel/kotlin_multiplatform_grpc_lib/KMChannel.kt index 5245acd..c332c2f 100644 --- a/grpc-multiplatform-lib/src/commonMain/kotlin/io/github/timortel/kotlin_multiplatform_grpc_lib/KMChannel.kt +++ b/grpc-multiplatform-lib/src/commonMain/kotlin/io/github/timortel/kotlin_multiplatform_grpc_lib/KMChannel.kt @@ -1,7 +1,5 @@ package io.github.timortel.kotlin_multiplatform_grpc_lib -import io.github.timortel.kotlin_multiplatform_grpc_lib.util.TimeUnit - /** * Wrapps around the GRPC-Channel */ diff --git a/plugin/build.gradle.kts b/plugin/build.gradle.kts index 79b4db7..e21afbf 100644 --- a/plugin/build.gradle.kts +++ b/plugin/build.gradle.kts @@ -7,7 +7,7 @@ plugins { } group = "io.github.timortel" -version = "0.2.1" +version = "0.3.0" java { withSourcesJar() diff --git a/plugin/src/main/java/io/github/timortel/kotlin_multiplatform_grpc_plugin/generate_mulitplatform_sources/generators/proto_file/IosJvmProtoFileWriteBase.kt b/plugin/src/main/java/io/github/timortel/kotlin_multiplatform_grpc_plugin/generate_mulitplatform_sources/generators/proto_file/IosJvmProtoFileWriteBase.kt index 2237578..ead673b 100644 --- a/plugin/src/main/java/io/github/timortel/kotlin_multiplatform_grpc_plugin/generate_mulitplatform_sources/generators/proto_file/IosJvmProtoFileWriteBase.kt +++ b/plugin/src/main/java/io/github/timortel/kotlin_multiplatform_grpc_plugin/generate_mulitplatform_sources/generators/proto_file/IosJvmProtoFileWriteBase.kt @@ -123,7 +123,10 @@ abstract class IosJvmProtoFileWriteBase(private val protoFile: ProtoFile) : FunSpec .builder(Const.Message.Companion.IOS.DataDeserializationFunction.NAME) .addModifiers(KModifier.OVERRIDE) - .addParameter(Const.Message.Companion.IOS.DataDeserializationFunction.DATA_PARAM, serializedDataType) + .addParameter( + Const.Message.Companion.IOS.DataDeserializationFunction.DATA_PARAM, + serializedDataType + ) .addCode( deserializeFunctionCode ) @@ -234,6 +237,11 @@ abstract class IosJvmProtoFileWriteBase(private val protoFile: ProtoFile) : Const.Message.OneOf.IosJvm.REQUIRED_SIZE_PROPERTY_NAME ) } + + // Fallback for messages without any fields. + if (onlyNonOneOfAttributes.isEmpty() && message.oneOfs.isEmpty()) { + add("0") + } } .build() } diff --git a/readme.md b/readme.md index d9607f3..27d55c6 100644 --- a/readme.md +++ b/readme.md @@ -1,5 +1,5 @@ [![GitHub license](https://img.shields.io/badge/license-Apache%20License%202.0-blue.svg?style=flat)](http://www.apache.org/licenses/LICENSE-2.0) -![version](https://img.shields.io/badge/version-0.2.0-blue) +![version](https://img.shields.io/badge/version-0.3.0-blue) ![badge][badge-android] ![badge][badge-jvm] @@ -9,6 +9,10 @@ # gRPC Kotlin Multiplatform This projects implements client-side gRPC for Android, JVM, iOS and the web. +**⚠️ Warning: This project is still under development and does not support all gRPC features!** + +**⚠️ Warning: The implementation for javascript is currently not covered by unit tests!** + ## Table of contents - [Features](#features) - [Usage](#usage)