From 0003d4c273f8996394ee3cc057f97577a2bcb4db Mon Sep 17 00:00:00 2001 From: GabrielBoceanu Date: Wed, 10 Apr 2024 18:33:25 +0100 Subject: [PATCH 1/6] update --- Dojo.Generators.Tests/Dojo.Generators.Tests.csproj | 12 ++++++------ .../Dojo.OpenApiGenerator.TestWebApi.csproj | 6 +++--- Dojo.OpenApiGenerator/Dojo.OpenApiGenerator.csproj | 14 +++----------- TestApp/TestApp.csproj | 6 +++--- 4 files changed, 15 insertions(+), 23 deletions(-) diff --git a/Dojo.Generators.Tests/Dojo.Generators.Tests.csproj b/Dojo.Generators.Tests/Dojo.Generators.Tests.csproj index ee0e4fc..331882b 100644 --- a/Dojo.Generators.Tests/Dojo.Generators.Tests.csproj +++ b/Dojo.Generators.Tests/Dojo.Generators.Tests.csproj @@ -6,15 +6,15 @@ - - - + + + - + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all @@ -27,7 +27,7 @@ - + diff --git a/Dojo.OpenApiGenerator.TestWebApi/Dojo.OpenApiGenerator.TestWebApi.csproj b/Dojo.OpenApiGenerator.TestWebApi/Dojo.OpenApiGenerator.TestWebApi.csproj index 7bc956b..74b5ce2 100644 --- a/Dojo.OpenApiGenerator.TestWebApi/Dojo.OpenApiGenerator.TestWebApi.csproj +++ b/Dojo.OpenApiGenerator.TestWebApi/Dojo.OpenApiGenerator.TestWebApi.csproj @@ -8,12 +8,12 @@ - + - + - + diff --git a/Dojo.OpenApiGenerator/Dojo.OpenApiGenerator.csproj b/Dojo.OpenApiGenerator/Dojo.OpenApiGenerator.csproj index 5111f22..8cf8817 100644 --- a/Dojo.OpenApiGenerator/Dojo.OpenApiGenerator.csproj +++ b/Dojo.OpenApiGenerator/Dojo.OpenApiGenerator.csproj @@ -39,13 +39,6 @@ - - - - - - - @@ -60,10 +53,10 @@ - - + + - + @@ -78,7 +71,6 @@ $(GetTargetPathDependsOn);GetDependencyTargetPaths - 1.2.4 diff --git a/TestApp/TestApp.csproj b/TestApp/TestApp.csproj index f316c20..b0d1a2c 100644 --- a/TestApp/TestApp.csproj +++ b/TestApp/TestApp.csproj @@ -10,9 +10,9 @@ - - - + + + From e8a1bab0e2f9228f178a16913e2553bcfa5ccbce Mon Sep 17 00:00:00 2001 From: GabrielBoceanu Date: Wed, 10 Apr 2024 20:47:55 +0100 Subject: [PATCH 2/6] update asp versioning --- .../Dojo.OpenApiGenerator.TestWebApi.csproj | 7 ++++--- Dojo.OpenApiGenerator.TestWebApi/Startup.cs | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Dojo.OpenApiGenerator.TestWebApi/Dojo.OpenApiGenerator.TestWebApi.csproj b/Dojo.OpenApiGenerator.TestWebApi/Dojo.OpenApiGenerator.TestWebApi.csproj index 74b5ce2..10044ff 100644 --- a/Dojo.OpenApiGenerator.TestWebApi/Dojo.OpenApiGenerator.TestWebApi.csproj +++ b/Dojo.OpenApiGenerator.TestWebApi/Dojo.OpenApiGenerator.TestWebApi.csproj @@ -1,16 +1,17 @@  - net6.0 + net8.0 + + + - - diff --git a/Dojo.OpenApiGenerator.TestWebApi/Startup.cs b/Dojo.OpenApiGenerator.TestWebApi/Startup.cs index 81839d7..db27c01 100644 --- a/Dojo.OpenApiGenerator.TestWebApi/Startup.cs +++ b/Dojo.OpenApiGenerator.TestWebApi/Startup.cs @@ -48,7 +48,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) options.Path = "/api/swagger/{documentName}/swagger.json"; }); - app.UseSwaggerUi3(x => + app.UseSwaggerUi(x => { x.Path = "/api/swagger"; x.DocumentPath = "/api/swagger/{documentName}/swagger.json"; From 15eaad050240faaea315e610034f2d4528dd73f8 Mon Sep 17 00:00:00 2001 From: GabrielBoceanu Date: Wed, 10 Apr 2024 20:57:39 +0100 Subject: [PATCH 3/6] update templates to generate new api version attributes --- .../ApiConfiguratorTemplate.mustache | 30 ++++++++----------- .../InheritedApiVersionAttribute.mustache | 4 +-- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/Dojo.OpenApiGenerator/CodeTemplates/ApiConfiguratorTemplate.mustache b/Dojo.OpenApiGenerator/CodeTemplates/ApiConfiguratorTemplate.mustache index d5bd3d6..6cb1ede 100644 --- a/Dojo.OpenApiGenerator/CodeTemplates/ApiConfiguratorTemplate.mustache +++ b/Dojo.OpenApiGenerator/CodeTemplates/ApiConfiguratorTemplate.mustache @@ -1,9 +1,8 @@ -using System.CodeDom.Compiler; -using {{ProjectNamespace}}.Generated.Common; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.Versioning; +using Asp.Versioning; using Microsoft.Extensions.DependencyInjection; using NSwag.Generation.AspNetCore; +using System.CodeDom.Compiler; +using {{ProjectNamespace}}.Generated.Common; namespace {{ProjectNamespace}}.Generated.StartupConfiguration { @@ -16,25 +15,20 @@ namespace {{ProjectNamespace}}.Generated.StartupConfiguration string versionHeaderName = "version", string infoTitle = "API", IApiVersionReader versionReader = null, - IErrorResponseProvider errorResponseProvider = null, ApiVersion defaultVersion = null, bool assumeDefaultVersionWhenUnspecified = true, bool reportApiVersions = true) { - services.AddApiVersioning(options => - { - options.AssumeDefaultVersionWhenUnspecified = assumeDefaultVersionWhenUnspecified; - options.DefaultApiVersion = defaultVersion ?? ApiVersion.Default; - options.ApiVersionReader = versionReader ?? new HeaderApiVersionReader(versionHeaderName); - options.ReportApiVersions = reportApiVersions; - - if (errorResponseProvider != null) + services + .AddApiVersioning(options => { - options.ErrorResponses = errorResponseProvider; - } - }); - - services.AddVersionedApiExplorer(); + options.AssumeDefaultVersionWhenUnspecified = assumeDefaultVersionWhenUnspecified; + options.DefaultApiVersion = defaultVersion ?? ApiVersion.Default; + options.ApiVersionReader = versionReader ?? new HeaderApiVersionReader(versionHeaderName); + options.ReportApiVersions = reportApiVersions; + }) + .AddMvc() + .AddApiExplorer(); foreach (var apiVersion in ApiConstants.ApiVersions) { diff --git a/Dojo.OpenApiGenerator/CodeTemplates/InheritedApiVersionAttribute.mustache b/Dojo.OpenApiGenerator/CodeTemplates/InheritedApiVersionAttribute.mustache index 71fb7c4..6988741 100644 --- a/Dojo.OpenApiGenerator/CodeTemplates/InheritedApiVersionAttribute.mustache +++ b/Dojo.OpenApiGenerator/CodeTemplates/InheritedApiVersionAttribute.mustache @@ -1,6 +1,6 @@ -using System; +using Asp.Versioning; +using System; using System.CodeDom.Compiler; -using Microsoft.AspNetCore.Mvc; namespace {{ProjectNamespace}}.Generated.Http { From 4c6c1ea5c20930848f337f6080a637264525564c Mon Sep 17 00:00:00 2001 From: GabrielBoceanu Date: Wed, 10 Apr 2024 21:01:06 +0100 Subject: [PATCH 4/6] update docker --- build/Dockerfile | 2 +- build/Dockerfile_build | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/build/Dockerfile b/build/Dockerfile index a78194c..51e081e 100644 --- a/build/Dockerfile +++ b/build/Dockerfile @@ -1,4 +1,4 @@ -FROM mcr.microsoft.com/dotnet/sdk:6.0 as builder +FROM mcr.microsoft.com/dotnet/sdk:8.0 as builder ARG VERSION_SUFFIX ARG NEXUS_URL ARG NEXUS_KEY diff --git a/build/Dockerfile_build b/build/Dockerfile_build index 5bb9578..6be0f05 100644 --- a/build/Dockerfile_build +++ b/build/Dockerfile_build @@ -1,4 +1,4 @@ -FROM mcr.microsoft.com/dotnet/sdk:6.0 as builder +FROM mcr.microsoft.com/dotnet/sdk:8.0 as builder WORKDIR /src ADD . . From 95898d43befc72955ac62612e74f58e6e148c4cb Mon Sep 17 00:00:00 2001 From: GabrielBoceanu Date: Wed, 10 Apr 2024 21:15:42 +0100 Subject: [PATCH 5/6] upgrade all projects --- .../Dojo.Generators.Abstractions.csproj | 1 + Dojo.Generators.Tests/Dojo.Generators.Tests.csproj | 2 +- TestApp/TestApp.csproj | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Dojo.Generators.Abstractions/Dojo.Generators.Abstractions.csproj b/Dojo.Generators.Abstractions/Dojo.Generators.Abstractions.csproj index 3a369df..1b718fd 100644 --- a/Dojo.Generators.Abstractions/Dojo.Generators.Abstractions.csproj +++ b/Dojo.Generators.Abstractions/Dojo.Generators.Abstractions.csproj @@ -6,6 +6,7 @@ Dojo.Generators.Abstractions false This library contains source generators used in PaymentSense DOJO + netstandard2.0 diff --git a/Dojo.Generators.Tests/Dojo.Generators.Tests.csproj b/Dojo.Generators.Tests/Dojo.Generators.Tests.csproj index 331882b..2c0d9a4 100644 --- a/Dojo.Generators.Tests/Dojo.Generators.Tests.csproj +++ b/Dojo.Generators.Tests/Dojo.Generators.Tests.csproj @@ -2,7 +2,7 @@ false - net6.0 + net8.0 diff --git a/TestApp/TestApp.csproj b/TestApp/TestApp.csproj index b0d1a2c..3076708 100644 --- a/TestApp/TestApp.csproj +++ b/TestApp/TestApp.csproj @@ -2,7 +2,7 @@ Exe - net6.0 + net8.0 From c8c9281b4e38446144cbd30b05ba110a51cf28ca Mon Sep 17 00:00:00 2001 From: GabrielBoceanu Date: Wed, 10 Apr 2024 22:01:58 +0100 Subject: [PATCH 6/6] 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,