Closed Steve-OH closed 5 months ago
Hi, many thanks for the bug report and the PR. I'll take a look today. I know that there are some users who want nullable instances when deserialization fails. I remember there's a DeserializationStrictness
flag, but I'll have to refresh my memory on what it does.
I don't think DeserializationStrictness
applies in this case. Here, the converter is being bypassed upstream of any Vogen code.
Thanks @Steve-OH - This is fixed in 4.0.11, which will be released in a couple of hours. There is a new flag in DeserializationStrictness
which disallows nulls. I kept it allowing nulls for backwards compatibility.
Describe the bug
IDE: Visual Studio 2022 Build target: .NET 8 Language: C# 12 Vogen: 4.0.8
Using
System.Text.Json
, if you useclass
orrecord
for your value object, Vogen will deserialize a JSONnull
without complaint. This won't happen if you usestruct
orrecord struct
. The reason for this is that the baseJsonConverter
class has abool
property,HandleNull
, that defaults totrue
forstruct
orrecord struct
, butfalse
forclass
orrecord
. WhenHandleNull
isfalse
, the converter is bypassed completely, allowing thenull
to pass through.The solution is to add the following line to the converter templates:
This will fix the problem for
class
andrecord
, and will have no effect onstruct
orrecord struct
.Steps to reproduce
Compile and run the following simple console program:
No exception will be thrown, and the value object
foo
will have a value ofnull
.Expected behaviour
Vogen should throw a
ValueObjectValidationException
with message "Cannot create a value object with null."NOTE: I've put together a pull request with the fix applied to the string converter template, and with one additional test to demonstrate the before and after behavior, but the fix needs to be applied to the templates for all of the pimitive types, and I think the issue may also affect the Newtonsoft converter, and I just don't have the time right now to try to fix everything. Also, my Visual Studio and ReSharper seem to be in cahoots to auto-reformat everything, even though I've tried to turn it off completely, so making the smallest change to a file causes a gazillion lines to change.
Anyway, if you'd like me to submit it as-is, I'd be happy to do so, but it's definitely WIP.