diff --git a/gen-tests.gradle b/gen-tests.gradle index 961d29c014..a00d8949cd 100644 --- a/gen-tests.gradle +++ b/gen-tests.gradle @@ -98,7 +98,7 @@ task generateGsonAdapterCompactJavaTests(type: JavaExec) { '--proto_path=wire-library/wire-tests/src/commonTest/shared/proto/proto2', '--java_out=wire-library/wire-gson-support/src/test/java', '--compact', - 'all_types.proto' + 'all_types_proto2.proto' ] } @@ -371,7 +371,7 @@ task generateMoshiAdapterCompactJavaTests(type: JavaExec) { '--proto_path=wire-library/wire-tests/src/commonTest/shared/proto/proto2', '--java_out=wire-library/wire-moshi-adapter/src/test/java', '--compact', - 'all_types.proto' + 'all_types_proto2.proto' ] } @@ -445,7 +445,8 @@ task generateSharedJsonCompactJavaTests(type: JavaExec) { '--proto3-preview=UNSUPPORTED', '--compact', 'all64.proto', - 'all_types.proto', + 'all_types_proto2.proto', + 'all_types_proto3.proto', 'all_structs.proto', 'camel_case.proto', 'pizza.proto', @@ -464,7 +465,8 @@ task generateSharedJsonKotlinTests(type: JavaExec) { '--proto3-preview=UNSUPPORTED', '--java_interop', 'all64.proto', - 'all_types.proto', + 'all_types_proto2.proto', + 'all_types_proto3.proto', 'all_structs.proto', 'camel_case.proto', 'pizza.proto', diff --git a/wire-library/wire-tests/src/commonTest/shared/json/all_types_proto3.json b/wire-library/wire-tests/src/commonTest/shared/json/all_types_proto3.json index 447fe0d639..6d6ccfd895 100644 --- a/wire-library/wire-tests/src/commonTest/shared/json/all_types_proto3.json +++ b/wire-library/wire-tests/src/commonTest/shared/json/all_types_proto3.json @@ -1,19 +1,19 @@ { - "int32": 111, - "uint32": 112, - "sint32": 113, - "fixed32": 114, - "sfixed32": 115, - "int64": "116", - "uint64": "117", - "sint64": "118", - "fixed64": "119", - "sfixed64": "120", - "bool": true, - "float": 122.0, - "double": 123.0, - "string": "124", - "bytes": "e30=", + "myInt32": 111, + "myUint32": 112, + "mySint32": 113, + "myFixed32": 114, + "mySfixed32": 115, + "myInt64": "116", + "myUint64": "117", + "mySint64": "118", + "myFixed64": "119", + "mySfixed64": "120", + "myBool": true, + "myFloat": 122.0, + "myDouble": 123.0, + "myString": "124", + "myBytes": "e30=", "nestedEnum": "A", "nestedMessage": { "a": 999 diff --git a/wire-library/wire-tests/src/commonTest/shared/proto/proto2/all_types.proto b/wire-library/wire-tests/src/commonTest/shared/proto/proto2/all_types_proto2.proto similarity index 100% rename from wire-library/wire-tests/src/commonTest/shared/proto/proto2/all_types.proto rename to wire-library/wire-tests/src/commonTest/shared/proto/proto2/all_types_proto2.proto diff --git a/wire-library/wire-tests/src/commonTest/shared/proto/proto3/all_types_proto3.proto b/wire-library/wire-tests/src/commonTest/shared/proto/proto3/all_types_proto3.proto new file mode 100644 index 0000000000..d8072cb79a --- /dev/null +++ b/wire-library/wire-tests/src/commonTest/shared/proto/proto3/all_types_proto3.proto @@ -0,0 +1,93 @@ +/* + * Copyright 2020 Square Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +syntax = "proto3"; + +package squareup.proto3; + +option java_package = "com.squareup.wire.proto3.alltypes"; + +message AllTypes { + enum NestedEnum { + UNKNOWN = 0; + A = 1; + } + + message NestedMessage { + int32 a = 1; + } + + int32 my_int32 = 1; + uint32 my_uint32 = 2; + sint32 my_sint32 = 3; + fixed32 my_fixed32 = 4; + sfixed32 my_sfixed32 = 5; + int64 my_int64 = 6; + uint64 my_uint64 = 7; + sint64 my_sint64 = 8; + fixed64 my_fixed64 = 9; + sfixed64 my_sfixed64 = 10; + bool my_bool = 11; + float my_float = 12; + double my_double = 13; + string my_string = 14; + bytes my_bytes = 15; + NestedEnum nested_enum = 16; + NestedMessage nested_message = 17; + + repeated int32 rep_int32 = 201 [packed = false]; + repeated uint32 rep_uint32 = 202 [packed = false]; + repeated sint32 rep_sint32 = 203 [packed = false]; + repeated fixed32 rep_fixed32 = 204 [packed = false]; + repeated sfixed32 rep_sfixed32 = 205 [packed = false]; + repeated int64 rep_int64 = 206 [packed = false]; + repeated uint64 rep_uint64 = 207 [packed = false]; + repeated sint64 rep_sint64 = 208 [packed = false]; + repeated fixed64 rep_fixed64 = 209 [packed = false]; + repeated sfixed64 rep_sfixed64 = 210 [packed = false]; + repeated bool rep_bool = 211 [packed = false]; + repeated float rep_float = 212 [packed = false]; + repeated double rep_double = 213 [packed = false]; + repeated string rep_string = 214 [packed = false]; + repeated bytes rep_bytes = 215 [packed = false]; + repeated NestedEnum rep_nested_enum = 216 [packed = false]; + repeated NestedMessage rep_nested_message = 217 [packed = false]; + + repeated int32 pack_int32 = 301 [packed = true]; + repeated uint32 pack_uint32 = 302 [packed = true]; + repeated sint32 pack_sint32 = 303 [packed = true]; + repeated fixed32 pack_fixed32 = 304 [packed = true]; + repeated sfixed32 pack_sfixed32 = 305 [packed = true]; + repeated int64 pack_int64 = 306 [packed = true]; + repeated uint64 pack_uint64 = 307 [packed = true]; + repeated sint64 pack_sint64 = 308 [packed = true]; + repeated fixed64 pack_fixed64 = 309 [packed = true]; + repeated sfixed64 pack_sfixed64 = 310 [packed = true]; + repeated bool pack_bool = 311 [packed = true]; + repeated float pack_float = 312 [packed = true]; + repeated double pack_double = 313 [packed = true]; + repeated NestedEnum pack_nested_enum = 316 [packed = true]; + + map map_int32_int32 = 501; + map map_string_string = 502; + map map_string_message = 503; + map map_string_enum = 504; + + oneof choice { + string oneof_string = 601; + int32 oneof_int32 = 602; + NestedMessage oneof_nested_message = 603; + } +} diff --git a/wire-library/wire-tests/src/jvmJsonTest/kotlin/com/squareup/wire/WireJsonTest.kt b/wire-library/wire-tests/src/jvmJsonTest/kotlin/com/squareup/wire/WireJsonTest.kt index 434dcf7cd6..b8b20189be 100644 --- a/wire-library/wire-tests/src/jvmJsonTest/kotlin/com/squareup/wire/WireJsonTest.kt +++ b/wire-library/wire-tests/src/jvmJsonTest/kotlin/com/squareup/wire/WireJsonTest.kt @@ -21,12 +21,12 @@ import com.google.gson.JsonSyntaxException import com.squareup.moshi.JsonDataException import com.squareup.moshi.Moshi import com.squareup.wire.json.assertJsonEquals -import com.squareup.wire.proto2.alltypes.AllTypes import okio.ByteString import okio.buffer import okio.source import org.assertj.core.api.Assertions.assertThat import org.junit.Assert.fail +import org.junit.Ignore import org.junit.Test import org.junit.runner.RunWith import org.junit.runners.Parameterized @@ -42,6 +42,8 @@ import squareup.proto3.Pizza import squareup.proto3.PizzaDelivery import java.io.File import java.util.Collections +import com.squareup.wire.proto2.alltypes.AllTypes as AllTypesProto2 +import com.squareup.wire.proto3.alltypes.AllTypes as AllTypesProto3 /** * Tests meant to be executed against both Java generated and Kotlin generated code among different @@ -53,37 +55,37 @@ class WireJsonTest { internal lateinit var jsonLibrary: JsonLibrary @Test fun allTypesSerializeTest() { - val value = allTypesBuilder().build() - assertJsonEquals(ALL_TYPES_JSON, jsonLibrary.toJson(value, AllTypes::class.java)) + val value = allTypesProto2Builder().build() + assertJsonEquals(ALL_TYPES_PROTO2_JSON, jsonLibrary.toJson(value, AllTypesProto2::class.java)) } @Test fun allTypesDeserializeTest() { - val value = allTypesBuilder().build() - val parsed = jsonLibrary.fromJson(ALL_TYPES_JSON, AllTypes::class.java) + val value = allTypesProto2Builder().build() + val parsed = jsonLibrary.fromJson(ALL_TYPES_PROTO2_JSON, AllTypesProto2::class.java) assertThat(parsed).isEqualTo(value) assertThat(parsed.toString()).isEqualTo(value.toString()) assertJsonEquals( - jsonLibrary.toJson(parsed, AllTypes::class.java), - jsonLibrary.toJson(value, AllTypes::class.java)) + jsonLibrary.toJson(parsed, AllTypesProto2::class.java), + jsonLibrary.toJson(value, AllTypesProto2::class.java)) } @Test fun allTypesIdentitySerializeTest() { - val value = allTypesIdentityBuilder().build() - assertJsonEquals(ALL_TYPES_IDENTITY_JSON, jsonLibrary.toJson(value, AllTypes::class.java)) + val value = allTypesProto2IdentityBuilder().build() + assertJsonEquals(ALL_TYPES_IDENTITY_PROTO2_JSON, jsonLibrary.toJson(value, AllTypesProto2::class.java)) } @Test fun allTypesIdentityDeserializeTest() { - val value = allTypesIdentityBuilder().build() - val parsed = jsonLibrary.fromJson(ALL_TYPES_IDENTITY_JSON, AllTypes::class.java) + val value = allTypesProto2IdentityBuilder().build() + val parsed = jsonLibrary.fromJson(ALL_TYPES_IDENTITY_PROTO2_JSON, AllTypesProto2::class.java) assertThat(parsed).isEqualTo(value) assertThat(parsed.toString()).isEqualTo(value.toString()) assertJsonEquals( - jsonLibrary.toJson(parsed, AllTypes::class.java), - jsonLibrary.toJson(value, AllTypes::class.java)) + jsonLibrary.toJson(parsed, AllTypesProto2::class.java), + jsonLibrary.toJson(value, AllTypesProto2::class.java)) } @Test fun omitsUnknownFields() { - val builder = allTypesBuilder() + val builder = allTypesProto2Builder() builder.addUnknownField(9000, FieldEncoding.FIXED32, 9000) builder.addUnknownField(9001, FieldEncoding.FIXED64, 9001L) builder.addUnknownField(9002, FieldEncoding.LENGTH_DELIMITED, @@ -91,7 +93,7 @@ class WireJsonTest { builder.addUnknownField(9003, FieldEncoding.VARINT, 9003L) val value = builder.build() - assertJsonEquals(ALL_TYPES_JSON, jsonLibrary.toJson(value, AllTypes::class.java)) + assertJsonEquals(ALL_TYPES_PROTO2_JSON, jsonLibrary.toJson(value, AllTypesProto2::class.java)) } @Test fun fieldNamesAreEncodedWithCamelCaseAndDecodedWithEither() { @@ -409,12 +411,70 @@ class WireJsonTest { assertThat(unsignedJson).contains(""""repUint64":["456"]""") } + @Test fun serializeAllTypesProto3() { + val json = jsonLibrary.toJson(allTypesProto3Builder().build(), AllTypesProto3::class.java) + assertJsonEquals(ALL_TYPES_PROTO3_JSON, json) + } + + @Test fun deserializeAllTypesProto3() { + val allTypes = allTypesProto3Builder().build() + val parsed = jsonLibrary.fromJson(ALL_TYPES_PROTO3_JSON, AllTypesProto3::class.java) + assertThat(parsed).isEqualTo(allTypes) + assertThat(parsed.toString()).isEqualTo(allTypes.toString()) + assertJsonEquals(jsonLibrary.toJson(parsed, AllTypesProto3::class.java), + jsonLibrary.toJson(allTypes, AllTypesProto3::class.java)) + } + + @Test fun serializeIdentityAllTypesMoshi() { + val allTypes = AllTypesProto3.Builder().build() + assertJsonEquals(ALL_TYPES_IDENTITY_PROTO3_JSON, + jsonLibrary.toJson(allTypes, AllTypesProto3::class.java)) + } + + @Test fun deserializeIdentityAllTypesMoshi() { + val allTypes = AllTypesProto3.Builder().build() + val parsed = jsonLibrary.fromJson(ALL_TYPES_IDENTITY_PROTO3_JSON, AllTypesProto3::class.java) + assertThat(parsed).isEqualTo(allTypes) + assertThat(parsed.toString()).isEqualTo(allTypes.toString()) + assertJsonEquals(jsonLibrary.toJson(parsed, AllTypesProto3::class.java), + jsonLibrary.toJson(allTypes, AllTypesProto3::class.java)) + } + + @Test fun serializeExplicitIdentityAllTypesMoshi() { + val value = allTypesExplicitIdentityProto3Builder().build() + assertJsonEquals(ALL_TYPES_EXPLICITY_IDENTITY_PROTO3_JSON, + jsonLibrary.toJson(value, AllTypesProto3::class.java)) + } + + @Ignore("Java needs a fix.") + @Test fun deserializeExplicitIdentityAllTypesMoshi() { + val value = allTypesExplicitIdentityProto3Builder().build() + + val parsed = jsonLibrary.fromJson(ALL_TYPES_EXPLICITY_IDENTITY_PROTO3_JSON, + AllTypesProto3::class.java) + assertThat(parsed).isEqualTo(value) + assertThat(parsed.toString()).isEqualTo(value.toString()) + assertJsonEquals(jsonLibrary.toJson(parsed, AllTypesProto3::class.java), + jsonLibrary.toJson(value, AllTypesProto3::class.java)) + } + + @Test fun minusDoubleZero() { + val value = AllTypesProto3.Builder().my_double(-0.0).build() + val json = "{\"myDouble\": -0.0}" + + assertJsonEquals(json, jsonLibrary.toJson(value, AllTypesProto3::class.java)) + + val parsed = jsonLibrary.fromJson(json, AllTypesProto3::class.java) + assertThat(parsed).isEqualTo(value) + assertThat(parsed.toString()).isEqualTo(value.toString()) + } + companion object { // Return a two-element list with a given repeated value private fun list(x: T): List = listOf(x, x) - private fun allTypesIdentityBuilder(): AllTypes.Builder { - return AllTypes.Builder() + private fun allTypesProto2IdentityBuilder(): AllTypesProto2.Builder { + return AllTypesProto2.Builder() .opt_int32(0) .opt_uint32(0) .opt_sint32(0) @@ -430,7 +490,7 @@ class WireJsonTest { .opt_double(0.0) .opt_string("") .opt_bytes(ByteString.EMPTY) - .opt_nested_enum(AllTypes.NestedEnum.A) + .opt_nested_enum(AllTypesProto2.NestedEnum.A) .opt_nested_message(null) .req_int32(0) .req_uint32(0) @@ -447,14 +507,14 @@ class WireJsonTest { .req_double(0.0) .req_string("") .req_bytes(ByteString.EMPTY) - .req_nested_enum(AllTypes.NestedEnum.A) - .req_nested_message(AllTypes.NestedMessage.Builder().a(0).build()) + .req_nested_enum(AllTypesProto2.NestedEnum.A) + .req_nested_message(AllTypesProto2.NestedMessage.Builder().a(0).build()) } - private fun allTypesBuilder(): AllTypes.Builder { + private fun allTypesProto2Builder(): AllTypesProto2.Builder { val bytes = ByteString.of(123.toByte(), 125.toByte()) - val nestedMessage = AllTypes.NestedMessage.Builder().a(999).build() - return AllTypes.Builder() + val nestedMessage = AllTypesProto2.NestedMessage.Builder().a(999).build() + return AllTypesProto2.Builder() .opt_int32(111) .opt_uint32(112) .opt_sint32(113) @@ -470,7 +530,7 @@ class WireJsonTest { .opt_double(123.0) .opt_string("124") .opt_bytes(bytes) - .opt_nested_enum(AllTypes.NestedEnum.A) + .opt_nested_enum(AllTypesProto2.NestedEnum.A) .opt_nested_message(nestedMessage) .req_int32(111) .req_uint32(112) @@ -487,7 +547,7 @@ class WireJsonTest { .req_double(123.0) .req_string("124") .req_bytes(bytes) - .req_nested_enum(AllTypes.NestedEnum.A) + .req_nested_enum(AllTypesProto2.NestedEnum.A) .req_nested_message(nestedMessage) .rep_int32(list(111)) .rep_uint32(list(112)) @@ -504,7 +564,7 @@ class WireJsonTest { .rep_double(list(123.0)) .rep_string(list("124")) .rep_bytes(list(bytes)) - .rep_nested_enum(list(AllTypes.NestedEnum.A)) + .rep_nested_enum(list(AllTypesProto2.NestedEnum.A)) .rep_nested_message(list(nestedMessage)) .pack_int32(list(111)) .pack_uint32(list(112)) @@ -519,11 +579,11 @@ class WireJsonTest { .pack_bool(list(true)) .pack_float(list(122.0f)) .pack_double(list(123.0)) - .pack_nested_enum(list(AllTypes.NestedEnum.A)) + .pack_nested_enum(list(AllTypesProto2.NestedEnum.A)) .map_int32_int32(Collections.singletonMap(1, 2)) .map_string_string(Collections.singletonMap("key", "value")) - .map_string_message(Collections.singletonMap("message", AllTypes.NestedMessage(1))) - .map_string_enum(Collections.singletonMap("enum", AllTypes.NestedEnum.A)) + .map_string_message(Collections.singletonMap("message", AllTypesProto2.NestedMessage(1))) + .map_string_enum(Collections.singletonMap("enum", AllTypesProto2.NestedEnum.A)) .oneof_int32(4444) .ext_opt_int32(Int.MAX_VALUE) .ext_opt_int64(Long.MIN_VALUE / 2 + 178) @@ -532,7 +592,7 @@ class WireJsonTest { .ext_opt_bool(true) .ext_opt_float(1.2345e6f) .ext_opt_double(1.2345e67) - .ext_opt_nested_enum(AllTypes.NestedEnum.A) + .ext_opt_nested_enum(AllTypesProto2.NestedEnum.A) .ext_opt_nested_message(nestedMessage) .ext_rep_int32(list(Int.MAX_VALUE)) .ext_rep_uint64(list(Long.MIN_VALUE / 2 + 178)) @@ -540,7 +600,7 @@ class WireJsonTest { .ext_rep_bool(list(true)) .ext_rep_float(list(1.2345e6f)) .ext_rep_double(list(1.2345e67)) - .ext_rep_nested_enum(list(AllTypes.NestedEnum.A)) + .ext_rep_nested_enum(list(AllTypesProto2.NestedEnum.A)) .ext_rep_nested_message(list(nestedMessage)) .ext_pack_int32(list(Int.MAX_VALUE)) .ext_pack_uint64(list(Long.MIN_VALUE / 2 + 178)) @@ -548,12 +608,132 @@ class WireJsonTest { .ext_pack_bool(list(true)) .ext_pack_float(list(1.2345e6f)) .ext_pack_double(list(1.2345e67)) - .ext_pack_nested_enum(list(AllTypes.NestedEnum.A)) + .ext_pack_nested_enum(list(AllTypesProto2.NestedEnum.A)) } - private val ALL_TYPES_JSON = loadJson("all_types_proto2.json") + private fun allTypesProto3Builder(): AllTypesProto3.Builder { + return AllTypesProto3.Builder() + .my_int32(111) + .my_uint32(112) + .my_sint32(113) + .my_fixed32(114) + .my_sfixed32(115) + .my_int64(116L) + .my_uint64(117L) + .my_sint64(118L) + .my_fixed64(119L) + .my_sfixed64(120L) + .my_bool(true) + .my_float(122.0F) + .my_double(123.0) + .my_string("124") + .my_bytes(ByteString.of(123, 125)) + .nested_enum(AllTypesProto3.NestedEnum.A) + .nested_message(AllTypesProto3.NestedMessage(999)) + .rep_int32(list(111)) + .rep_uint32(list(112)) + .rep_sint32(list(113)) + .rep_fixed32(list(114)) + .rep_sfixed32(list(115)) + .rep_int64(list(116L)) + .rep_uint64(list(117L)) + .rep_sint64(list(118L)) + .rep_fixed64(list(119L)) + .rep_sfixed64(list(120L)) + .rep_bool(list(true)) + .rep_float(list(122.0F)) + .rep_double(list(123.0)) + .rep_string(list("124")) + .rep_bytes(list(ByteString.of(123, 125))) + .rep_nested_enum(list(AllTypesProto3.NestedEnum.A)) + .rep_nested_message(list(AllTypesProto3.NestedMessage(999))) + .pack_int32(list(111)) + .pack_uint32(list(112)) + .pack_sint32(list(113)) + .pack_fixed32(list(114)) + .pack_sfixed32(list(115)) + .pack_int64(list(116L)) + .pack_uint64(list(117L)) + .pack_sint64(list(118L)) + .pack_fixed64(list(119L)) + .pack_sfixed64(list(120L)) + .pack_bool(list(true)) + .pack_float(list(122.0F)) + .pack_double(list(123.0)) + .pack_nested_enum(list(AllTypesProto3.NestedEnum.A)) + .map_int32_int32(mapOf(1 to 2)) + .map_string_string(mapOf("key" to "value")) + .map_string_message(mapOf("message" to AllTypesProto3.NestedMessage(1))) + .map_string_enum(mapOf("enum" to AllTypesProto3.NestedEnum.A)) + .oneof_int32(0) + } + + private fun allTypesExplicitIdentityProto3Builder(): AllTypesProto3.Builder { + return AllTypesProto3.Builder() + .my_int32(0) + .my_uint32(0) + .my_sint32(0) + .my_fixed32(0) + .my_sfixed32(0) + .my_int64(0L) + .my_uint64(0L) + .my_sint64(0L) + .my_fixed64(0L) + .my_sfixed64(0L) + .my_bool(false) + .my_float(0F) + .my_double(0.0) + .my_string("") + .my_bytes(ByteString.EMPTY) + .nested_enum(AllTypesProto3.NestedEnum.UNKNOWN) + .nested_message(AllTypesProto3.NestedMessage(0)) + .rep_int32(list(0)) + .rep_uint32(list(0)) + .rep_sint32(list(0)) + .rep_fixed32(list(0)) + .rep_sfixed32(emptyList()) + .rep_int64(emptyList()) + .rep_uint64(emptyList()) + .rep_sint64(emptyList()) + .rep_fixed64(emptyList()) + .rep_sfixed64(emptyList()) + .rep_bool(emptyList()) + .rep_float(emptyList()) + .rep_double(emptyList()) + .rep_string(list("")) + .rep_bytes(list(ByteString.EMPTY)) + .rep_nested_enum(emptyList()) + .rep_nested_message(emptyList()) + .pack_int32(emptyList()) + .pack_uint32(emptyList()) + .pack_sint32(emptyList()) + .pack_fixed32(emptyList()) + .pack_sfixed32(list(0)) + .pack_int64(list(0L)) + .pack_uint64(list(0L)) + .pack_sint64(list(0L)) + .pack_fixed64(list(0L)) + .pack_sfixed64(list(0L)) + .pack_bool(list(false)) + .pack_float(list(0F)) + .pack_double(list(0.0)) + .pack_nested_enum(list(AllTypesProto3.NestedEnum.UNKNOWN)) + .map_int32_int32(mapOf(0 to 0)) + .map_string_message(mapOf("" to AllTypesProto3.NestedMessage.Builder().build())) + .map_string_enum(mapOf("" to AllTypesProto3.NestedEnum.UNKNOWN)) + .oneof_int32(0) + } + + private val ALL_TYPES_PROTO2_JSON = loadJson("all_types_proto2.json") + + private val ALL_TYPES_IDENTITY_PROTO2_JSON = loadJson("all_types_identity_proto2.json") + + private val ALL_TYPES_PROTO3_JSON = loadJson("all_types_proto3.json") + + private val ALL_TYPES_IDENTITY_PROTO3_JSON = loadJson("all_types_identity_proto3.json") - private val ALL_TYPES_IDENTITY_JSON = loadJson("all_types_identity_proto2.json") + private val ALL_TYPES_EXPLICITY_IDENTITY_PROTO3_JSON = + loadJson("all_types_explicit_identity_proto3.json") private val CAMEL_CASE_JSON = loadJson("camel_case_proto3.json") diff --git a/wire-protoc-compatibility-tests/build.gradle b/wire-protoc-compatibility-tests/build.gradle index 5aeb228507..fd776b61a5 100644 --- a/wire-protoc-compatibility-tests/build.gradle +++ b/wire-protoc-compatibility-tests/build.gradle @@ -55,10 +55,7 @@ dependencies { implementation deps.wire.runtime testImplementation deps.assertj testImplementation deps.junit - testImplementation deps.moshi testImplementation deps.protobuf.javaUtil - testImplementation deps.wire.gsonSupport - testImplementation deps.wire.moshiAdapter testImplementation deps.wire.testUtils } diff --git a/wire-protoc-compatibility-tests/src/main/proto/squareup/proto3/kotlin/alltypes/all_types.proto b/wire-protoc-compatibility-tests/src/main/proto/squareup/proto3/kotlin/alltypes/all_types.proto index a98edbe23b..59f4c56fa0 100644 --- a/wire-protoc-compatibility-tests/src/main/proto/squareup/proto3/kotlin/alltypes/all_types.proto +++ b/wire-protoc-compatibility-tests/src/main/proto/squareup/proto3/kotlin/alltypes/all_types.proto @@ -27,21 +27,21 @@ message AllTypes { int32 a = 1; } - int32 int32 = 1; - uint32 uint32 = 2; - sint32 sint32 = 3; - fixed32 fixed32 = 4; - sfixed32 sfixed32 = 5; - int64 int64 = 6; - uint64 uint64 = 7; - sint64 sint64 = 8; - fixed64 fixed64 = 9; - sfixed64 sfixed64 = 10; - bool bool = 11; - float float = 12; - double double = 13; - string string = 14; - bytes bytes = 15; + int32 my_int32 = 1; + uint32 my_uint32 = 2; + sint32 my_sint32 = 3; + fixed32 my_fixed32 = 4; + sfixed32 my_sfixed32 = 5; + int64 my_int64 = 6; + uint64 my_uint64 = 7; + sint64 my_sint64 = 8; + fixed64 my_fixed64 = 9; + sfixed64 my_sfixed64 = 10; + bool my_bool = 11; + float my_float = 12; + double my_double = 13; + string my_string = 14; + bytes my_bytes = 15; NestedEnum nested_enum = 16; NestedMessage nested_message = 17; diff --git a/wire-protoc-compatibility-tests/src/test/java/com/squareup/wire/Proto3WireProtocCompatibilityTests.kt b/wire-protoc-compatibility-tests/src/test/java/com/squareup/wire/Proto3WireProtocCompatibilityTests.kt index bfc886ea2a..0afd15a44f 100644 --- a/wire-protoc-compatibility-tests/src/test/java/com/squareup/wire/Proto3WireProtocCompatibilityTests.kt +++ b/wire-protoc-compatibility-tests/src/test/java/com/squareup/wire/Proto3WireProtocCompatibilityTests.kt @@ -26,15 +26,11 @@ import com.google.protobuf.Struct import com.google.protobuf.Timestamp import com.google.protobuf.Value import com.google.protobuf.util.JsonFormat -import com.squareup.moshi.JsonAdapter -import com.squareup.moshi.JsonDataException -import com.squareup.moshi.Moshi import com.squareup.wire.json.assertJsonEquals import okio.ByteString import okio.buffer import okio.source import org.assertj.core.api.Assertions.assertThat -import org.junit.Assert.fail import org.junit.Test import squareup.proto3.kotlin.alltypes.All64OuterClass import squareup.proto3.kotlin.alltypes.AllTypesOuterClass @@ -44,7 +40,6 @@ import java.io.File import com.squareup.wire.proto3.kotlin.requiredextension.RequiredExtension as RequiredExtensionK import com.squareup.wire.proto3.kotlin.requiredextension.RequiredExtensionMessage as RequiredExtensionMessageK import squareup.proto3.java.alltypes.AllTypes as AllTypesJ -import squareup.proto3.kotlin.alltypes.All64 as All64K import squareup.proto3.kotlin.alltypes.AllTypes as AllTypesK import squareup.proto3.kotlin.pizza.PizzaDelivery as PizzaDeliveryK @@ -142,11 +137,6 @@ class Proto3WireProtocCompatibilityTests { assertThat(AllTypesK.ADAPTER.decode(protocBytes)).isEqualTo(wireMessageKotlin) } - @Test fun serializeDefaultAllTypesMoshi() { - assertJsonEquals(DEFAULT_ALL_TYPES_JSON, - moshi.adapter(AllTypesK::class.java).toJson(defaultAllTypesWireKotlin)) - } - @Test fun deserializeDefaultAllTypesProtoc() { val allTypes = defaultAllTypesProtoc val jsonParser = JsonFormat.parser() @@ -160,16 +150,6 @@ class Proto3WireProtocCompatibilityTests { assertJsonEquals(jsonPrinter.print(parsed), jsonPrinter.print(allTypes)) } - @Test fun deserializeDefaultAllTypesMoshi() { - val allTypesAdapter: JsonAdapter = moshi.adapter(AllTypesK::class.java) - - val allTypes = defaultAllTypesWireKotlin - val parsed = allTypesAdapter.fromJson(DEFAULT_ALL_TYPES_JSON) - assertThat(parsed).isEqualTo(allTypes) - assertThat(parsed.toString()).isEqualTo(allTypes.toString()) - assertJsonEquals(allTypesAdapter.toJson(parsed), allTypesAdapter.toJson(allTypes)) - } - @Test fun serializeIdentityAllTypesProtoc() { val identityAllTypes = AllTypesOuterClass.AllTypes.newBuilder().build() @@ -177,22 +157,12 @@ class Proto3WireProtocCompatibilityTests { assertJsonEquals(IDENTITY_ALL_TYPES_JSON, jsonPrinter.print(identityAllTypes)) } - @Test fun serializeIdentityAllTypesMoshi() { - val allTypes = AllTypesK() - assertJsonEquals(IDENTITY_ALL_TYPES_JSON, moshi.adapter(AllTypesK::class.java).toJson(allTypes)) - } - @Test fun serializeExplicitIdentityAllTypesProtoc() { val jsonPrinter = JsonFormat.printer() assertJsonEquals(EXPLICIT_IDENTITY_ALL_TYPES_JSON, jsonPrinter.print(explicitIdentityAllTypesProtoc)) } - @Test fun serializeExplicitIdentityAllTypesMoshi() { - assertJsonEquals(EXPLICIT_IDENTITY_ALL_TYPES_JSON, - moshi.adapter(AllTypesK::class.java).toJson(explicitIdentityAllTypesWireKotlin)) - } - @Test fun deserializeIdentityAllTypesProtoc() { val identityAllTypes = AllTypesOuterClass.AllTypes.newBuilder().build() val jsonParser = JsonFormat.parser() @@ -206,16 +176,6 @@ class Proto3WireProtocCompatibilityTests { assertJsonEquals(jsonPrinter.print(parsed), jsonPrinter.print(identityAllTypes)) } - @Test fun deserializeIdentityAllTypesMoshi() { - val allTypesAdapter: JsonAdapter = moshi.adapter(AllTypesK::class.java) - - val allTypes = AllTypesK() - val parsed = allTypesAdapter.fromJson(IDENTITY_ALL_TYPES_JSON) - assertThat(parsed).isEqualTo(allTypes) - assertThat(parsed.toString()).isEqualTo(allTypes.toString()) - assertJsonEquals(allTypesAdapter.toJson(parsed), allTypesAdapter.toJson(allTypes)) - } - @Test fun deserializeExplicitIdentityAllTypesProtoc() { val jsonParser = JsonFormat.parser() val parsed = AllTypesOuterClass.AllTypes.newBuilder() @@ -228,16 +188,6 @@ class Proto3WireProtocCompatibilityTests { assertJsonEquals(jsonPrinter.print(parsed), jsonPrinter.print(explicitIdentityAllTypesProtoc)) } - @Test fun deserializeExplicitIdentityAllTypesMoshi() { - val allTypesAdapter: JsonAdapter = moshi.adapter(AllTypesK::class.java) - - val parsed = allTypesAdapter.fromJson(EXPLICIT_IDENTITY_ALL_TYPES_JSON) - assertThat(parsed).isEqualTo(explicitIdentityAllTypesWireKotlin) - assertThat(parsed.toString()).isEqualTo(explicitIdentityAllTypesWireKotlin.toString()) - assertJsonEquals(allTypesAdapter.toJson(parsed), - allTypesAdapter.toJson(explicitIdentityAllTypesWireKotlin)) - } - @Test fun `protoc validation camel case json`() { val protocCamel = CamelCaseOuterClass.CamelCase.newBuilder() .setNestedMessage(CamelCaseOuterClass.CamelCase.NestedCamelCase.newBuilder().setOneInt32(1)) @@ -365,45 +315,35 @@ class Proto3WireProtocCompatibilityTests { @Test fun minusDoubleZero() { val protoc = AllTypesOuterClass.AllTypes.newBuilder() - .setDouble(-0.0) + .setMyDouble(-0.0) .build() - val wireKotlin = AllTypesK(squareup_proto3_kotlin_alltypes_double = -0.0) + val wireKotlin = AllTypesK(my_double = -0.0) val protocByteArray = protoc.toByteArray() assertThat(AllTypesK.ADAPTER.encode(wireKotlin)).isEqualTo(protocByteArray) assertThat(AllTypesK.ADAPTER.decode(protocByteArray)).isEqualTo(wireKotlin) val protocJson = JsonFormat.printer().print(protoc) - val wireKotlinJson = moshi.adapter(AllTypesK::class.java).toJson(wireKotlin) - assertJsonEquals("{\"double\": -0.0}", protocJson) - // We parse-check because Wire prints empty lists and empty maps while protoc doesn't. - assertThat(wireKotlinJson).contains("\"double\":-0.0") - - val parsedWireKotlin = moshi.adapter(AllTypesK::class.java).fromJson(protocJson) - assertThat(parsedWireKotlin).isEqualTo(wireKotlin) + assertJsonEquals("{\"myDouble\": -0.0}", protocJson) } companion object { - private val moshi = Moshi.Builder() - .add(WireJsonAdapterFactory()) - .build() - private val defaultAllTypesProtoc = AllTypesOuterClass.AllTypes.newBuilder() - .setInt32(111) - .setUint32(112) - .setSint32(113) - .setFixed32(114) - .setSfixed32(115) - .setInt64(116L) - .setUint64(117L) - .setSint64(118L) - .setFixed64(119L) - .setSfixed64(120L) - .setBool(true) - .setFloat(122.0F) - .setDouble(123.0) - .setString("124") - .setBytes(com.google.protobuf.ByteString.copyFrom(ByteString.of(123, 125).toByteArray())) + .setMyInt32(111) + .setMyUint32(112) + .setMySint32(113) + .setMyFixed32(114) + .setMySfixed32(115) + .setMyInt64(116L) + .setMyUint64(117L) + .setMySint64(118L) + .setMyFixed64(119L) + .setMySfixed64(120L) + .setMyBool(true) + .setMyFloat(122.0F) + .setMyDouble(123.0) + .setMyString("124") + .setMyBytes(com.google.protobuf.ByteString.copyFrom(ByteString.of(123, 125).toByteArray())) .setNestedEnum(AllTypesOuterClass.AllTypes.NestedEnum.A) .setNestedMessage(AllTypesOuterClass.AllTypes.NestedMessage.newBuilder().setA(999).build()) .addAllRepInt32(list(111)) @@ -448,21 +388,21 @@ class Proto3WireProtocCompatibilityTests { .build() private val defaultAllTypesWireKotlin = AllTypesK( - squareup_proto3_kotlin_alltypes_int32 = 111, - squareup_proto3_kotlin_alltypes_uint32 = 112, - squareup_proto3_kotlin_alltypes_sint32 = 113, - squareup_proto3_kotlin_alltypes_fixed32 = 114, - squareup_proto3_kotlin_alltypes_sfixed32 = 115, - squareup_proto3_kotlin_alltypes_int64 = 116L, - squareup_proto3_kotlin_alltypes_uint64 = 117L, - squareup_proto3_kotlin_alltypes_sint64 = 118L, - squareup_proto3_kotlin_alltypes_fixed64 = 119L, - squareup_proto3_kotlin_alltypes_sfixed64 = 120L, - squareup_proto3_kotlin_alltypes_bool = true, - squareup_proto3_kotlin_alltypes_float = 122.0F, - squareup_proto3_kotlin_alltypes_double = 123.0, - squareup_proto3_kotlin_alltypes_string = "124", - squareup_proto3_kotlin_alltypes_bytes = ByteString.of(123, 125), + my_int32 = 111, + my_uint32 = 112, + my_sint32 = 113, + my_fixed32 = 114, + my_sfixed32 = 115, + my_int64 = 116L, + my_uint64 = 117L, + my_sint64 = 118L, + my_fixed64 = 119L, + my_sfixed64 = 120L, + my_bool = true, + my_float = 122.0F, + my_double = 123.0, + my_string = "124", + my_bytes = ByteString.of(123, 125), nested_enum = AllTypesK.NestedEnum.A, nested_message = AllTypesK.NestedMessage(a = 999), rep_int32 = list(111), @@ -575,21 +515,21 @@ class Proto3WireProtocCompatibilityTests { private val EXPLICIT_IDENTITY_ALL_TYPES_JSON = loadJson("all_types_explicit_identity_proto3.json") private val explicitIdentityAllTypesWireKotlin = AllTypesK( - squareup_proto3_kotlin_alltypes_int32 = 0, - squareup_proto3_kotlin_alltypes_uint32 = 0, - squareup_proto3_kotlin_alltypes_sint32 = 0, - squareup_proto3_kotlin_alltypes_fixed32 = 0, - squareup_proto3_kotlin_alltypes_sfixed32 = 0, - squareup_proto3_kotlin_alltypes_int64 = 0L, - squareup_proto3_kotlin_alltypes_uint64 = 0L, - squareup_proto3_kotlin_alltypes_sint64 = 0L, - squareup_proto3_kotlin_alltypes_fixed64 = 0L, - squareup_proto3_kotlin_alltypes_sfixed64 = 0L, - squareup_proto3_kotlin_alltypes_bool = false, - squareup_proto3_kotlin_alltypes_float = 0F, - squareup_proto3_kotlin_alltypes_double = 0.0, - squareup_proto3_kotlin_alltypes_string = "", - squareup_proto3_kotlin_alltypes_bytes = ByteString.EMPTY, + my_int32 = 0, + my_uint32 = 0, + my_sint32 = 0, + my_fixed32 = 0, + my_sfixed32 = 0, + my_int64 = 0L, + my_uint64 = 0L, + my_sint64 = 0L, + my_fixed64 = 0L, + my_sfixed64 = 0L, + my_bool = false, + my_float = 0F, + my_double = 0.0, + my_string = "", + my_bytes = ByteString.EMPTY, nested_enum = AllTypesK.NestedEnum.UNKNOWN, nested_message = AllTypesK.NestedMessage(a = 0), rep_int32 = list(0), @@ -679,27 +619,27 @@ class Proto3WireProtocCompatibilityTests { .pack_double(list(0.0)) .pack_nested_enum(list(AllTypesJ.NestedEnum.UNKNOWN)) .map_int32_int32(mapOf(0 to 0)) - .map_string_message(mapOf("" to AllTypesJ.NestedMessage(null))) + .map_string_message(mapOf("" to AllTypesJ.NestedMessage.Builder().build())) .map_string_enum(mapOf("" to AllTypesJ.NestedEnum.UNKNOWN)) .oneof_int32(0) .build() private val explicitIdentityAllTypesProtoc = AllTypesOuterClass.AllTypes.newBuilder() - .setInt32(0) - .setUint32(0) - .setSint32(0) - .setFixed32(0) - .setSfixed32(0) - .setInt64(0L) - .setUint64(0L) - .setSint64(0L) - .setFixed64(0L) - .setSfixed64(0L) - .setBool(false) - .setFloat(0F) - .setDouble(0.0) - .setString("") - .setBytes(com.google.protobuf.ByteString.copyFrom(ByteString.EMPTY.toByteArray())) + .setMyInt32(0) + .setMyUint32(0) + .setMySint32(0) + .setMyFixed32(0) + .setMySfixed32(0) + .setMyInt64(0L) + .setMyUint64(0L) + .setMySint64(0L) + .setMyFixed64(0L) + .setMySfixed64(0L) + .setMyBool(false) + .setMyFloat(0F) + .setMyDouble(0.0) + .setMyString("") + .setMyBytes(com.google.protobuf.ByteString.copyFrom(ByteString.EMPTY.toByteArray())) .setNestedEnum(AllTypesOuterClass.AllTypes.NestedEnum.UNKNOWN) .setNestedMessage(AllTypesOuterClass.AllTypes.NestedMessage.newBuilder().setA(0).build()) .addAllRepInt32(list(0))