From 8fba7edae857c77199431fbc3fa6f851b4d3b0f6 Mon Sep 17 00:00:00 2001 From: Timo Notheisen Date: Wed, 19 Feb 2025 16:18:33 +0100 Subject: [PATCH] chore: enable Swagger UI --- .../ConsumerApi/src/ConsumerApi.csproj | 1 + Applications/ConsumerApi/src/Program.cs | 65 +++++++++++++++++++ .../HttpResponseEnvelope.cs | 18 ++--- .../Mvc/ApiControllerBase.cs | 2 +- 4 files changed, 76 insertions(+), 10 deletions(-) diff --git a/Applications/ConsumerApi/src/ConsumerApi.csproj b/Applications/ConsumerApi/src/ConsumerApi.csproj index cd52bf3846..4d731db617 100644 --- a/Applications/ConsumerApi/src/ConsumerApi.csproj +++ b/Applications/ConsumerApi/src/ConsumerApi.csproj @@ -24,6 +24,7 @@ + diff --git a/Applications/ConsumerApi/src/Program.cs b/Applications/ConsumerApi/src/Program.cs index da4caf932e..effeaac25e 100644 --- a/Applications/ConsumerApi/src/Program.cs +++ b/Applications/ConsumerApi/src/Program.cs @@ -35,6 +35,7 @@ using Microsoft.AspNetCore.HttpOverrides; using Microsoft.Extensions.Options; using Microsoft.IdentityModel.Logging; +using Microsoft.OpenApi.Models; using Serilog; using Serilog.Enrichers.Sensitive; using Serilog.Exceptions; @@ -130,6 +131,67 @@ static WebApplication CreateApp(string[] args) static void ConfigureServices(IServiceCollection services, IConfiguration configuration, IHostEnvironment environment) { + services.AddEndpointsApiExplorer(); + + services.AddSwaggerGen(c => + { + c.CustomSchemaIds(t => + { + static string GetReadableName(Type type) + { + if (!type.IsGenericType) + { + return type.Name + .Replace("DTO", string.Empty) + .Replace("Command", "Request") + .Replace("Query", "Request"); + } + + var typeName = type.Name + .Replace("HttpResponseEnvelopeResult", "ResponseWrapper") + .Replace("PagedHttpResponseEnvelopeResult", "PagedResponseWrapper"); + var name = $"{typeName[..typeName.IndexOf('`')]}_{string.Join("_", type.GetGenericArguments().Select(GetReadableName))}"; + return name; + } + + return GetReadableName(t); + }); + + c.AddServer(new OpenApiServer { Url = "https://nmshd-bkb.demo.meinbildungsraum.de" }); + c.AddServer(new OpenApiServer { Url = "https://nmshd-bkb.meinbildungsraum.de" }); + c.AddServer(new OpenApiServer { Url = "https://pilot.enmeshed.eu" }); + + c.AddSecurityDefinition( + "oauth2", + new OpenApiSecurityScheme + { + Type = SecuritySchemeType.OAuth2, + Flows = new OpenApiOAuthFlows + { + Password = new OpenApiOAuthFlow + { + TokenUrl = new Uri("/connect/token", UriKind.Relative) + } + } + }); + + c.AddSecurityRequirement( + new OpenApiSecurityRequirement + { + { + new OpenApiSecurityScheme + { + Reference = new OpenApiReference + { + Id = "oauth2", //The name of the previously defined security scheme. + Type = ReferenceType.SecurityScheme + } + }, + new List() + } + }); + }); + services.ConfigureAndValidate(configuration.Bind); #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 static void Configure(WebApplication app) { + app.UseSwagger(); + app.UseSwaggerUI(); + app.UseSerilogRequestLogging(opts => { opts.EnrichDiagnosticContext = LogHelper.EnrichFromRequest; diff --git a/BuildingBlocks/src/BuildingBlocks.API/HttpResponseEnvelope.cs b/BuildingBlocks/src/BuildingBlocks.API/HttpResponseEnvelope.cs index 247c426c58..ba5906b50a 100644 --- a/BuildingBlocks/src/BuildingBlocks.API/HttpResponseEnvelope.cs +++ b/BuildingBlocks/src/BuildingBlocks.API/HttpResponseEnvelope.cs @@ -30,20 +30,20 @@ public HttpResponseEnvelopeResult(T result) public class PagedHttpResponseEnvelope : HttpResponseEnvelopeResult> { - public PagedHttpResponseEnvelope(IEnumerable result, PaginationData paginationData) : base(result) + public PagedHttpResponseEnvelope(IEnumerable result, PagedHttpResponseEnvelopePaginationData paginationData) : base(result) { Pagination = paginationData; } - public PaginationData Pagination { get; set; } + public PagedHttpResponseEnvelopePaginationData Pagination { get; set; } +} - public class PaginationData - { - public int PageNumber { get; set; } - public int? PageSize { get; set; } - public int TotalPages { get; set; } - public int TotalRecords { get; set; } - } +public class PagedHttpResponseEnvelopePaginationData +{ + public int PageNumber { get; set; } + public int? PageSize { get; set; } + public int TotalPages { get; set; } + public int TotalRecords { get; set; } } public class HttpResponseEnvelopeError : HttpResponseEnvelope diff --git a/BuildingBlocks/src/BuildingBlocks.API/Mvc/ApiControllerBase.cs b/BuildingBlocks/src/BuildingBlocks.API/Mvc/ApiControllerBase.cs index d1cd44ec9a..6528f8f0a2 100644 --- a/BuildingBlocks/src/BuildingBlocks.API/Mvc/ApiControllerBase.cs +++ b/BuildingBlocks/src/BuildingBlocks.API/Mvc/ApiControllerBase.cs @@ -68,7 +68,7 @@ public OkObjectResult Paged(IEnumerable pagedData, PaginationData paginati { if (paginationData.TotalPages <= 1) return Ok(pagedData); - var response = new PagedHttpResponseEnvelope(pagedData, new PagedHttpResponseEnvelope.PaginationData + var response = new PagedHttpResponseEnvelope(pagedData, new PagedHttpResponseEnvelopePaginationData { TotalRecords = paginationData.TotalRecords, PageNumber = paginationData.PageNumber,