flatbuffers icon indicating copy to clipboard operation
flatbuffers copied to clipboard

[Kotlin] parameter name isn't the same as its usage in struct

Open serhii-solodilov opened this issue 1 year ago • 4 comments

There is an inconsistency in the parameter naming generated by FlatBuffers for the Second.kt file. Specifically:

The generated parameter in the createSecond function is named opFirst_final, but the usage inside the function refers to op_first_final. Might be related to struct, because table works fine.

Steps to Reproduce:

  1. Use the following schema in Best.fbs:
struct First {
    first: uint16;
}

struct Second {
    op_first: First;
}

table Third {
    or_second: [Second] (required);
}

root_type Third;
  1. Run the following FlatBuffers command: flatc -o . --kotlin Best.fbs

  2. Inspect the generated Kotlin code, particularly in Second.kt, and observe the following discrepancy:

class Second : Struct() {

    fun __init(_i: Int, _bb: ByteBuffer)  {
        __reset(_i, _bb)
    }
    fun __assign(_i: Int, _bb: ByteBuffer) : Second {
        __init(_i, _bb)
        return this
    }
    val opFirst : First? get() = opFirst(First())
    fun opFirst(obj: First) : First? = obj.__assign(bb_pos + 0, bb)
    companion object {
        fun createSecond(builder: FlatBufferBuilder, opFirst_final: UShort) : Int {
            builder.prep(2, 2)
            builder.prep(2, 2)
            builder.putShort(op_first_final.toShort())
            return builder.offset()
        }
    }
}

Expected Behavior: Generated and used parameters for op_first_final should be the same.

Actual Behavior: The parameter is declared as opFirst_final, but the function uses op_first_final, causing the issues.

serhii-solodilov avatar Oct 07 '24 21:10 serhii-solodilov

This issue is stale because it has been open 6 months with no activity. Please comment or label not-stale, or this will be closed in 14 days.

github-actions[bot] avatar Apr 08 '25 20:04 github-actions[bot]

not-stale

serhii-solodilov avatar Apr 09 '25 01:04 serhii-solodilov

This issue is stale because it has been open 6 months with no activity. Please comment or label not-stale, or this will be closed in 14 days.

github-actions[bot] avatar Oct 09 '25 20:10 github-actions[bot]

not-stale

serhii-solodilov avatar Oct 23 '25 19:10 serhii-solodilov

@souma987 would love your input here

jtdavis777 avatar Dec 14 '25 00:12 jtdavis777

The cause seems to be that:

the parameter uses namer_.Variable() https://github.com/google/flatbuffers/blob/c9a301e60181bf255692e5ca1ce56850ff0cb1a0/src/idl_gen_kotlin.cpp#L1474-L1475

while the body does not https://github.com/google/flatbuffers/blob/c9a301e60181bf255692e5ca1ce56850ff0cb1a0/src/idl_gen_kotlin.cpp#L482-L483

I suggest changing the body, as fields are referenced by their camelCase name in other parts of the generated code (e.g., getters).

souma987 avatar Dec 14 '25 13:12 souma987

Sounds like an easy fix -- want to PR it?

jtdavis777 avatar Dec 14 '25 14:12 jtdavis777