Skip to content

Commit

Permalink
fix processing of date versions.
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielBoceanu committed Apr 10, 2024
1 parent 95898d4 commit c8c9281
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 24 deletions.
22 changes: 1 addition & 21 deletions Dojo.OpenApiGenerator.TestWebApi/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
11 changes: 8 additions & 3 deletions Dojo.OpenApiGenerator/AutoApiGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -220,6 +219,12 @@ private HashSet<string> TryGetSupportedApiVersions(IOpenApiExtensible openApiDoc
{
supportedVersions.Add(openApiDateTime.Value.ToString(_autoApiGeneratorSettings.DateTimeVersionFormat));

break;
}
case OpenApiDate openApiDate:
{
supportedVersions.Add(openApiDate.Value.ToString(_autoApiGeneratorSettings.DateTimeVersionFormat));

break;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit c8c9281

Please sign in to comment.