proudmonkey / AutoWrapper

A simple, yet customizable global exception handler and Http response wrapper for ASP.NET Core APIs.
MIT License
679 stars 82 forks source link

Swagger (Swashbuckle) does not generate correct scheme #92

Closed ashahabov closed 3 years ago

ashahabov commented 3 years ago

Environment

.csproj

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="AutoWrapper.Core" Version="4.3.1" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
  </ItemGroup>

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    services.AddSwaggerGen();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseHttpsRedirection();
    app.UseApiResponseAndExceptionWrapper();
    app.UseRouting();

    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });
    app.UseSwagger();
    app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"));
}

Swagger scheme

Response of GET: /weatherforecast request is correct:

{
  "message": "GET Request successful.",
  "result": [
    {
      "date": "2020-12-29T17:15:40.56733+02:00",
      "temperatureC": -2,
      "temperatureF": 29,
      "summary": "Bracing"
    },
    {
      "date": "2020-12-30T17:15:40.5673646+02:00",
      "temperatureC": -10,
      "temperatureF": 15,
      "summary": "Chilly"
    },
    {
      "date": "2020-12-31T17:15:40.5673653+02:00",
      "temperatureC": 27,
      "temperatureF": 80,
      "summary": "Balmy"
    },
    {
      "date": "2021-01-01T17:15:40.5673657+02:00",
      "temperatureC": 6,
      "temperatureF": 42,
      "summary": "Warm"
    },
    {
      "date": "2021-01-02T17:15:40.567366+02:00",
      "temperatureC": 41,
      "temperatureF": 105,
      "summary": "Chilly"
    }
  ]
}

But ACTUAL swagger's scheme is not: image

EXPECTED scheme:

{
  ...
  "result": [
    {
      "date": "2020-12-28T15:21:59.414Z",
      "temperatureC": 0,
      "temperatureF": 0,
      "summary": "string"
    }
  ]
}
proudmonkey commented 3 years ago

Set the ProducesResponseType attribute and then pass the ApiResultResponse as type like in the following:

        [Route("weatherforecast")]
        [HttpGet]
        [ProducesResponseType(typeof(ApiResultResponse<WeatherForecast>), Status200OK)]
        public async Task<IActionResult> Get()
        {
            // some code
        }
ashahabov commented 3 years ago

Works! Thank you.