diff --git a/src/FsCodec.SystemTextJson/FsCodec.SystemTextJson.fsproj b/src/FsCodec.SystemTextJson/FsCodec.SystemTextJson.fsproj index e221a36..9af7a97 100644 --- a/src/FsCodec.SystemTextJson/FsCodec.SystemTextJson.fsproj +++ b/src/FsCodec.SystemTextJson/FsCodec.SystemTextJson.fsproj @@ -10,6 +10,7 @@ + @@ -22,7 +23,7 @@ - + diff --git a/src/FsCodec.SystemTextJson/Options.fs b/src/FsCodec.SystemTextJson/Options.fs index 288de0c..37e335d 100755 --- a/src/FsCodec.SystemTextJson/Options.fs +++ b/src/FsCodec.SystemTextJson/Options.fs @@ -60,7 +60,10 @@ type Options private () = [] ?autoUnionToJsonObject: bool, // Apply RejectNullStringConverter in order to have serialization throw on null strings. // Use string option to represent strings that can potentially be null. - [] ?rejectNullStrings: bool) = + [] ?rejectNullStrings: bool, + // Apply RejectNullConverter in order to have serialization throw on null on null or missing list or Set values. + // Wrap the type in option to represent values that can potentially be null or missing + [] ?rejectNull: bool) = let autoTypeSafeEnumToJsonString = defaultArg autoTypeSafeEnumToJsonString false let autoUnionToJsonObject = defaultArg autoUnionToJsonObject false let rejectNullStrings = defaultArg rejectNullStrings false @@ -68,6 +71,7 @@ type Options private () = Options.CreateDefault( converters = [| if rejectNullStrings then RejectNullStringConverter() + if defaultArg rejectNull false then RejectNullConverterFactory() if autoTypeSafeEnumToJsonString || autoUnionToJsonObject then UnionOrTypeSafeEnumConverterFactory(typeSafeEnum = autoTypeSafeEnumToJsonString, union = autoUnionToJsonObject) if converters <> null then yield! converters diff --git a/src/FsCodec.SystemTextJson/RejectNullConverter.fs b/src/FsCodec.SystemTextJson/RejectNullConverter.fs new file mode 100644 index 0000000..7fd63cd --- /dev/null +++ b/src/FsCodec.SystemTextJson/RejectNullConverter.fs @@ -0,0 +1,36 @@ +namespace FsCodec.SystemTextJson + +open System +open System.Text.Json +open System.Text.Json.Serialization + +type RejectNullConverter<'T>() = + inherit System.Text.Json.Serialization.JsonConverter<'T>() + + let defaultConverter = JsonSerializerOptions.Default.GetConverter(typeof<'T>) :?> JsonConverter<'T> + let msg () = sprintf "Expected value, got null. When rejectNull is true you must explicitly wrap optional %s values in an 'option'" typeof<'T>.Name + + override _.HandleNull = true + + override _.Read(reader, typeToConvert, options) = + if reader.TokenType = JsonTokenType.Null then msg () |> nullArg else + // PROBLEM: Fails with NRE when RejectNullConverter delegates to Default list Converter + // System.NullReferenceException + // at System.Text.Json.Serialization.JsonCollectionConverter`2.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, TCollection& value) + // https://github.com/dotnet/runtime/issues/50205 via https://github.com/jet/FsCodec/pull/112#issuecomment-1907633798 + defaultConverter.Read(&reader, typeToConvert, options) + // Pretty sure the above is the correct approach (and this unsurprisingly loops, blowing the stack) + // JsonSerializer.Deserialize(&reader, typeToConvert, options) :?> 'T + + override _.Write(writer, value, options) = + if value |> box |> isNull then msg () |> nullArg + defaultConverter.Write(writer, value, options) + // JsonSerializer.Serialize<'T>(writer, value, options) + +type RejectNullConverterFactory(predicate) = + inherit JsonConverterFactory() + static let isListOrSet (t: Type) = t.IsGenericType && let g = t.GetGenericTypeDefinition() in g = typedefof> || g = typedefof> + new() = RejectNullConverterFactory(isListOrSet) + + override _.CanConvert(t: Type) = predicate t + override _.CreateConverter(t, _options) = typedefof>.MakeGenericType(t).GetConstructors().[0].Invoke[||] :?> _ diff --git a/tests/FsCodec.SystemTextJson.Tests/SerdesTests.fs b/tests/FsCodec.SystemTextJson.Tests/SerdesTests.fs index 017a153..64e4cfd 100644 --- a/tests/FsCodec.SystemTextJson.Tests/SerdesTests.fs +++ b/tests/FsCodec.SystemTextJson.Tests/SerdesTests.fs @@ -2,6 +2,7 @@ module FsCodec.SystemTextJson.Tests.SerdesTests open System open System.Collections.Generic +open System.Text.Json.Serialization.Metadata open FsCodec.SystemTextJson open Swensen.Unquote open Xunit @@ -79,6 +80,42 @@ let [] ``RejectNullStringConverter rejects null strings`` () = let value = { c = 1; d = null } raises <@ serdes.Serialize value @> +type WithList = { x: int; y: list; z: Set } + +let [] ``RejectNullConverter rejects null lists and Sets`` () = +#if false // requires WithList to be CLIMutable, which would be a big imposition + let tir = + DefaultJsonTypeInfoResolver() + .WithAddedModifier(fun x -> + // if x.Kind <> JsonTypeInfoKind.Object then + for p in x.Properties do + let pt = p.PropertyType + if pt.IsGenericType && (let d = pt.GetGenericTypeDefinition() in d = typedefof> || d = typedefof>) then + p.IsRequired <- true) + let serdes = Options.Create(TypeInfoResolver = tir) |> Serdes + raises <@ serdes.Deserialize """{"x":0}""" @> +#else + let serdes = Options.Create(rejectNull = true) |> Serdes + + // PROBLEM: Fails with NRE when RejectNullConverter delegates to Default list Converter + // let res = serdes.Deserialize """{"x":0,"y":[1]}""" + // test <@ [1] = res.y @> + + // PROBLEM: Doesn't raise as Converter not called + raises <@ serdes.Deserialize """{"x":0}""" @> + + // PROBLEM: doesnt raise - there doesn't seem to be a way to intercept explicitly passed nulls + // raises <@ serdes.Deserialize """{"x":0,"y":null}""" @> +#endif + +#if false // I guess TypeShape is doing a reasonable thing not propagating + // PROBLEM: TypeShape.Generic.exists does not call the predicate if the list or set is `null` + let res = serdes.Deserialize """{"x":0}""" + let hasNullList = TypeShape.Generic.exists (fun (x: int list) -> obj.ReferenceEquals(x, null)) + let hasNullSet = TypeShape.Generic.exists (fun (x: Set) -> obj.ReferenceEquals(x, null)) + test <@ hasNullList res && hasNullSet res @> +#endif + let [] ``RejectNullStringConverter serializes strings correctly`` () = let serdes = Serdes(Options.Create(rejectNullStrings = true)) let value = { c = 1; d = "some string" }