Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET core: NSwag vs. Swashbuckle

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.

like image 966
clemensoe Avatar asked Mar 10 '21 13:03

clemensoe


People also ask

What is the difference between swashbuckle and NSwag?

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.

What is NSwag in .NET core?

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.

What is the use of NSwag studio?

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.

Is used as an alternative to swashbuckle?

Introduce NSwag as an alternative to Swashbuckle when using Swagger · Issue #4258 · dotnet/AspNetCore.


1 Answers

This solved it.

services.AddSwaggerGen(options =>
{
    options.UseAllOfForInheritance();
like image 126
clemensoe Avatar answered Sep 20 '22 19:09

clemensoe