Skip to content

Commit 4cbc82c

Browse files
committed
Fix #100: Allow unionTagName to be part of the record type definition when deserializing union of records
1 parent a915456 commit 4cbc82c

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

src/FSharp.SystemTextJson/Union.fs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,8 @@ type JsonUnionConverter<'T>
359359
let readInternalTag (reader: byref<Utf8JsonReader>) (options: JsonSerializerOptions) =
360360
if namedFields then
361361
expectAlreadyRead JsonTokenType.StartObject "object" &reader ty
362-
let struct (case, usedDocument) = getCase &reader
362+
let mutable snapshot = reader
363+
let struct (case, _usedDocument) = getCase &snapshot
363364
readFieldsAsRestOfObject &reader case false options
364365
else
365366
expectAlreadyRead JsonTokenType.StartArray "array" &reader ty

tests/FSharp.SystemTextJson.Tests/Test.Regression.fs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,14 @@ let ``regression #87`` () =
2525
Assert.Equal("A.B was expected to be of type String, but was null.", ex1.Message)
2626
let ex2 = Assert.Throws<JsonException>(fun () -> JsonSerializer.Deserialize<A>("""{ "A": null, "B": "a" }""", options) |> ignore)
2727
Assert.Equal("A.A was expected to be of type Int32, but was null.", ex2.Message)
28+
29+
type R100Rec1 = { Case: string }
30+
type R100Rec2 = { Case: string; other: int }
31+
type R100U = Rec1 of R100Rec1 | Rec2 of R100Rec2
32+
33+
[<Fact>]
34+
let ``regression #100`` () =
35+
let options = JsonSerializerOptions()
36+
options.Converters.Add(JsonFSharpConverter(JsonUnionEncoding.InternalTag ||| JsonUnionEncoding.UnwrapRecordCases))
37+
Assert.Equal(Rec1 { Case = "Rec1" }, JsonSerializer.Deserialize("""{"Case":"Rec1"}""", options))
38+
Assert.Equal(Rec2 { Case = "Rec2"; other = 42 }, JsonSerializer.Deserialize("""{"Case":"Rec2","other":42}""", options))

0 commit comments

Comments
 (0)