[CSHARP-2728] Logged Command property casing not serializer compatible Created: 04/Sep/19  Updated: 27/Oct/23  Resolved: 18/Sep/19

Status: Closed
Project: C# Driver
Component/s: Serialization
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Richard Collette Assignee: Dmitry Lukyanov (Inactive)
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

I am logging commands using

            // The following will map .NET Pascal Cased properties to bson Camel Cased properties.
            var conventionPack = new ConventionPack
            {
                new CamelCaseElementNameConvention(), new IgnoreExtraElementsConvention(true)
            };
            ConventionRegistry.Register("camelCase", conventionPack, t => true);
            
            clientSettings.ClusterConfigurator = cb =>
            {
                // This makes the output strictly JSON, such that we can format the logged commands.
                // The output can still be used in a shell (ex NoSqlBooster)
                // Without this, the output can contain JS code, such a regular expression literals
                // which would fail to format in a JSON formatter.
                var jsonWriterSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict };
                cb.Subscribe<CommandStartedEvent>(e =>
                {
                    if (_logger.IsEnabled(LogLevel.Debug))
                    {
                        _logger.LogDebug($"MongoDb - {e.CommandName} - {e.Command.ToJson(jsonWriterSettings)}");
                    }
                });
            };

This method

// ...
.Project(augmentedProductType => new ProductTypeSearchResult
{
  Description = augmentedProductType.ExternalProductTypeDescription
})

Results in a logged statement like (theLoggedCommand)

 private const string ProjectionStage = @"
{
   "$project":{
      "Description":"$externalProductTypeDesc"
   }
}

But if I then replace the Project method call with the logged command using

 private static readonly PipelineStageDefinition<SearchAugmentedProductType, ProductTypeSearchResult>
            ProjectionPipelineStageDefinition = "theLoggedCommand";
.AppendStage(ProjectionPipelineStageDefinition);

Default serialization mapping does not work. I actually have to change the command to have lower case properties.

 private const string ProjectionStage = @"
{
   "$project":{
      "description":"$externalProductTypeDesc"
   }
}

I would not expect to have to do that. I would expect that if I am using the logged command, the serialization mapping should continue to work. It took quite a while trying to figure out why I was having to add BsonElement annotations after switching to using AppendStage with the command that was logged.



 Comments   
Comment by Dmitry Lukyanov (Inactive) [ 18/Sep/19 ]

Hello rcollette@yahoo.com,
Convention packs are designed ONLY to be used for creating BsonClassMaps for POCO. So, your second way is not affected by these settings at all.

Generated at Wed Feb 07 21:43:23 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.