Skip to content

Commit 8fba7ed

Browse files
committed
chore: enable Swagger UI
1 parent f8dd7e9 commit 8fba7ed

File tree

4 files changed

+76
-10
lines changed

4 files changed

+76
-10
lines changed

Applications/ConsumerApi/src/ConsumerApi.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<PackageReference Include="Serilog.Formatting.Compact" Version="3.0.0" />
2525
<PackageReference Include="Serilog.Sinks.Http" Version="9.1.0" />
2626
<PackageReference Include="Serilog.Sinks.Seq" Version="9.0.0" />
27+
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" />
2728
</ItemGroup>
2829

2930
<ItemGroup>

Applications/ConsumerApi/src/Program.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
using Microsoft.AspNetCore.HttpOverrides;
3636
using Microsoft.Extensions.Options;
3737
using Microsoft.IdentityModel.Logging;
38+
using Microsoft.OpenApi.Models;
3839
using Serilog;
3940
using Serilog.Enrichers.Sensitive;
4041
using Serilog.Exceptions;
@@ -130,6 +131,67 @@ static WebApplication CreateApp(string[] args)
130131

131132
static void ConfigureServices(IServiceCollection services, IConfiguration configuration, IHostEnvironment environment)
132133
{
134+
services.AddEndpointsApiExplorer();
135+
136+
services.AddSwaggerGen(c =>
137+
{
138+
c.CustomSchemaIds(t =>
139+
{
140+
static string GetReadableName(Type type)
141+
{
142+
if (!type.IsGenericType)
143+
{
144+
return type.Name
145+
.Replace("DTO", string.Empty)
146+
.Replace("Command", "Request")
147+
.Replace("Query", "Request");
148+
}
149+
150+
var typeName = type.Name
151+
.Replace("HttpResponseEnvelopeResult", "ResponseWrapper")
152+
.Replace("PagedHttpResponseEnvelopeResult", "PagedResponseWrapper");
153+
var name = $"{typeName[..typeName.IndexOf('`')]}_{string.Join("_", type.GetGenericArguments().Select(GetReadableName))}";
154+
return name;
155+
}
156+
157+
return GetReadableName(t);
158+
});
159+
160+
c.AddServer(new OpenApiServer { Url = "https://nmshd-bkb.demo.meinbildungsraum.de" });
161+
c.AddServer(new OpenApiServer { Url = "https://nmshd-bkb.meinbildungsraum.de" });
162+
c.AddServer(new OpenApiServer { Url = "https://pilot.enmeshed.eu" });
163+
164+
c.AddSecurityDefinition(
165+
"oauth2",
166+
new OpenApiSecurityScheme
167+
{
168+
Type = SecuritySchemeType.OAuth2,
169+
Flows = new OpenApiOAuthFlows
170+
{
171+
Password = new OpenApiOAuthFlow
172+
{
173+
TokenUrl = new Uri("/connect/token", UriKind.Relative)
174+
}
175+
}
176+
});
177+
178+
c.AddSecurityRequirement(
179+
new OpenApiSecurityRequirement
180+
{
181+
{
182+
new OpenApiSecurityScheme
183+
{
184+
Reference = new OpenApiReference
185+
{
186+
Id = "oauth2", //The name of the previously defined security scheme.
187+
Type = ReferenceType.SecurityScheme
188+
}
189+
},
190+
new List<string>()
191+
}
192+
});
193+
});
194+
133195
services.ConfigureAndValidate<BackboneConfiguration>(configuration.Bind);
134196

135197
#pragma warning disable ASP0000 // We retrieve the BackboneConfiguration via IOptions here so that it is validated
@@ -181,6 +243,9 @@ static void ConfigureServices(IServiceCollection services, IConfiguration config
181243

182244
static void Configure(WebApplication app)
183245
{
246+
app.UseSwagger();
247+
app.UseSwaggerUI();
248+
184249
app.UseSerilogRequestLogging(opts =>
185250
{
186251
opts.EnrichDiagnosticContext = LogHelper.EnrichFromRequest;

BuildingBlocks/src/BuildingBlocks.API/HttpResponseEnvelope.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,20 @@ public HttpResponseEnvelopeResult(T result)
3030

3131
public class PagedHttpResponseEnvelope<T> : HttpResponseEnvelopeResult<IEnumerable<T>>
3232
{
33-
public PagedHttpResponseEnvelope(IEnumerable<T> result, PaginationData paginationData) : base(result)
33+
public PagedHttpResponseEnvelope(IEnumerable<T> result, PagedHttpResponseEnvelopePaginationData paginationData) : base(result)
3434
{
3535
Pagination = paginationData;
3636
}
3737

38-
public PaginationData Pagination { get; set; }
38+
public PagedHttpResponseEnvelopePaginationData Pagination { get; set; }
39+
}
3940

40-
public class PaginationData
41-
{
42-
public int PageNumber { get; set; }
43-
public int? PageSize { get; set; }
44-
public int TotalPages { get; set; }
45-
public int TotalRecords { get; set; }
46-
}
41+
public class PagedHttpResponseEnvelopePaginationData
42+
{
43+
public int PageNumber { get; set; }
44+
public int? PageSize { get; set; }
45+
public int TotalPages { get; set; }
46+
public int TotalRecords { get; set; }
4747
}
4848

4949
public class HttpResponseEnvelopeError : HttpResponseEnvelope

BuildingBlocks/src/BuildingBlocks.API/Mvc/ApiControllerBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public OkObjectResult Paged<T>(IEnumerable<T> pagedData, PaginationData paginati
6868
{
6969
if (paginationData.TotalPages <= 1) return Ok(pagedData);
7070

71-
var response = new PagedHttpResponseEnvelope<T>(pagedData, new PagedHttpResponseEnvelope<T>.PaginationData
71+
var response = new PagedHttpResponseEnvelope<T>(pagedData, new PagedHttpResponseEnvelopePaginationData
7272
{
7373
TotalRecords = paginationData.TotalRecords,
7474
PageNumber = paginationData.PageNumber,

0 commit comments

Comments
 (0)