Imagine the following base classes:
public abstract class BaseClass
{
public string BaseProperty { get; set; }
}
public class DataClass1 : BaseClass
{
public string Data1 { get; set; }
}
public class DataClass2 : BaseClass
{
public string Data2 { get; set; }
}
Then imagine an MVC API controller making use of the abstract type:
[ApiController]
public class DataController : ControllerBase
{
[HttpPost("[action]")]
public void SaveData(BaseClass dataClass)
{
// internal method that can handle the abstract type
}
}
The API generates an OpenApi schema with the following definition:
(...)
"/Data/SaveData": {
"post": {
"tags": [
"Data"
],
"requestBody": {
"content": {
"application/json-patch+json": {
"schema": {
"oneOf": [
{
"$ref": "#/components/schemas/DataClass1"
},
{
"$ref": "#/components/schemas/DataClass2"
}
(...)
The schema makes it clear that different classes can be used for the SaveData
method.
My client is generated using the following OpenApi
reference in the CSPROJ file:
<OpenApiReference Include="..BackendBackendService.json" CodeGenerator="NSwagCSharp" Link="OpenAPIsBackendService.json">
<Options>/GenerateClientInterfaces:true /AdditionalNamespaceUsages:Base.Entities,Base.Enums,Base.Entities.Settings,Base.Models /GenerateDtoTypes:false</Options>
</OpenApiReference>
However, the actual generated client only permits one class, seemingly the first one from the list (in my case it’s Data1
).
Are such types not permitted or not handled by the NSwagCSharp
code generator? Is it a case of my settings interfering with these abstract types or is there another trick to getting this to work?