-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9782 from knom/aspnetcore3
Updated to support .net core 3
- Loading branch information
Showing
22 changed files
with
575 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
modules/swagger-codegen/src/main/resources/aspnetcore/3.0/Dockerfile.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
FROM mcr.microsoft.com/dotnet/core/sdk:{{aspNetCoreVersion}} AS build-env | ||
WORKDIR /app | ||
|
||
ENV DOTNET_CLI_TELEMETRY_OPTOUT 1 | ||
|
||
# copy csproj and restore as distinct layers | ||
COPY *.csproj ./ | ||
RUN dotnet restore | ||
|
||
# copy everything else and build | ||
COPY . ./ | ||
RUN dotnet publish -c Release -o out | ||
|
||
# build runtime image | ||
FROM mcr.microsoft.com/dotnet/core/aspnet:{{aspNetCoreVersion}} | ||
WORKDIR /app | ||
COPY --from=build-env /app/out . | ||
ENTRYPOINT ["dotnet", "{{packageName}}.dll"] |
51 changes: 51 additions & 0 deletions
51
modules/swagger-codegen/src/main/resources/aspnetcore/3.0/Filters/BasePathFilter.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
using Swashbuckle.AspNetCore.Swagger; | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
using Microsoft.OpenApi.Models; | ||
|
||
namespace {{packageName}}.Filters | ||
{ | ||
/// <summary> | ||
/// BasePath Document Filter sets BasePath property of Swagger and removes it from the individual URL paths | ||
/// </summary> | ||
public class BasePathFilter : IDocumentFilter | ||
{ | ||
/// <summary> | ||
/// Constructor | ||
/// </summary> | ||
/// <param name="basePath">BasePath to remove from Operations</param> | ||
public BasePathFilter(string basePath) | ||
{ | ||
BasePath = basePath; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the BasePath of the Swagger Doc | ||
/// </summary> | ||
/// <returns>The BasePath of the Swagger Doc</returns> | ||
public string BasePath { get; } | ||
|
||
/// <summary> | ||
/// Apply the filter | ||
/// </summary> | ||
/// <param name="swaggerDoc">OpenApiDocument</param> | ||
/// <param name="context">FilterContext</param> | ||
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context) | ||
{ | ||
swaggerDoc.Servers.Add(new OpenApiServer() { Url = this.BasePath }); | ||
|
||
var pathsToModify = swaggerDoc.Paths.Where(p => p.Key.StartsWith(this.BasePath)).ToList(); | ||
|
||
foreach (var path in pathsToModify) | ||
{ | ||
if (path.Key.StartsWith(this.BasePath)) | ||
{ | ||
string newKey = Regex.Replace(path.Key, $"^{this.BasePath}", string.Empty); | ||
swaggerDoc.Paths.Remove(path.Key); | ||
swaggerDoc.Paths.Add(newKey, path.Value); | ||
} | ||
} | ||
} | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
...gen/src/main/resources/aspnetcore/3.0/Filters/GeneratePathParamsValidationFilter.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Linq; | ||
using Microsoft.AspNetCore.Mvc.Controllers; | ||
using Swashbuckle.AspNetCore.Swagger; | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
using Microsoft.OpenApi.Models; | ||
|
||
namespace {{packageName}}.Filters | ||
{ | ||
/// <summary> | ||
/// Path Parameter Validation Rules Filter | ||
/// </summary> | ||
public class GeneratePathParamsValidationFilter : IOperationFilter | ||
{ | ||
/// <summary> | ||
/// Constructor | ||
/// </summary> | ||
/// <param name="operation">Operation</param> | ||
/// <param name="context">OperationFilterContext</param> | ||
public void Apply(OpenApiOperation operation, OperationFilterContext context) | ||
{ | ||
var pars = context.ApiDescription.ParameterDescriptions; | ||
foreach (var par in pars) | ||
{ | ||
var swaggerParam = operation.Parameters.SingleOrDefault(p => p.Name == par.Name); | ||
var attributes = ((ControllerParameterDescriptor)par.ParameterDescriptor).ParameterInfo.CustomAttributes; | ||
if (attributes != null && attributes.Count() > 0 && swaggerParam != null) | ||
{ | ||
// Required - [Required] | ||
var requiredAttr = attributes.FirstOrDefault(p => p.AttributeType == typeof(RequiredAttribute)); | ||
if (requiredAttr != null) | ||
{ | ||
swaggerParam.Required = true; | ||
} | ||
|
||
// Regex Pattern [RegularExpression] | ||
var regexAttr = attributes.FirstOrDefault(p => p.AttributeType == typeof(RegularExpressionAttribute)); | ||
if (regexAttr != null) | ||
{ | ||
string regex = (string)regexAttr.ConstructorArguments[0].Value; | ||
if (swaggerParam is OpenApiParameter) | ||
{ | ||
((OpenApiParameter)swaggerParam).Schema.Pattern = regex; | ||
} | ||
} | ||
|
||
// String Length [StringLength] | ||
int? minLenght = null, maxLength = null; | ||
var stringLengthAttr = attributes.FirstOrDefault(p => p.AttributeType == typeof(StringLengthAttribute)); | ||
if (stringLengthAttr != null) | ||
{ | ||
if (stringLengthAttr.NamedArguments.Count == 1) | ||
{ | ||
minLenght = (int)stringLengthAttr.NamedArguments.Single(p => p.MemberName == "MinimumLength").TypedValue.Value; | ||
} | ||
maxLength = (int)stringLengthAttr.ConstructorArguments[0].Value; | ||
} | ||
|
||
var minLengthAttr = attributes.FirstOrDefault(p => p.AttributeType == typeof(MinLengthAttribute)); | ||
if (minLengthAttr != null) | ||
{ | ||
minLenght = (int)minLengthAttr.ConstructorArguments[0].Value; | ||
} | ||
|
||
var maxLengthAttr = attributes.FirstOrDefault(p => p.AttributeType == typeof(MaxLengthAttribute)); | ||
if (maxLengthAttr != null) | ||
{ | ||
maxLength = (int)maxLengthAttr.ConstructorArguments[0].Value; | ||
} | ||
|
||
if (swaggerParam is OpenApiParameter) | ||
{ | ||
((OpenApiParameter)swaggerParam).Schema.MinLength = minLenght; | ||
((OpenApiParameter)swaggerParam).Schema.MaxLength = maxLength; | ||
} | ||
|
||
// Range [Range] | ||
var rangeAttr = attributes.FirstOrDefault(p => p.AttributeType == typeof(RangeAttribute)); | ||
if (rangeAttr != null) | ||
{ | ||
int rangeMin = (int)rangeAttr.ConstructorArguments[0].Value; | ||
int rangeMax = (int)rangeAttr.ConstructorArguments[1].Value; | ||
if (swaggerParam is OpenApiParameter) | ||
{ | ||
((OpenApiParameter)swaggerParam).Schema.Minimum = rangeMin; | ||
((OpenApiParameter)swaggerParam).Schema.Maximum = rangeMax; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
29 changes: 29 additions & 0 deletions
29
modules/swagger-codegen/src/main/resources/aspnetcore/3.0/Program.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore; | ||
|
||
namespace {{packageName}} | ||
{ | ||
/// <summary> | ||
/// Program | ||
/// </summary> | ||
public class Program | ||
{ | ||
/// <summary> | ||
/// Main | ||
/// </summary> | ||
/// <param name="args"></param> | ||
public static void Main(string[] args) | ||
{ | ||
CreateWebHostBuilder(args).Build().Run(); | ||
} | ||
|
||
/// <summary> | ||
/// Create the web host builder. | ||
/// </summary> | ||
/// <param name="args"></param> | ||
/// <returns>IWebHostBuilder</returns> | ||
public static IWebHostBuilder CreateWebHostBuilder(string[] args) => | ||
WebHost.CreateDefaultBuilder(args) | ||
.UseStartup<Startup>(); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
modules/swagger-codegen/src/main/resources/aspnetcore/3.0/Project.csproj.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
<PropertyGroup> | ||
<Description>{{packageName}}</Description> | ||
<Copyright>{{packageName}}</Copyright> | ||
<TargetFramework>netcoreapp{{aspNetCoreVersion}}</TargetFramework> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<PreserveCompilationContext>true</PreserveCompilationContext> | ||
<AssemblyName>{{packageName}}</AssemblyName> | ||
<PackageId>{{packageName}}</PackageId> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc4"/> | ||
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="5.0.0-rc4"/> | ||
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="5.0.0-rc4"/> | ||
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.0.0-rc4" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.4" /> | ||
</ItemGroup> | ||
</Project> |
Oops, something went wrong.