0

I create a simple .net5 web api. Then I get the great Rico Suter's Nswag.aspnetcore and tweak it for get my application to create the swagger.json file

In the scaffolded controller I write another PUT action that TAKES AN OBJECT parameter (the simplest I can)

[HttpPost]
public ActionResult simple(WeatherForecast w)
{
    return NoContent();
}

Everything works nice out of the box. I can find in http://localhost:63630/swagger/v1/swagger.json the file.

Then, I'l use NSwagStudio to get a C# client proxy with interfaces.

With OpenAPI Specification I'll get a pretty c# interface:

[System.CodeDom.Compiler.GeneratedCode("NSwag", "13.10.2.0 (NJsonSchema v10.3.4.0 (Newtonsoft.Json v12.0.0.0))")]
public partial interface IWeatherForecastClient
{
    /// <exception cref="ApiException">A server side error occurred.</exception>
    System.Threading.Tasks.Task<System.Collections.Generic.ICollection<WeatherForecast>> GetAsync();

    /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
    /// <exception cref="ApiException">A server side error occurred.</exception>
    System.Threading.Tasks.Task<System.Collections.Generic.ICollection<WeatherForecast>> GetAsync(System.Threading.CancellationToken cancellationToken);

    /// <exception cref="ApiException">A server side error occurred.</exception>
    System.Threading.Tasks.Task<FileResponse> SimpleAsync(WeatherForecast w);

    /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
    /// <exception cref="ApiException">A server side error occurred.</exception>
    System.Threading.Tasks.Task<FileResponse> SimpleAsync(WeatherForecast w, System.Threading.CancellationToken cancellationToken);

}

Now, I'll use NSwagStudio option "WebAPI reflection", Config:

  • .Net Asembly paths => .\bin\Debug\net5.0\WebApplication7.dll
  • Path for referenced Asembly files => .\bin\Debug\net5.0\
  • Check Assembly is using ASP.NET Core

And the interface is

[System.CodeDom.Compiler.GeneratedCode("NSwag", "13.10.2.0 (NJsonSchema v10.3.4.0 (Newtonsoft.Json v12.0.0.0))")]
public partial interface IWeatherForecastClient
{
    /// <exception cref="ApiException">A server side error occurred.</exception>
    System.Threading.Tasks.Task<System.Collections.Generic.ICollection<WeatherForecast>> GetAsync();

    /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
    /// <exception cref="ApiException">A server side error occurred.</exception>
    System.Threading.Tasks.Task<System.Collections.Generic.ICollection<WeatherForecast>> GetAsync(System.Threading.CancellationToken cancellationToken);

    /// <exception cref="ApiException">A server side error occurred.</exception>
    System.Threading.Tasks.Task<FileResponse> SimpleAsync(System.DateTimeOffset? date, int? temperatureC, int? temperatureF, string summary);

    /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
    /// <exception cref="ApiException">A server side error occurred.</exception>
    System.Threading.Tasks.Task<FileResponse> SimpleAsync(System.DateTimeOffset? date, int? temperatureC, int? temperatureF, string summary, System.Threading.CancellationToken cancellationToken);

}

The parameter in the post method has been de-constructed!!

The question is: How could I get with reflection the same interface as with OpenAPI specification option?

Vic
  • 337
  • 3
  • 9

1 Answers1

0

Do not use "WebAPI reflection" with ASP.NET Core as it is only for the legacy ASP.NET (non core!). Use "ASP.NET Core via API Explorer" which is also more or less reflection based (loads assemblies or .csproj). I recommend to use .csproj as input instead of DLLs... If the there is no NSwag document registered in DI you can also specify the config via UI.

Rico Suter
  • 11,548
  • 6
  • 67
  • 93