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

UnionConverter: Accept custom discriminator without catchAllCase #51

Merged
merged 3 commits into from
May 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ The `Unreleased` section name is replaced by the expected version of next releas
### Removed
### Fixed

- `UnionConverter`: Support overriding discriminator without needing to nominate a `catchAllCase` [#51](https://github.com/jet/FsCodec/pull/51)

<a name="2.1.0"></a>
## [2.1.0] - 2020-05-10

Expand Down
5 changes: 3 additions & 2 deletions src/FsCodec.NewtonsoftJson/UnionConverter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ module private Union =
type UnionConverter private (discriminator : string, ?catchAllCase) =
inherit JsonConverter()

new() = UnionConverter("case")
new(discriminator: string, catchAllCase: string) = UnionConverter(discriminator, ?catchAllCase = match catchAllCase with null -> None | x -> Some x)
new() = UnionConverter("case", ?catchAllCase=None)
new(discriminator: string) = UnionConverter(discriminator, ?catchAllCase=None)
new(discriminator: string, catchAllCase: string) = UnionConverter(discriminator, ?catchAllCase=match catchAllCase with null -> None | x -> Some x)

override __.CanConvert (t : Type) = Union.isUnion t

Expand Down
20 changes: 20 additions & 0 deletions tests/FsCodec.NewtonsoftJson.Tests/UnionConverterTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,26 @@ let ``Implementation ensures no internal errors escape (which would render a Web
test <@ (CaseD "hi") = d @>
test <@ false = gotError @>

module CustomDiscriminator =

[<JsonConverter(typeof<UnionConverter>, "esac")>]
type DuWithCustomDiscriminator =
| Known
| Catchall

[<Fact>]
let ``UnionConverter handles custom discriminator`` () =
let json = """{"esac":"Known"}"""
test <@ DuWithCustomDiscriminator.Known = JsonConvert.DeserializeObject<_> json @>

[<Fact>]
let ``UnionConverter can complain about missing case with custom discriminator without catchall`` () =
let aJson = """{"esac":"CaseUnknown"}"""
let act () = JsonConvert.DeserializeObject<DuWithCustomDiscriminator>(aJson, settings)

fun (e : System.InvalidOperationException) -> <@ -1 <> e.Message.IndexOf "No case defined for 'CaseUnknown', and no catchAllCase nominated" @>
|> raisesWith <@ act() @>

module ``Unmatched case handling`` =

[<Fact>]
Expand Down