we're currently using Swashbuckle.AspNetCore for API documentation purpose, but when it comes to generation of client-side models (Typescript) it seems there is a major drawback of it.
As a sample, I enhanced the known ASP.NET default project (WeatherForecast) with a base class
public class InfoEntryBase
{
public string Name { get; set; }
public string Description { get; set; }
}
public class WeatherForecast : InfoEntryBase
{
public DateTime Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string Summary { get; set; }
}
It then exposes WeatherForecast as
WeatherForecast{
name string
nullable: true
description string
nullable: true
date string($date-time)
temperatureC integer($int32)
temperatureF integer($int32)
readOnly: true
summary string
nullable: true
}
and the inheritance gets lost. This will make it impossible to auto-generate client-side models from the server-side code as we naturally like to port the inheritance to the Typescript code.
On investigating NSwag.AspNetCore I discovered it to take care about the inheritance. It exposes:
WeatherForecast{
name string
description string
date* string($date-time)
temperatureC* integer($int32)
temperatureF* integer($int32)
summary string
}
InfoEntryBase{
name string
description string
}
Did I overlook something regarding Swashbuckle or is there no alternative to switch from it to NSwag?
You can review the code on https://github.com/ClemensOesterle/NSwagSpike/tree/swashbuckle whereas the NSwag implementation resides in the master branch.
Swashbuckle+NSwag Does Not SupportPoint to client side class Point . Open API and NSwag supports inheritance, however Swashbuckle's support for inheritance is poor, as of Swashbuckle. AspNetCore 5.0. Open API and NSwag provide limited supports for enum , however, Swashbuckle supports even less.
NSwag is a Swagger/OpenAPI 2.0 and 3.0 toolchain for . NET, . NET Core, Web API, ASP.NET Core, TypeScript (jQuery, AngularJS, Angular 2+, Aurelia, KnockoutJS and more) and other platforms, written in C#. The OpenAPI/Swagger specification uses JSON and JSON Schema to describe a RESTful web API.
The NSwag. MSBuild NuGet package. The Unchase OpenAPI (Swagger) Connected Service: A Visual Studio Connected Service for generating API client code in C# or TypeScript. Also generates C# controllers for OpenAPI services with NSwag.
Introduce NSwag as an alternative to Swashbuckle when using Swagger · Issue #4258 · dotnet/AspNetCore.
This solved it.
services.AddSwaggerGen(options =>
{
options.UseAllOfForInheritance();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With