Skip to content

Commit b3f6e0f

Browse files
authored
Add value class support to the ProtoBufSchemaGenerator (#2542)
Fixes #2089
1 parent c10428e commit b3f6e0f

File tree

4 files changed

+56
-15
lines changed

4 files changed

+56
-15
lines changed

formats/protobuf/commonMain/src/kotlinx/serialization/protobuf/schema/ProtoBufSchemaGenerator.kt

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,29 +216,34 @@ public object ProtoBufSchemaGenerator {
216216
val messageDescriptor = messageType.descriptor
217217

218218
val fieldDescriptor = messageDescriptor.getElementDescriptor(index)
219+
var unwrappedFieldDescriptor = fieldDescriptor
220+
while (unwrappedFieldDescriptor.isInline) {
221+
unwrappedFieldDescriptor = unwrappedFieldDescriptor.getElementDescriptor(0)
222+
}
223+
219224
val nestedTypes: List<TypeDefinition>
220225
val typeName: String = when {
221226
messageDescriptor.isSealedPolymorphic && index == 1 -> {
222227
appendLine(" // decoded as message with one of these types:")
223-
nestedTypes = fieldDescriptor.elementDescriptors.map { TypeDefinition(it) }.toList()
228+
nestedTypes = unwrappedFieldDescriptor.elementDescriptors.map { TypeDefinition(it) }.toList()
224229
nestedTypes.forEachIndexed { _, childType ->
225230
append(" // message ").append(childType.descriptor.messageOrEnumName).append(", serial name '")
226231
.append(removeLineBreaks(childType.descriptor.serialName)).appendLine('\'')
227232
}
228-
fieldDescriptor.scalarTypeName()
233+
unwrappedFieldDescriptor.scalarTypeName()
229234
}
230-
fieldDescriptor.isProtobufScalar -> {
235+
unwrappedFieldDescriptor.isProtobufScalar -> {
231236
nestedTypes = emptyList()
232-
fieldDescriptor.scalarTypeName(messageDescriptor.getElementAnnotations(index))
237+
unwrappedFieldDescriptor.scalarTypeName(messageDescriptor.getElementAnnotations(index))
233238
}
234-
fieldDescriptor.isOpenPolymorphic -> {
239+
unwrappedFieldDescriptor.isOpenPolymorphic -> {
235240
nestedTypes = listOf(SyntheticPolymorphicType)
236241
SyntheticPolymorphicType.descriptor.serialName
237242
}
238243
else -> {
239244
// enum or regular message
240-
nestedTypes = listOf(TypeDefinition(fieldDescriptor))
241-
fieldDescriptor.messageOrEnumName
245+
nestedTypes = listOf(TypeDefinition(unwrappedFieldDescriptor))
246+
unwrappedFieldDescriptor.messageOrEnumName
242247
}
243248
}
244249

formats/protobuf/jvmTest/resources/OptionalClass.proto

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,21 @@ package kotlinx.serialization.protobuf.schema.generator;
55
// serial name 'kotlinx.serialization.protobuf.schema.GenerationTest.OptionalClass'
66
message OptionalClass {
77
required int32 requiredInt = 1;
8+
required int32 requiredUInt = 2;
9+
required int32 requiredWrappedUInt = 3;
810
// WARNING: a default value decoded when value is missing
9-
optional int32 optionalInt = 2;
10-
optional int32 nullableInt = 3;
11+
optional int32 optionalInt = 4;
1112
// WARNING: a default value decoded when value is missing
12-
optional int32 nullableOptionalInt = 4;
13+
optional int32 optionalUInt = 5;
14+
// WARNING: a default value decoded when value is missing
15+
optional int32 optionalWrappedUInt = 6;
16+
optional int32 nullableInt = 7;
17+
optional int32 nullableUInt = 8;
18+
optional int32 nullableWrappedUInt = 9;
19+
// WARNING: a default value decoded when value is missing
20+
optional int32 nullableOptionalInt = 10;
21+
// WARNING: a default value decoded when value is missing
22+
optional int32 nullableOptionalUInt = 11;
23+
// WARNING: a default value decoded when value is missing
24+
optional int32 nullableOptionalWrappedUInt = 12;
1325
}

formats/protobuf/jvmTest/resources/common/schema.proto

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,23 @@ message MapClass {
6363
// serial name 'kotlinx.serialization.protobuf.schema.GenerationTest.OptionalClass'
6464
message OptionalClass {
6565
required int32 requiredInt = 1;
66+
required int32 requiredUInt = 2;
67+
required int32 requiredWrappedUInt = 3;
6668
// WARNING: a default value decoded when value is missing
67-
optional int32 optionalInt = 2;
68-
optional int32 nullableInt = 3;
69+
optional int32 optionalInt = 4;
6970
// WARNING: a default value decoded when value is missing
70-
optional int32 nullableOptionalInt = 4;
71+
optional int32 optionalUInt = 5;
72+
// WARNING: a default value decoded when value is missing
73+
optional int32 optionalWrappedUInt = 6;
74+
optional int32 nullableInt = 7;
75+
optional int32 nullableUInt = 8;
76+
optional int32 nullableWrappedUInt = 9;
77+
// WARNING: a default value decoded when value is missing
78+
optional int32 nullableOptionalInt = 10;
79+
// WARNING: a default value decoded when value is missing
80+
optional int32 nullableOptionalUInt = 11;
81+
// WARNING: a default value decoded when value is missing
82+
optional int32 nullableOptionalWrappedUInt = 12;
7183
}
7284

7385
// serial name 'kotlinx.serialization.protobuf.schema.GenerationTest.ContextualHolder'

formats/protobuf/jvmTest/src/kotlinx/serialization/protobuf/schema/GenerationTest.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class GenerationTest {
6161
@ProtoNumber(5)
6262
val b: Int,
6363
@ProtoNumber(3)
64-
val c: Int
64+
val c: UInt,
6565
)
6666

6767
@Serializable
@@ -84,6 +84,10 @@ class GenerationTest {
8484
@Serializable
8585
data class OptionsClass(val i: Int)
8686

87+
@JvmInline
88+
@Serializable
89+
value class WrappedUInt(val i : UInt)
90+
8791
@Serializable
8892
class ListClass(
8993
val intList: List<Int>,
@@ -113,9 +117,17 @@ class GenerationTest {
113117
@Serializable
114118
data class OptionalClass(
115119
val requiredInt: Int,
120+
val requiredUInt: UInt,
121+
val requiredWrappedUInt: WrappedUInt,
116122
val optionalInt: Int = 5,
123+
val optionalUInt: UInt = 5U,
124+
val optionalWrappedUInt: WrappedUInt = WrappedUInt(5U),
117125
val nullableInt: Int?,
118-
val nullableOptionalInt: Int? = 10
126+
val nullableUInt: UInt?,
127+
val nullableWrappedUInt: WrappedUInt?,
128+
val nullableOptionalInt: Int? = 10,
129+
val nullableOptionalUInt: UInt? = 10U,
130+
val nullableOptionalWrappedUInt: WrappedUInt? = WrappedUInt(10U),
119131
)
120132

121133
@Serializable

0 commit comments

Comments
 (0)