Closed
Description
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
When serializing descendant types, for methods that take a single (not a collection) parameter of the base type as an argument, the discriminator field is not added to JSON.
JSON for collection argument:
{
"type": 1,
"target": "WeatherForecast",
"arguments": [
[
{
"$type": "extended",
"temperatureC": -15,
"summary": "Freezing",
"date": "2023-11-24"
},
{
"$type": "summary",
"summary": "Mild",
"date": "2022-11-20"
}
]
]
}
JSON for single argument:
{
"type": 1,
"target": "SummaryWeatherForecast",
"arguments": [
{
"summary": "Mild",
"date": "2023-11-24"
}
]
}
{
"type": 1,
"target": "ExtendedWeatherForecast",
"arguments": [
{
"temperatureC": -15,
"summary": "Freezing",
"date": "2023-11-24"
}
]
}
This is similar to the problem described previously in #44852
Expected Behavior
I expect SignalR JSON to contain a $type field similar to responses from HTTP endpoints:
{
"$type": "summary",
"summary": "Mild",
"date": "2023-11-24"
}
{
"$type": "extended",
"temperatureC": -15,
"summary": "Freezing",
"date": "2023-11-24"
}
Steps To Reproduce
Repository with code showing the problem.
I used the following classes to show the problem:
[JsonPolymorphic]
[JsonDerivedType(typeof(SummaryWeatherForecast), "summary")]
[JsonDerivedType(typeof(ExtendedWeatherForecast), "extended")]
public class WeatherForecast
{
public DateOnly Date { get; set; }
}
public class SummaryWeatherForecast(string summary): WeatherForecast
{
public string Summary { get; } = summary;
}
public class ExtendedWeatherForecast(string summary, int temperatureC): SummaryWeatherForecast(summary)
{
public int TemperatureC { get; } = temperatureC;
}
public class WeatherForecastHub: Hub<IWeatherForecastHub>;
[SuppressMessage("ReSharper", "AsyncApostle.AsyncMethodNamingHighlighting")]
public interface IWeatherForecastHub
{
Task SummaryWeatherForecast(WeatherForecast summaryWeatherForecast);
Task ExtendedWeatherForecast(WeatherForecast extendedWeatherForecast);
Task WeatherForecast(WeatherForecast[] extendedWeatherForecast);
}
[ApiController]
[Route("[controller]")]
public class WeatherForecastController(IHubContext<WeatherForecastHub, IWeatherForecastHub> hubContext): ControllerBase
{
[HttpGet]
public async Task<IEnumerable<WeatherForecast>> Get()
{
var forecast1 = new ExtendedWeatherForecast("Freezing", -15)
{
Date = DateOnly.FromDateTime(DateTime.UtcNow)
};
var forecast2 = new SummaryWeatherForecast("Mild") { Date = DateOnly.Parse("2022-11-20") };
var result = new WeatherForecast[] { forecast1, forecast2 };
await hubContext.Clients.All.WeatherForecast(result);
return result;
}
[HttpGet("Summary")]
public async Task<WeatherForecast> GetSummary()
{
var forecast2 = new SummaryWeatherForecast("Mild")
{
Date = DateOnly.FromDateTime(DateTime.UtcNow)
};
await hubContext.Clients.All.SummaryWeatherForecast(forecast2);
return forecast2;
}
[HttpGet("Extended")]
public async Task<WeatherForecast> GetExtended()
{
var forecast1 = new ExtendedWeatherForecast("Freezing", -15)
{
Date = DateOnly.FromDateTime(DateTime.UtcNow)
};
await hubContext.Clients.All.ExtendedWeatherForecast(forecast1);
return forecast1;
}
}
Exceptions (if any)
No response
.NET Version
8.0.100
Anything else?
No response