Skip to content

Commit 6f6ccbd

Browse files
authored
Merge pull request #10 from fossapps/swagger
feat(swagger): add openapi support
2 parents 4b04775 + 083428a commit 6f6ccbd

File tree

3 files changed

+17
-23
lines changed

3 files changed

+17
-23
lines changed

Micro.Starter.Api/Controllers/WeatherForecastController.cs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace Micro.Starter.Api.Controllers
99
{
1010
[ApiController]
11-
[Route("api/v{version:apiVersion}/[controller]")]
11+
[Route("api/[controller]")]
1212
public class WeatherForecastController : ControllerBase
1313
{
1414
private static readonly string[] Summaries = new[]
@@ -24,7 +24,6 @@ public WeatherForecastController(ILogger<WeatherForecastController> logger)
2424
}
2525

2626
[HttpGet]
27-
[ApiVersion("1", Deprecated = true)]
2827
[ProducesResponseType(typeof(IEnumerable<WeatherForecast>), StatusCodes.Status200OK)]
2928
public IEnumerable<WeatherForecast> Get()
3029
{
@@ -37,20 +36,5 @@ public IEnumerable<WeatherForecast> Get()
3736
})
3837
.ToArray();
3938
}
40-
41-
[HttpGet]
42-
[ApiVersion("2")]
43-
[ProducesResponseType(typeof(IEnumerable<WeatherForecast>), StatusCodes.Status200OK)]
44-
public IEnumerable<WeatherForecast> GetNewVersion()
45-
{
46-
var rng = new Random();
47-
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
48-
{
49-
Date = DateTime.Now.AddDays(index),
50-
TemperatureC = rng.Next(-20, 55),
51-
Summary = Summaries[rng.Next(Summaries.Length)]
52-
})
53-
.ToArray();
54-
}
5539
}
5640
}

Micro.Starter.Api/Micro.Starter.Api.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
<PackageReference Include="App.Metrics.AspNetCore.Mvc" Version="3.2.0-dev0002" />
1010
<PackageReference Include="App.Metrics.Extensions.Configuration" Version="3.2.0-dev0002" />
1111
<PackageReference Include="App.Metrics.Reporting.InfluxDB" Version="3.2.0-dev0002" />
12-
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="4.0.0-preview8.19405.7" />
1312
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.0.0-preview8.19405.11" />
1413
<PackageReference Include="Microsoft.Extensions.Logging.Slack" Version="1.1.0" />
1514
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.0.0-preview8" />
1615
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.Design" Version="1.1.1" />
1716
<None Include="./appsettings.ci.json" CopyToPublishDirectory="Always" />
17+
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc2" />
1818
</ItemGroup>
1919

2020
<Target Name="Install githooks" BeforeTargets="Build">

Micro.Starter.Api/Startup.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using Micro.Starter.Api.Configs;
34
using Micro.Starter.Api.Models;
45
using Micro.Starter.Api.Repository;
@@ -13,6 +14,7 @@
1314
using Microsoft.Extensions.Logging;
1415
using Microsoft.Extensions.Logging.Slack;
1516
using Microsoft.Extensions.Options;
17+
using Microsoft.OpenApi.Models;
1618

1719
namespace Micro.Starter.Api
1820
{
@@ -32,12 +34,14 @@ public void ConfigureServices(IServiceCollection services)
3234
services.AddMetrics();
3335
ConfigureDependencies(services);
3436
services.AddControllers();
35-
services.AddApiVersioning(x =>
37+
services.AddSwaggerGen(c =>
3638
{
37-
x.DefaultApiVersion = new ApiVersion(1, 0);
38-
x.RegisterMiddleware = true;
39-
x.ReportApiVersions = true;
40-
x.AssumeDefaultVersionWhenUnspecified = true;
39+
c.ResolveConflictingActions(apiDescription => apiDescription.Last());
40+
c.SwaggerDoc("v1", new OpenApiInfo
41+
{
42+
Title = "Title",
43+
Version = "v1"
44+
});
4145
});
4246
RegisterWorker(services);
4347
}
@@ -73,6 +77,12 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF
7377

7478
app.UseAuthorization();
7579

80+
app.UseSwagger();
81+
app.UseSwaggerUI(x =>
82+
{
83+
x.RoutePrefix = "swagger";
84+
x.SwaggerEndpoint("/swagger/v1/swagger.json", "V1");
85+
});
7686
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
7787
}
7888

0 commit comments

Comments
 (0)