Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NULL JSON column prevents entity deserialization #112

Open
tunger opened this issue Jul 13, 2020 · 1 comment
Open

NULL JSON column prevents entity deserialization #112

tunger opened this issue Jul 13, 2020 · 1 comment

Comments

@tunger
Copy link
Contributor

tunger commented Jul 13, 2020

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.

@PaulStovell
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants