Nevermore icon indicating copy to clipboard operation
Nevermore copied to clipboard

NULL JSON column prevents entity deserialization

Open tunger opened this issue 5 years ago • 1 comments

Use case: migrate existing table/entity to Nevermore.

When adding a JSON column as NULLable, and it contains NULL, no properties are deserialized, not even column-bound properties.

    public class ReportState
    {
        public Guid CorrelationId { get; set; }
        public DateTime LastUpdatedTimestamp { get; set; }
    }

    public class ReportStateMap : DocumentMap<ReportState>
    {
        public ReportStateMap()
        {
            TableName = "ReportState";
            Id(x => x.CorrelationId);
            Column(x => x.LastUpdatedTimestamp);
        }
    }

Query

            var reportStates = reportTransaction.Query<ReportState>()
                .Where("CorrelationId IN @reportGuids")
                .OrderBy(x => x.CorrelationId)
                .Parameter("reportGuids", reportGuids)
                .ToList()
                .ToDictionary(x => x.CorrelationId);

Expected Query should return list of ReportState correctly populated with data.

Actual Empty list is returned.

Workaround Update JSON column with '{}'

It should either throw if JSON is nullable, or handle NULL values as empty objects.

tunger avatar Jul 13 '20 10:07 tunger

I agree with your 'Expected' assertion. However, what if the type has no default constructor?

How's this for a proposal:

  • If the type has a parameterless constructor, and the JSON is empty/null, new() it up
  • If the type doesn't have a parameterless constructor, and the JSON is null/empty, throw

Let me know what you think.

I think it will just require a change to DocumentReaderContext.cs so that when told to deserialize, instead of returning null/ignoring, it calls the constructor or throws. Alternatively, we might need to change the generated code so that it does the null check and calls the constructor that way.

PaulStovell avatar Aug 20 '20 10:08 PaulStovell