[SERVER-41272] IDL should differentiate when an optional field was not specified, or specified as 'null' Created: 22/May/19  Updated: 06/Dec/22

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

Type: Improvement Priority: Major - P3
Reporter: Anton Korshunov Assignee: Backlog - Service Architecture
Resolution: Unresolved Votes: 0
Labels: sa-remove-fv-backlog-22
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Assigned Teams:
Service Arch
Sprint: Security 2019-06-03
Participants:

 Description   

Currently there is no way to tell if an optional field wasn't specified in the document, or was set to 'null'. E.g., if 'foo' is an optional field, then 'if (doc.getFoo())' will be false for the following two documents:

{foo: null} 
{}

However, semantically, not specifying a field, and setting it to 'null' may have different meanings. So, the IDL should provide a mechanism to check whether an optional field didn't exist in the document, or its value is 'null'.

 



 Comments   
Comment by Anton Korshunov [ 04/Jun/19 ]

Thanks for the updated table. The BSON Undefined type seems to be deprecated, so I'm not sure we need to support it. Otherwise, it looks good. Thanks for looking into it!

Comment by Mark Benvenuto [ 31/May/19 ]

Undefined is BSON undefined in this case. Here is the updated table to clarify the intention.

Depending on the field value, here is the expected result:

Value Return
<absent> false
BSON null true
BSON undefined true
other bson types true
Comment by Anton Korshunov [ 31/May/19 ]

mark.benvenuto This looks great! One question though - what it this 'undefined' value? I was thinking of undefined as being absent. Is it BSON's Undefined type? I also don't have a strong preference about the name for a new method, so up to you.

Comment by Mark Benvenuto [ 31/May/19 ]

If there was a new method for each optional field that return a boolean as defined below, would this meet your needs.

Behavior:
For each field, it would generate a new method:
isOptional<FIELD NANE>Present() (not a great name, subject to change)

Depending on the field value, here is the expected result:

Value Return
<absent> false
null true
undefined true
other types true

Sample
Example:

structs:
  testStruct:
    fields
      testField:
        optional:true
        type: string

Example Generated Code:
isOptionalTestFieldPresent()

Comment by Anton Korshunov [ 30/May/19 ]

mark.benvenuto Right, the IDL may continue treating them as non-existent for backward compatibility. What I'm asking for is to provide a mechanism to check if the field was null or missed. Like, generate an extra method for each optional field which the user may call if he needs to differentiate between null and undefined fields.

Comment by Mark Benvenuto [ 29/May/19 ]

IDL treats null and undefined as non-existent fields in the input document. This is part of the original design. See https://github.com/mongodb/mongo/blob/cb45824b458c1b3714a379c5c658e1e89238c03d/src/mongo/idl/idl_parser.cpp#L71-L74.

Generated at Thu Feb 08 04:57:17 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.