ASP.NET core: NSwag vs. Swashbuckle
Asked Answered
S

1

8

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.

Shortfall answered 10/3, 2021 at 13:19 Comment(0)
S
8

This solved it.

services.AddSwaggerGen(options =>
{
    options.UseAllOfForInheritance();
Shortfall answered 10/3, 2021 at 16:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.