Skip to content

Commit 50c9d26

Browse files
committed
added support for aspnet core 3
1 parent 889bdb8 commit 50c9d26

16 files changed

+905
-6
lines changed

src/main/java/io/swagger/codegen/v3/generators/dotnet/AspNetCoreServerCodegen.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class AspNetCoreServerCodegen extends AbstractCSharpCodegen {
3030
private static final String ASP_NET_CORE_VERSION_OPTION = "--aspnet-core-version";
3131
private static final String INTERFACE_ONLY_OPTION = "--interface-only";
3232
private static final String INTERFACE_CONTROLLER_OPTION = "--interface-controller";
33-
private final String DEFAULT_ASP_NET_CORE_VERSION = "2.2";
33+
private final String DEFAULT_ASP_NET_CORE_VERSION = "3.0";
3434
private String aspNetCoreVersion;
3535

3636
@SuppressWarnings("hiding")
@@ -146,13 +146,30 @@ public void processOpts() {
146146
supportingFiles.add(new SupportingFile("Program.mustache", packageFolder, "Program.cs"));
147147
supportingFiles.add(new SupportingFile("Project.csproj.mustache", packageFolder, this.packageName + ".csproj"));
148148
supportingFiles.add(new SupportingFile("Dockerfile.mustache", packageFolder, "Dockerfile"));
149-
} else{
149+
supportingFiles.add(new SupportingFile("Filters" + File.separator + "BasePathFilter.mustache", packageFolder + File.separator + "Filters", "BasePathFilter.cs"));
150+
supportingFiles.add(new SupportingFile("Filters" + File.separator + "GeneratePathParamsValidationFilter.mustache", packageFolder + File.separator + "Filters", "GeneratePathParamsValidationFilter.cs"));
151+
supportingFiles.add(new SupportingFile("Startup.mustache", packageFolder, "Startup.cs"));
152+
} else if (aspNetCoreVersion.equals("2.1")) {
150153
apiTemplateFiles.put("2.1/controller.mustache", ".cs");
151154
addInterfaceControllerTemplate();
152155

153156
supportingFiles.add(new SupportingFile("2.1/Program.mustache", packageFolder, "Program.cs"));
154157
supportingFiles.add(new SupportingFile("2.1/Project.csproj.mustache", packageFolder, this.packageName + ".csproj"));
155158
supportingFiles.add(new SupportingFile("2.1/Dockerfile.mustache", packageFolder, "Dockerfile"));
159+
supportingFiles.add(new SupportingFile("Filters" + File.separator + "BasePathFilter.mustache", packageFolder + File.separator + "Filters", "BasePathFilter.cs"));
160+
supportingFiles.add(new SupportingFile("Filters" + File.separator + "GeneratePathParamsValidationFilter.mustache", packageFolder + File.separator + "Filters", "GeneratePathParamsValidationFilter.cs"));
161+
supportingFiles.add(new SupportingFile("Startup.mustache", packageFolder, "Startup.cs"));
162+
} else {
163+
apiTemplateFiles.put("3.0/controller.mustache", ".cs");
164+
addInterfaceControllerTemplate();
165+
166+
supportingFiles.add(new SupportingFile("3.0" + File.separator + "Filters" + File.separator + "BasePathFilter.mustache", packageFolder + File.separator + "Filters", "BasePathFilter.cs"));
167+
supportingFiles.add(new SupportingFile("3.0" + File.separator + "Filters" + File.separator + "GeneratePathParamsValidationFilter.mustache", packageFolder + File.separator + "Filters", "GeneratePathParamsValidationFilter.cs"));
168+
169+
supportingFiles.add(new SupportingFile("3.0/Startup.mustache", packageFolder, "Startup.cs"));
170+
supportingFiles.add(new SupportingFile("3.0/Program.mustache", packageFolder, "Program.cs"));
171+
supportingFiles.add(new SupportingFile("3.0/Project.csproj.mustache", packageFolder, this.packageName + ".csproj"));
172+
supportingFiles.add(new SupportingFile("3.0/Dockerfile.mustache", packageFolder, "Dockerfile"));
156173
}
157174

158175
if (!additionalProperties.containsKey(CodegenConstants.API_PACKAGE)) {
@@ -173,15 +190,15 @@ public void processOpts() {
173190
supportingFiles.add(new SupportingFile("gitignore", packageFolder, ".gitignore"));
174191
supportingFiles.add(new SupportingFile("appsettings.json", packageFolder, "appsettings.json"));
175192

176-
supportingFiles.add(new SupportingFile("Startup.mustache", packageFolder, "Startup.cs"));
193+
//supportingFiles.add(new SupportingFile("Startup.mustache", packageFolder, "Startup.cs"));
177194

178195
supportingFiles.add(new SupportingFile("validateModel.mustache", packageFolder + File.separator + "Attributes", "ValidateModelStateAttribute.cs"));
179196
supportingFiles.add(new SupportingFile("web.config", packageFolder, "web.config"));
180197

181198
supportingFiles.add(new SupportingFile("Properties" + File.separator + "launchSettings.json", packageFolder + File.separator + "Properties", "launchSettings.json"));
182199

183-
supportingFiles.add(new SupportingFile("Filters" + File.separator + "BasePathFilter.mustache", packageFolder + File.separator + "Filters", "BasePathFilter.cs"));
184-
supportingFiles.add(new SupportingFile("Filters" + File.separator + "GeneratePathParamsValidationFilter.mustache", packageFolder + File.separator + "Filters", "GeneratePathParamsValidationFilter.cs"));
200+
// supportingFiles.add(new SupportingFile("Filters" + File.separator + "BasePathFilter.mustache", packageFolder + File.separator + "Filters", "BasePathFilter.cs"));
201+
// supportingFiles.add(new SupportingFile("Filters" + File.separator + "GeneratePathParamsValidationFilter.mustache", packageFolder + File.separator + "Filters", "GeneratePathParamsValidationFilter.cs"));
185202

186203
supportingFiles.add(new SupportingFile("wwwroot" + File.separator + "README.md", packageFolder + File.separator + "wwwroot", "README.md"));
187204
supportingFiles.add(new SupportingFile("wwwroot" + File.separator + "index.html", packageFolder + File.separator + "wwwroot", "index.html"));

src/main/resources/arguments/aspnetcore.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
arguments:
22
- option: "--aspnet-core-version"
3-
description: "aspnetcore version to use, current options are: 2.0, 2.1 and 2.2 (default)"
3+
description: "aspnetcore version to use, current options are: 2.0, 2.1, 2.2 and 3.0 (default)"
44
type: "string"
55
- option: "--interface-only"
66
description: "creates interfaces controller only"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
FROM mcr.microsoft.com/dotnet/core/sdk:{{aspNetCoreVersion}} AS build-env
2+
WORKDIR /app
3+
4+
ENV DOTNET_CLI_TELEMETRY_OPTOUT 1
5+
6+
# copy csproj and restore as distinct layers
7+
COPY *.csproj ./
8+
RUN dotnet restore
9+
10+
# copy everything else and build
11+
COPY . ./
12+
RUN dotnet publish -c Release -o out
13+
14+
# build runtime image
15+
FROM mcr.microsoft.com/dotnet/core/aspnet:{{aspNetCoreVersion}}
16+
WORKDIR /app
17+
COPY --from=build-env /app/out .
18+
ENTRYPOINT ["dotnet", "{{packageName}}.dll"]
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System.Linq;
2+
using System.Text.RegularExpressions;
3+
using Swashbuckle.AspNetCore.Swagger;
4+
using Swashbuckle.AspNetCore.SwaggerGen;
5+
using Microsoft.OpenApi.Models;
6+
7+
namespace {{packageName}}.Filters
8+
{
9+
/// <summary>
10+
/// BasePath Document Filter sets BasePath property of Swagger and removes it from the individual URL paths
11+
/// </summary>
12+
public class BasePathFilter : IDocumentFilter
13+
{
14+
/// <summary>
15+
/// Constructor
16+
/// </summary>
17+
/// <param name="basePath">BasePath to remove from Operations</param>
18+
public BasePathFilter(string basePath)
19+
{
20+
BasePath = basePath;
21+
}
22+
23+
/// <summary>
24+
/// Gets the BasePath of the Swagger Doc
25+
/// </summary>
26+
/// <returns>The BasePath of the Swagger Doc</returns>
27+
public string BasePath { get; }
28+
29+
/// <summary>
30+
/// Apply the filter
31+
/// </summary>
32+
/// <param name="swaggerDoc">OpenApiDocument</param>
33+
/// <param name="context">FilterContext</param>
34+
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
35+
{
36+
swaggerDoc.Servers.Add(new OpenApiServer() { Url = this.BasePath });
37+
38+
var pathsToModify = swaggerDoc.Paths.Where(p => p.Key.StartsWith(this.BasePath)).ToList();
39+
40+
foreach (var path in pathsToModify)
41+
{
42+
if (path.Key.StartsWith(this.BasePath))
43+
{
44+
string newKey = Regex.Replace(path.Key, $"^{this.BasePath}", string.Empty);
45+
swaggerDoc.Paths.Remove(path.Key);
46+
swaggerDoc.Paths.Add(newKey, path.Value);
47+
}
48+
}
49+
}
50+
}
51+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using System.ComponentModel.DataAnnotations;
2+
using System.Linq;
3+
using Microsoft.AspNetCore.Mvc.Controllers;
4+
using Microsoft.OpenApi.Models;
5+
using Swashbuckle.AspNetCore.SwaggerGen;
6+
7+
namespace {{packageName}}.Filters
8+
{
9+
/// <summary>
10+
/// Path Parameter Validation Rules Filter
11+
/// </summary>
12+
public class GeneratePathParamsValidationFilter : IOperationFilter
13+
{
14+
/// <summary>
15+
/// Constructor
16+
/// </summary>
17+
/// <param name="operation">Operation</param>
18+
/// <param name="context">OperationFilterContext</param>
19+
public void Apply(OpenApiOperation operation, OperationFilterContext context)
20+
{
21+
var pars = context.ApiDescription.ParameterDescriptions;
22+
23+
foreach (var par in pars)
24+
{
25+
var swaggerParam = operation.Parameters.SingleOrDefault(p => p.Name == par.Name);
26+
27+
var attributes = ((ControllerParameterDescriptor)par.ParameterDescriptor).ParameterInfo.CustomAttributes;
28+
29+
if (attributes != null && attributes.Count() > 0 && swaggerParam != null)
30+
{
31+
// Required - [Required]
32+
var requiredAttr = attributes.FirstOrDefault(p => p.AttributeType == typeof(RequiredAttribute));
33+
if (requiredAttr != null)
34+
{
35+
swaggerParam.Required = true;
36+
}
37+
38+
// Regex Pattern [RegularExpression]
39+
var regexAttr = attributes.FirstOrDefault(p => p.AttributeType == typeof(RegularExpressionAttribute));
40+
if (regexAttr != null)
41+
{
42+
string regex = (string)regexAttr.ConstructorArguments[0].Value;
43+
if (swaggerParam is OpenApiParameter)
44+
{
45+
((OpenApiParameter)swaggerParam).Schema.Pattern = regex;
46+
}
47+
}
48+
49+
// String Length [StringLength]
50+
int? minLenght = null, maxLength = null;
51+
var stringLengthAttr = attributes.FirstOrDefault(p => p.AttributeType == typeof(StringLengthAttribute));
52+
if (stringLengthAttr != null)
53+
{
54+
if (stringLengthAttr.NamedArguments.Count == 1)
55+
{
56+
minLenght = (int)stringLengthAttr.NamedArguments.Single(p => p.MemberName == "MinimumLength").TypedValue.Value;
57+
}
58+
maxLength = (int)stringLengthAttr.ConstructorArguments[0].Value;
59+
}
60+
61+
var minLengthAttr = attributes.FirstOrDefault(p => p.AttributeType == typeof(MinLengthAttribute));
62+
if (minLengthAttr != null)
63+
{
64+
minLenght = (int)minLengthAttr.ConstructorArguments[0].Value;
65+
}
66+
67+
var maxLengthAttr = attributes.FirstOrDefault(p => p.AttributeType == typeof(MaxLengthAttribute));
68+
if (maxLengthAttr != null)
69+
{
70+
maxLength = (int)maxLengthAttr.ConstructorArguments[0].Value;
71+
}
72+
73+
if (swaggerParam is OpenApiParameter)
74+
{
75+
((OpenApiParameter)swaggerParam).Schema.MinLength = minLenght;
76+
((OpenApiParameter)swaggerParam).Schema.MaxLength = maxLength;
77+
}
78+
79+
// Range [Range]
80+
var rangeAttr = attributes.FirstOrDefault(p => p.AttributeType == typeof(RangeAttribute));
81+
if (rangeAttr != null)
82+
{
83+
int rangeMin = (int)rangeAttr.ConstructorArguments[0].Value;
84+
int rangeMax = (int)rangeAttr.ConstructorArguments[1].Value;
85+
86+
if (swaggerParam is OpenApiParameter)
87+
{
88+
((OpenApiParameter)swaggerParam).Schema.Minimum = rangeMin;
89+
((OpenApiParameter)swaggerParam).Schema.Maximum = rangeMax;
90+
}
91+
}
92+
}
93+
}
94+
}
95+
}
96+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Microsoft.AspNetCore.Hosting;
2+
using Microsoft.AspNetCore;
3+
4+
namespace {{packageName}}
5+
{
6+
/// <summary>
7+
/// Program
8+
/// </summary>
9+
public class Program
10+
{
11+
/// <summary>
12+
/// Main
13+
/// </summary>
14+
/// <param name="args"></param>
15+
public static void Main(string[] args)
16+
{
17+
CreateWebHostBuilder(args).Build().Run();
18+
}
19+
20+
/// <summary>
21+
/// Create the web host builder.
22+
/// </summary>
23+
/// <param name="args"></param>
24+
/// <returns>IWebHostBuilder</returns>
25+
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
26+
WebHost.CreateDefaultBuilder(args)
27+
.UseStartup<Startup>();
28+
}
29+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
<PropertyGroup>
3+
<Description>{{packageName}}</Description>
4+
<Copyright>{{packageName}}</Copyright>
5+
<TargetFramework>netcoreapp{{aspNetCoreVersion}}</TargetFramework>
6+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
7+
<PreserveCompilationContext>true</PreserveCompilationContext>
8+
<AssemblyName>{{packageName}}</AssemblyName>
9+
<PackageId>{{packageName}}</PackageId>
10+
</PropertyGroup>
11+
<ItemGroup>
12+
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc4"/>
13+
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="5.0.0-rc4"/>
14+
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="5.0.0-rc4"/>
15+
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.0.0-rc4" />
16+
</ItemGroup>
17+
<ItemGroup>
18+
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.4" />
19+
</ItemGroup>
20+
</Project>

0 commit comments

Comments
 (0)