Search code examples
protocol-buffersflatbuffers

flatc (flatbuffer) compiler doesnot handle nested namespace in correct manner


I am providing following proto file to flatbuffer compiler to generate .fbs file.

File: test.proto

    message A {
      optional int32 afoo = 1;
      message B {
       optional int32 bfoo_ = 1;
      }
      optional B bfoo= 2;
   }


  message C {
    optional int32 abar = 1;
    message B {
      optional int32 bbar_ = 1;
    }
    optional B bbar = 2;
  }

After this i ran: flatc --proto test.proto, which will generate .fbs file

File: test.fbs // Generated from test.proto

        namespace ;

        table A {
          afoo:int;
          bfoo:_A.B;
        }

        namespace _A;

        table B {
          bfoo_:int;
        }

        namespace ;

        table C {
          abar:int;
          bbar:_C.B;
        }

        namespace _C;

        table B {
          bbar_:int;
        }

Point to note here is "B" has been pulled out to global namespace After that i ran flat -cpp test.proto, which eventually generate incorrect header file. Contains two definitions of B

        struct B FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table

Solution

  • This appears to be a bug in FlatBuffers, in the generated code they're all in the same namespace. Report a bug here: https://github.com/google/flatbuffers/issues

    Meanwhile, un-nest the .proto manually, and it should work.

    EDIT: This was recently fixed: https://github.com/google/flatbuffers/commit/20c0082ee5bfeeecaa443c001a89934e9448ffa4