ardalis / ardalis/Result

Type discriminator is lost when serializing a Result<T> using [TranslateResultToActionResult]

Open
#204 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
1k
Forks
127
PR merge metrics
No merged PRs in 30d

Description

When serializing a derived type as its base type, System.Text.Json will use an extra type discriminator field to communicate the actual type of the serialized data. Given a base class `Base` and a derived type `Derived`, I have observed the following behavior:

1. A controller endpoint returning `Base` will correctly include the type discriminator.
2. A controller endpoint returning `ActionResult` will include the type discriminator.
3. A controller endpoint returning `Result` will include the properties of the derived type but it will NOT include the type discriminator.

Minimal reproducible example: https://github.com/uflowie/ResultLostTypeDiscriminator

Contributor guide

Open the contributing guide

Research direction

Start with the [TranslateResultToActionResult] path and compare serialization for Result with the documented Base and ActionResult cases, using the linked minimal reproducible example. Done means the Result response preserves the System.Text.Json type discriminator while retaining the derived-type properties.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.