[SERVER-80330] IDL generate noncompilable c++ code in case of a chained optional variant field Created: 23/Aug/23  Updated: 29/Oct/23  Resolved: 14/Sep/23

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: 7.2.0-rc0

Type: Bug Priority: Major - P3
Reporter: Enrico Golfieri Assignee: Pierlauro Sciarelli
Resolution: Fixed Votes: 0
Labels: car-71-backport-declined
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Assigned Teams:
Service Arch
Backwards Compatibility: Fully Compatible
Operating System: ALL
Sprint: Sharding EMEA 2023-09-18
Participants:

 Description   

Currently experiencing the idl generator to generate c++ code that doesn't compile in case of a chained Struct that contains an optional variant field between a generic type and a customised struct

Reproducible idl example

 

global:
    cpp_namespace: "mongo"
imports:
    - "mongo/db/basic_types.idl"
    
structs:
    Point:
        description: "A struct which will be part of the variant type"
        strict: true
        fields:
            x:
                type: int
                stability: stable
            y:
                type: int
                stability: stable
 
    Data:
        description: "Just some data"
        strict: false
        fields:
          broken_field:
            description: "The field that will cause c++ generated code not to compile"
            type:
                variant: [bool, Point]
            optional: true
            stability: stable
 
commands:
    mycommand:
        description: "A command"
        command_name: mycommand
        namespace: concatenate_with_db
        cpp_name: MyCommand
        api_version: ""
        strict: true
        reply_type: OkReply
        inline_chained_structs: true
        chained_structs:
          Data: Data
 

The generated code will cause this snippet not to compile

 

switch (variantType) {
                    case Bool:
                    {
                        _data.setBroken_field(boost::optional<stdx::variant<bool, mongo::Point>>(element.boolean()));
                        break;
                    }
                    case Object:
                        _data.setBroken_field(mongo::Point::parse(ctxt, element.Obj())); // <------------- this does not compile because it's missing a cast
                        break;
                    default:
                        ctxt.throwBadType(element, {BSONType::Bool});
                        break;
                    }
                    continue;

 



 Comments   
Comment by Githook User [ 13/Sep/23 ]

Author:

{'name': 'Pierlauro Sciarelli', 'email': 'pierlauro.sciarelli@mongodb.com', 'username': 'pierlauro'}

Message: SERVER-80330 IDL generator must handle optional IDL-generated variant types
Branch: master
https://github.com/mongodb/mongo/commit/53e1c5b38c0bba3f65f2b34d3a98451399e668a2

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