Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/replace asp versioning #17

Merged
merged 6 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<PackageId>Dojo.Generators.Abstractions</PackageId>
<RequireLicenseAcceptance>false</RequireLicenseAcceptance>
<Description>This library contains source generators used in PaymentSense DOJO</Description>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
Expand Down
14 changes: 7 additions & 7 deletions Dojo.Generators.Tests/Dojo.Generators.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

<PropertyGroup>
<IsPackable>false</IsPackable>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ApprovalTests" Version="5.7.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="ApprovalTests" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="xunit" Version="2.7.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.0">
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand All @@ -27,7 +27,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" PrivateAssets="all" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<!-- If you want to see genereted code, then please uncomment EmitCompilerGeneratedFiles and CompilerGeneratedFilesOutputPath blocks -->
<!-- <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>Generated</CompilerGeneratedFilesOutputPath> -->
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="6.0.25" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="5.0.0" />
<PackageReference Include="Microsoft.OpenApi" Version="1.6.11" />
<PackageReference Include="Asp.Versioning.Mvc" Version="8.1.0" />
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" />
<PackageReference Include="AutoMapper" Version="13.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="8.0.4" />
<PackageReference Include="Microsoft.OpenApi" Version="1.6.14" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NSwag.AspNetCore" Version="13.20.0" />
<PackageReference Include="NSwag.AspNetCore" Version="14.0.7" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

Expand Down
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.UseSwaggerUi3(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,9 +1,14 @@
using System.CodeDom.Compiler;
using {{ProjectNamespace}}.Generated.Common;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Versioning;
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 All @@ -16,25 +21,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)
{
Expand All @@ -44,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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using Asp.Versioning;
using System;
using System.CodeDom.Compiler;
using Microsoft.AspNetCore.Mvc;

namespace {{ProjectNamespace}}.Generated.Http
{
Expand Down
14 changes: 3 additions & 11 deletions Dojo.OpenApiGenerator/Dojo.OpenApiGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
<None Include="$(OutputPath)\Dojo.Generators.Abstractions.dll" Pack="true" PackagePath="lib/$(TargetFramework)/Dojo.Generators.Abstractions.dll" Visible="false" />
<None Include="$(OutputPath)\Dojo.Generators.Core.dll" Pack="true" PackagePath="lib/$(TargetFramework)/Dojo.Generators.Core.dll" Visible="false" />

<!-- Package the third pary assemblies longside the generator assembly -->
<None Include="$(PKGSharpYaml)\lib\netstandard2.0\*.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
<None Include="$(PKGStubble_Core)\lib\netstandard2.0\*.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
<None Include="$(PKGMicrosoft_OpenApi)\lib\netstandard2.0\*.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
<None Include="$(PKGMicrosoft_OpenApi_Readers)\lib\netstandard2.0\*.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
<None Include="$(PkgNewtonsoft_Json)\lib\netstandard2.0\*.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
</ItemGroup>

<ItemGroup>
Expand All @@ -60,10 +53,10 @@
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" PrivateAssets="all" />
<PackageReference Include="Microsoft.OpenApi" Version="1.6.11" PrivateAssets="all" GeneratePathProperty="true" />
<PackageReference Include="Microsoft.OpenApi.Readers" Version="1.6.11" PrivateAssets="all" GeneratePathProperty="true" />
<PackageReference Include="Microsoft.OpenApi" Version="1.6.14" PrivateAssets="all" GeneratePathProperty="true" />
<PackageReference Include="Microsoft.OpenApi.Readers" Version="1.6.14" PrivateAssets="all" GeneratePathProperty="true" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" PrivateAssets="all" GeneratePathProperty="true" />
<PackageReference Include="SharpYaml" Version="2.1.0" PrivateAssets="all" GeneratePathProperty="true" />
<PackageReference Include="SharpYaml" Version="2.1.1" PrivateAssets="all" GeneratePathProperty="true" />
<PackageReference Include="Stubble.Core" Version="1.10.8" PrivateAssets="all" GeneratePathProperty="true" />
<PackageReference Include="System.Text.Encodings.Web" Version="4.7.2" />
<ProjectReference Include="../Dojo.Generators.Abstractions/Dojo.Generators.Abstractions.csproj" />
Expand All @@ -78,7 +71,6 @@
<!-- Sections below allow to referrence project directly -->
<PropertyGroup>
<GetTargetPathDependsOn>$(GetTargetPathDependsOn);GetDependencyTargetPaths</GetTargetPathDependsOn>
<Version>1.2.4</Version>
</PropertyGroup>

<Target Name="GetDependencyTargetPaths">
Expand Down
8 changes: 4 additions & 4 deletions TestApp/TestApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<!-- <PackageReference Include="Dojo.AutoGenerators" Version="1.0.8">-->
<!-- <IncludeAssets>all</IncludeAssets>-->
<!-- <PrivateAssets>analyzers</PrivateAssets>-->
<!-- </PackageReference>-->
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Unity.Microsoft.DependencyInjection" Version="5.11.5" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion build/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion build/Dockerfile_build
Original file line number Diff line number Diff line change
@@ -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 . .
Expand Down