google / flatbuffers

FlatBuffers: Memory Efficient Serialization Library
https://flatbuffers.dev/
Apache License 2.0
22.52k stars 3.19k forks source link

Default value of 0 for field is not part of [optional] enum (java) #8247

Open djchapm opened 4 months ago

djchapm commented 4 months ago

flatc version 23.5.26

Similar to #5108 but For java. flatc -j test.fbs on contents:

enum InvestorType: int
{
    customer = 67,
    foreign = 70
}

table OrderSet
{
    has_order_id:bool;
    investor_type:InvestorType;
}

Error: test.fbs:10: 28: error: default value of 0 for field investor_type is not part of enum InvestorType.

According to #5108 there should be an implicit 'NONE' field. Also since the field is optional, I'd think hasInvestorType would return false and investorType would be null by default instead of having to set the default.

We can't specify a value '0' for various reasons, we don't want the possibility. It does work if I declare investor_type:InvestorType = null;, but isn't this what optional means? We don't have to provide =null; for any other types as far as I know, correct me if I'm wrong.

Thanks!