forked from OpenAPITools/openapi-generator
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update the Asp.Net core server generator to support Asp.net Core 2.1 (O…
…penAPITools#1008) * Update the .net core sdk to v2.1 and update the asp.net packages used. * Upgrade the SwashBuckle Asp.Net Core package to v3.0.0. * Update the AppSettings json file and add a file used in development. Also, remove the web.config file. * Update the program template to use the web host builder class. * Update the startup class to use the Asp.Net 2.1 paradigms. * Update the launch settings json file. * Update the controller template to derive from the ControllerBase class as aposed to Controller. * Add the SwashBuckle annotations package. * Add the SwashBuckle.AspNetCore.Annotations namespace to the controller template. * Update the Startup template to add comments to the Configuration property and an env parameter to the Configure method. * Update the startup class so we don't need to inject the hosting environment. * Update the program class to have hte recommended CreateWebHostBuilder method from asp.net core 2.1. * Update the asp.net core pet store sample server. * add back aspnetcore 2.0 template via option * remove web.config for aspnet core 2.1
- Loading branch information
1 parent
634defb
commit 7de0bf0
Showing
75 changed files
with
1,098 additions
and
49 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
18 changes: 18 additions & 0 deletions
18
modules/openapi-generator/src/main/resources/aspnetcore/2.1/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 microsoft/aspnetcore-build:2.0 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 microsoft/aspnetcore:2.0 | ||
WORKDIR /app | ||
COPY --from=build-env /app/out . | ||
ENTRYPOINT ["dotnet", "{{packageName}}.dll"] |
50 changes: 50 additions & 0 deletions
50
modules/openapi-generator/src/main/resources/aspnetcore/2.1/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,50 @@ | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
using Swashbuckle.AspNetCore.Swagger; | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
|
||
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">SwaggerDocument</param> | ||
/// <param name="context">FilterContext</param> | ||
public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context) | ||
{ | ||
swaggerDoc.BasePath = BasePath; | ||
var pathsToModify = swaggerDoc.Paths.Where(p => p.Key.StartsWith(BasePath)).ToList(); | ||
foreach (var path in pathsToModify) | ||
{ | ||
if (path.Key.StartsWith(BasePath)) | ||
{ | ||
string newKey = Regex.Replace(path.Key, $"^{BasePath}", string.Empty); | ||
swaggerDoc.Paths.Remove(path.Key); | ||
swaggerDoc.Paths.Add(newKey, path.Value); | ||
} | ||
} | ||
} | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
...tor/src/main/resources/aspnetcore/2.1/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,97 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Linq; | ||
using Microsoft.AspNetCore.Mvc.Controllers; | ||
using Swashbuckle.AspNetCore.Swagger; | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
|
||
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(Operation 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 NonBodyParameter) | ||
{ | ||
((NonBodyParameter)swaggerParam).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 NonBodyParameter) | ||
{ | ||
((NonBodyParameter)swaggerParam).MinLength = minLenght; | ||
((NonBodyParameter)swaggerParam).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 NonBodyParameter) | ||
{ | ||
((NonBodyParameter)swaggerParam).Minimum = rangeMin; | ||
((NonBodyParameter)swaggerParam).Maximum = rangeMax; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
30 changes: 30 additions & 0 deletions
30
modules/openapi-generator/src/main/resources/aspnetcore/2.1/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,30 @@ | ||
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>() | ||
.UseUrls("http://0.0.0.0:{{#serverPort}}{{serverPort}}{{/serverPort}}{{^serverPort}}8080{{/serverPort}}/"); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
modules/openapi-generator/src/main/resources/aspnetcore/2.1/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,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
<PropertyGroup> | ||
<Description>{{packageName}}</Description> | ||
<Copyright>{{packageName}}</Copyright> | ||
<TargetFramework>netcoreapp2.1</TargetFramework> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<PreserveCompilationContext>true</PreserveCompilationContext> | ||
<AssemblyName>{{packageName}}</AssemblyName> | ||
<PackageId>{{packageName}}</PackageId> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.App" /> | ||
{{#useSwashbuckle}} | ||
<PackageReference Include="Swashbuckle.AspNetCore" Version="3.0.0"/> | ||
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="3.0.0" /> | ||
{{/useSwashbuckle}} | ||
</ItemGroup> | ||
<ItemGroup> | ||
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.3" /> | ||
</ItemGroup> | ||
</Project> |
30 changes: 30 additions & 0 deletions
30
modules/openapi-generator/src/main/resources/aspnetcore/2.1/Properties/launchSettings.json
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,30 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/launchsettings.json", | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:61788", | ||
"sslPort": 44301 | ||
} | ||
}, | ||
"profiles": { | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"launchUrl": "api/values", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"WebApplication1": { | ||
"commandName": "Project", | ||
"launchBrowser": true, | ||
"launchUrl": "api/values", | ||
"applicationUrl": "https://localhost:5001;http://localhost:5000", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.