From c8c9281b4e38446144cbd30b05ba110a51cf28ca Mon Sep 17 00:00:00 2001 From: GabrielBoceanu Date: Wed, 10 Apr 2024 22:01:58 +0100 Subject: [PATCH] fix processing of date versions. --- Dojo.OpenApiGenerator.TestWebApi/Startup.cs | 22 +--------- Dojo.OpenApiGenerator/AutoApiGenerator.cs | 11 +++-- .../ApiConfiguratorTemplate.mustache | 42 +++++++++++++++++++ 3 files changed, 51 insertions(+), 24 deletions(-) diff --git a/Dojo.OpenApiGenerator.TestWebApi/Startup.cs b/Dojo.OpenApiGenerator.TestWebApi/Startup.cs index db27c01..8c0a041 100644 --- a/Dojo.OpenApiGenerator.TestWebApi/Startup.cs +++ b/Dojo.OpenApiGenerator.TestWebApi/Startup.cs @@ -43,27 +43,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) var serverUrl = string.Empty; - app.UseOpenApi(options => - { - options.Path = "/api/swagger/{documentName}/swagger.json"; - }); - - app.UseSwaggerUi(x => - { - x.Path = "/api/swagger"; - x.DocumentPath = "/api/swagger/{documentName}/swagger.json"; - - if (!env.IsDevelopment()) - { - x.TransformToExternalPath = (url, _) => - { - x.ServerUrl = serverUrl; - return url.EndsWith(".json", StringComparison.OrdinalIgnoreCase) - ? x.ServerUrl + url - : url; - }; - } - }); + app.UseOpenApiSwaggerUi(env); app.UseHttpsRedirection(); diff --git a/Dojo.OpenApiGenerator/AutoApiGenerator.cs b/Dojo.OpenApiGenerator/AutoApiGenerator.cs index 6a88c2b..3868529 100644 --- a/Dojo.OpenApiGenerator/AutoApiGenerator.cs +++ b/Dojo.OpenApiGenerator/AutoApiGenerator.cs @@ -47,11 +47,10 @@ public void Initialize(GeneratorInitializationContext context) //#if DEBUG // if (!Debugger.IsAttached) // { -// Debugger.Launch(); +// Debugger.Launch(); // } //#endif - -// Debug.WriteLine("Initialize code generator"); +// Debug.WriteLine("Initialize code generator"); } public void Execute(GeneratorExecutionContext context) @@ -220,6 +219,12 @@ private HashSet TryGetSupportedApiVersions(IOpenApiExtensible openApiDoc { supportedVersions.Add(openApiDateTime.Value.ToString(_autoApiGeneratorSettings.DateTimeVersionFormat)); + break; + } + case OpenApiDate openApiDate: + { + supportedVersions.Add(openApiDate.Value.ToString(_autoApiGeneratorSettings.DateTimeVersionFormat)); + break; } } diff --git a/Dojo.OpenApiGenerator/CodeTemplates/ApiConfiguratorTemplate.mustache b/Dojo.OpenApiGenerator/CodeTemplates/ApiConfiguratorTemplate.mustache index 6cb1ede..de1082b 100644 --- a/Dojo.OpenApiGenerator/CodeTemplates/ApiConfiguratorTemplate.mustache +++ b/Dojo.OpenApiGenerator/CodeTemplates/ApiConfiguratorTemplate.mustache @@ -1,7 +1,13 @@ using Asp.Versioning; +using Dojo.OpenApiGenerator.TestWebApi.Generated.Common; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; using NSwag.Generation.AspNetCore; +using System; using System.CodeDom.Compiler; +using System.Linq; using {{ProjectNamespace}}.Generated.Common; namespace {{ProjectNamespace}}.Generated.StartupConfiguration @@ -38,6 +44,42 @@ namespace {{ProjectNamespace}}.Generated.StartupConfiguration return services; } + public static IApplicationBuilder UseOpenApiSwaggerUi(this IApplicationBuilder app, IWebHostEnvironment env, bool prefixWithEnvironmentName = true, string serverUrl = "", string swaggerPath = "/api/swagger", string docPath = "/api/swagger/{documentName}/swagger.json") + { + app.UseOpenApi(options => + { + options.Path = docPath; + + if (prefixWithEnvironmentName && !env.IsDevelopment()) + { + options.PostProcess = (document, _) => + { + document.Servers.First().Url += $"/{env.EnvironmentName}"; + serverUrl = document.Servers.First().Url; + }; + } + }); + + app.UseSwaggerUi(x => + { + x.Path = swaggerPath; + x.DocumentPath = docPath; + + if (prefixWithEnvironmentName && !env.IsDevelopment()) + { + x.TransformToExternalPath = (url, _) => + { + x.ServerUrl = serverUrl; + return url.EndsWith(".json", StringComparison.OrdinalIgnoreCase) + ? x.ServerUrl + url + : url; + }; + } + }); + + return app; + } + private static void ConfigureSingleVersion( string title, AspNetCoreOpenApiDocumentGeneratorSettings configure,