Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-83931

IDL generates code that depends on field ordering with variant-of-structs

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None
    • Service Arch
    • ALL

      If you create an IDL struct that has a field with type variant-of-structs, IDL will refuse to compile it with an error ID0081 unless each struct in the variant has a unique first field. This is to avoid ambiguity when parsing the subdocument.

      However, in the case where IDL does compile your struct, it generates code that relies on the fields in the variant subdocument being ordered. For example, consider this IDL:

      structs:
          Foo:
              description: "foo"
              fields:
                  fieldFoo:
                      type: int
                  fieldCommon:
                      type: int
      
          Bar:
              description: "bar"
              fields:
                  fieldBar:
                      type: int
                  fieldCommon:
                      type: int
      
          FooOrBar:
              description: "foo or bar"
              fields:
                  fieldFooOrBar:
                      type:
                          variant: [Foo, Bar]
      

      It will compile, and will successfully parse a BSON document like:

      {fieldFooOrBar: {fieldBar: 1, fieldCommon: 2}}
      

      but not a similar document with the fields reordered:

      {fieldFooOrBar: {fieldCommon: 2, fieldBar: 1}}
      

      This is a footgun since the second document is a valid document of type Bar. One possible fix is to add an ordered field to IDL structs and only support variant-of-structs where all of the structs have ordered: true. Another possibility is to remove the variant-of-structs feature altogether and use a different approach where it's currently used.

            Assignee:
            backlog-server-servicearch [DO NOT USE] Backlog - Service Architecture
            Reporter:
            ryan.berryhill@mongodb.com Ryan Berryhill
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: