[SERVER-83931] IDL generates code that depends on field ordering with variant-of-structs Created: 06/Dec/23  Updated: 12/Dec/23

Status: Backlog
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Ryan Berryhill Assignee: Backlog - Service Architecture
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to SERVER-73138 Support variants of complex types in IDL Closed
is related to SERVER-84122 Document that IDL struct variants imp... Closed
Assigned Teams:
Service Arch
Operating System: ALL
Participants:

 Description   

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.


Generated at Thu Feb 08 06:53:34 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.