EJSON - fromEJSON does not respect fields that have default value.

This issue belongs to an archived project. You can view it, but you can't modify it. Learn more

XMLWordPrintableJSON

    • Type: Improvement
    • Resolution: Fixed
    • Priority: Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None

      What happened?

      Because MongoDB/Atlas Search does not transfer information about fields that have their default values (nullable). The fromEJSON throws InvalidEJson because the fields are not set with NULL.

      Repro steps

      Create a model with nullable fields like:

      Unable to find source-code formatter for language: dart. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
        class _AddressModel {
          late String? type;
          late String street;
          late String? suburb;
          late String city;
          late String? state;
          late String? postalCode;
          late String countryCode;
          late _LocationModel? location;
        }
      

      Try to convert Map (JSON) to Object
      Note: type, suburb, state and postalCode have their default value (null) so nothing is returned from Atlas Search

      Unable to find source-code formatter for language: dart. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      AddressModel testWithCrash = AddressModel.fromEJson<AddressModel>(
        {
          countryCode: NLD, 
          street: Malieveld, 
          city: Den Haag, 
          location: {
           coord: [{$numberDouble: 52.085413}, {$numberDouble: 4.317371}],
            type: Point}
        }
      )
      
        static AddressModel _fromEJson(EJsonValue ejson) {
          return switch (ejson) {
            {
              'type': EJsonValue type, <--------- Throws because the json above does not contain that field.
              'street': EJsonValue street,
              'suburb': EJsonValue suburb,
              'city': EJsonValue city,
              'state': EJsonValue state,
              'postalCode': EJsonValue postalCode,
              'countryCode': EJsonValue countryCode,
              'location': EJsonValue location,
            } =>
              AddressModel(
                type: fromEJson(type),
                street: fromEJson(street),
                suburb: fromEJson(suburb),
                city: fromEJson(city),
                state: fromEJson(state),
                postalCode: fromEJson(postalCode),
                countryCode: fromEJson(countryCode),
                location: fromEJson(location),
              ),
            _ => raiseInvalidEJson(ejson),
          };
        }
      

      Version

      Flutter 3.19.3 • channel stable - Dart SDK version: 3.3.1 (stable)

      What Atlas Services are you using?

      Both Atlas Device Sync and Atlas App Services

      What type of application is this?

      Flutter Application

      Client OS and version

      realm: ^3.0.0

      Code snippets

      No response

      Stacktrace of the exception/crash you're getting

      Unable to find source-code formatter for language: shell. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      Exception has occurred.
      InvalidEJson (Invalid EJson for AddressModel: {countryCode: NLD, street: Malieveld, city: Den Haag, location: {coord: [{$numberDouble: 52.085413}, {$numberDouble: 4.317371}], type: Point}})
      

      Relevant log output

      No response

            Assignee:
            Kasper Nielsen (Inactive)
            Reporter:
            Unito Sync Bot
            Archiver:
            Marc Greenfield

              Created:
              Updated:
              Archived: