summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelipe Rotilho <felipe@rotilho.com>2024-01-12 16:09:03 +0100
committerGitHub <noreply@github.com>2024-01-12 16:09:03 +0100
commitb3f6e0f5405ab81c354ccaeeb2cc13ba278e4bfa (patch)
treed96b2a7dc43bc039bc621bbe8e884a0cab76f001
parentc10428e5d1fc03cbb012db5c8be0c98aafe8dd16 (diff)
downloadkotlinx.serialization-b3f6e0f5405ab81c354ccaeeb2cc13ba278e4bfa.tar.gz
Add value class support to the ProtoBufSchemaGenerator (#2542)
Fixes #2089
-rw-r--r--formats/protobuf/commonMain/src/kotlinx/serialization/protobuf/schema/ProtoBufSchemaGenerator.kt19
-rw-r--r--formats/protobuf/jvmTest/resources/OptionalClass.proto18
-rw-r--r--formats/protobuf/jvmTest/resources/common/schema.proto18
-rw-r--r--formats/protobuf/jvmTest/src/kotlinx/serialization/protobuf/schema/GenerationTest.kt16
4 files changed, 56 insertions, 15 deletions
diff --git a/formats/protobuf/commonMain/src/kotlinx/serialization/protobuf/schema/ProtoBufSchemaGenerator.kt b/formats/protobuf/commonMain/src/kotlinx/serialization/protobuf/schema/ProtoBufSchemaGenerator.kt
index b22df626..4f4ca9c4 100644
--- a/formats/protobuf/commonMain/src/kotlinx/serialization/protobuf/schema/ProtoBufSchemaGenerator.kt
+++ b/formats/protobuf/commonMain/src/kotlinx/serialization/protobuf/schema/ProtoBufSchemaGenerator.kt
@@ -216,29 +216,34 @@ public object ProtoBufSchemaGenerator {
val messageDescriptor = messageType.descriptor
val fieldDescriptor = messageDescriptor.getElementDescriptor(index)
+ var unwrappedFieldDescriptor = fieldDescriptor
+ while (unwrappedFieldDescriptor.isInline) {
+ unwrappedFieldDescriptor = unwrappedFieldDescriptor.getElementDescriptor(0)
+ }
+
val nestedTypes: List<TypeDefinition>
val typeName: String = when {
messageDescriptor.isSealedPolymorphic && index == 1 -> {
appendLine(" // decoded as message with one of these types:")
- nestedTypes = fieldDescriptor.elementDescriptors.map { TypeDefinition(it) }.toList()
+ nestedTypes = unwrappedFieldDescriptor.elementDescriptors.map { TypeDefinition(it) }.toList()
nestedTypes.forEachIndexed { _, childType ->
append(" // message ").append(childType.descriptor.messageOrEnumName).append(", serial name '")
.append(removeLineBreaks(childType.descriptor.serialName)).appendLine('\'')
}
- fieldDescriptor.scalarTypeName()
+ unwrappedFieldDescriptor.scalarTypeName()
}
- fieldDescriptor.isProtobufScalar -> {
+ unwrappedFieldDescriptor.isProtobufScalar -> {
nestedTypes = emptyList()
- fieldDescriptor.scalarTypeName(messageDescriptor.getElementAnnotations(index))
+ unwrappedFieldDescriptor.scalarTypeName(messageDescriptor.getElementAnnotations(index))
}
- fieldDescriptor.isOpenPolymorphic -> {
+ unwrappedFieldDescriptor.isOpenPolymorphic -> {
nestedTypes = listOf(SyntheticPolymorphicType)
SyntheticPolymorphicType.descriptor.serialName
}
else -> {
// enum or regular message
- nestedTypes = listOf(TypeDefinition(fieldDescriptor))
- fieldDescriptor.messageOrEnumName
+ nestedTypes = listOf(TypeDefinition(unwrappedFieldDescriptor))
+ unwrappedFieldDescriptor.messageOrEnumName
}
}
diff --git a/formats/protobuf/jvmTest/resources/OptionalClass.proto b/formats/protobuf/jvmTest/resources/OptionalClass.proto
index 41fdba71..68fda00a 100644
--- a/formats/protobuf/jvmTest/resources/OptionalClass.proto
+++ b/formats/protobuf/jvmTest/resources/OptionalClass.proto
@@ -5,9 +5,21 @@ package kotlinx.serialization.protobuf.schema.generator;
// serial name 'kotlinx.serialization.protobuf.schema.GenerationTest.OptionalClass'
message OptionalClass {
required int32 requiredInt = 1;
+ required int32 requiredUInt = 2;
+ required int32 requiredWrappedUInt = 3;
// WARNING: a default value decoded when value is missing
- optional int32 optionalInt = 2;
- optional int32 nullableInt = 3;
+ optional int32 optionalInt = 4;
// WARNING: a default value decoded when value is missing
- optional int32 nullableOptionalInt = 4;
+ optional int32 optionalUInt = 5;
+ // WARNING: a default value decoded when value is missing
+ optional int32 optionalWrappedUInt = 6;
+ optional int32 nullableInt = 7;
+ optional int32 nullableUInt = 8;
+ optional int32 nullableWrappedUInt = 9;
+ // WARNING: a default value decoded when value is missing
+ optional int32 nullableOptionalInt = 10;
+ // WARNING: a default value decoded when value is missing
+ optional int32 nullableOptionalUInt = 11;
+ // WARNING: a default value decoded when value is missing
+ optional int32 nullableOptionalWrappedUInt = 12;
}
diff --git a/formats/protobuf/jvmTest/resources/common/schema.proto b/formats/protobuf/jvmTest/resources/common/schema.proto
index 79e2d79e..44b5a181 100644
--- a/formats/protobuf/jvmTest/resources/common/schema.proto
+++ b/formats/protobuf/jvmTest/resources/common/schema.proto
@@ -63,11 +63,23 @@ message MapClass {
// serial name 'kotlinx.serialization.protobuf.schema.GenerationTest.OptionalClass'
message OptionalClass {
required int32 requiredInt = 1;
+ required int32 requiredUInt = 2;
+ required int32 requiredWrappedUInt = 3;
// WARNING: a default value decoded when value is missing
- optional int32 optionalInt = 2;
- optional int32 nullableInt = 3;
+ optional int32 optionalInt = 4;
// WARNING: a default value decoded when value is missing
- optional int32 nullableOptionalInt = 4;
+ optional int32 optionalUInt = 5;
+ // WARNING: a default value decoded when value is missing
+ optional int32 optionalWrappedUInt = 6;
+ optional int32 nullableInt = 7;
+ optional int32 nullableUInt = 8;
+ optional int32 nullableWrappedUInt = 9;
+ // WARNING: a default value decoded when value is missing
+ optional int32 nullableOptionalInt = 10;
+ // WARNING: a default value decoded when value is missing
+ optional int32 nullableOptionalUInt = 11;
+ // WARNING: a default value decoded when value is missing
+ optional int32 nullableOptionalWrappedUInt = 12;
}
// serial name 'kotlinx.serialization.protobuf.schema.GenerationTest.ContextualHolder'
diff --git a/formats/protobuf/jvmTest/src/kotlinx/serialization/protobuf/schema/GenerationTest.kt b/formats/protobuf/jvmTest/src/kotlinx/serialization/protobuf/schema/GenerationTest.kt
index 61a2dce5..f2a44234 100644
--- a/formats/protobuf/jvmTest/src/kotlinx/serialization/protobuf/schema/GenerationTest.kt
+++ b/formats/protobuf/jvmTest/src/kotlinx/serialization/protobuf/schema/GenerationTest.kt
@@ -61,7 +61,7 @@ class GenerationTest {
@ProtoNumber(5)
val b: Int,
@ProtoNumber(3)
- val c: Int
+ val c: UInt,
)
@Serializable
@@ -84,6 +84,10 @@ class GenerationTest {
@Serializable
data class OptionsClass(val i: Int)
+ @JvmInline
+ @Serializable
+ value class WrappedUInt(val i : UInt)
+
@Serializable
class ListClass(
val intList: List<Int>,
@@ -113,9 +117,17 @@ class GenerationTest {
@Serializable
data class OptionalClass(
val requiredInt: Int,
+ val requiredUInt: UInt,
+ val requiredWrappedUInt: WrappedUInt,
val optionalInt: Int = 5,
+ val optionalUInt: UInt = 5U,
+ val optionalWrappedUInt: WrappedUInt = WrappedUInt(5U),
val nullableInt: Int?,
- val nullableOptionalInt: Int? = 10
+ val nullableUInt: UInt?,
+ val nullableWrappedUInt: WrappedUInt?,
+ val nullableOptionalInt: Int? = 10,
+ val nullableOptionalUInt: UInt? = 10U,
+ val nullableOptionalWrappedUInt: WrappedUInt? = WrappedUInt(10U),
)
@Serializable