Fix nullability of generated Kotlin ByteBuffer accessors (#8844)

* fixes #8691
This commit is contained in:
souma987
2025-12-15 08:56:40 +09:00
committed by GitHub
parent 7bfaabc358
commit 60910fb7f5
7 changed files with 73 additions and 39 deletions

View File

@@ -80,6 +80,7 @@ class KotlinTest {
TestSharedStringPool()
TestScalarOptional()
TestDictionaryLookup()
TestNullFields()
println("FlatBuffers test: completed successfully")
}
@@ -142,7 +143,7 @@ class KotlinTest {
assert(invsum == 10u)
// Alternative way of accessing a vector:
val ibb = monster.inventoryAsByteBuffer
val ibb = monster.inventoryAsByteBuffer!!
invsum = 0u
while (ibb.position() < ibb.limit()) invsum += ibb.get().toUInt()
assert(invsum == 10u)
@@ -615,5 +616,37 @@ class KotlinTest {
assert(scalarStuff.maybeEnum == OptionalByte.Two)
assert(scalarStuff.defaultEnum == OptionalByte.Two)
}
fun TestNullFields() {
val fbb = FlatBufferBuilder(1)
val nameOffset = fbb.createString("name")
Monster.startMonster(fbb)
Monster.addName(fbb, nameOffset)
Monster.finishMonsterBuffer(fbb, Monster.endMonster(fbb))
val monsterBb = fbb.dataBuffer()
val monster = Monster.getRootAsMonster(monsterBb.duplicate())
assert(monster.pos == null) // struct
assert(monster.enemy == null) // table
assert(monster.test(Monster()) == null) // union
assert(monster.testType == Any_.NONE)
assert(monster.inventoryLength == 0) // vector of scalars
assert(monster.inventoryAsByteBuffer == null)
assert(monster.inventoryInByteBuffer(monsterBb) == null)
assert(monster.testarrayofsortedstructLength == 0) // vector of structs
assert(monster.testarrayofstringLength == 0) // vector of strings
assert(monster.testarrayoftablesLength == 0) // vector of tables
val fbb2 = FlatBufferBuilder(1)
val offset = Stat.createStat(fbb2, 0, 10, 10.toUShort())
fbb2.finish(offset)
val statBb = fbb2.dataBuffer()
val stat = Stat.getRootAsStat(statBb.duplicate())
assert(stat.id == null) // string
assert(stat.idAsByteBuffer == null)
assert(stat.idInByteBuffer(statBb) == null)
}
}
}