diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetCoreServerCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetCoreServerCodegen.java index 2ae16f32581f..fe873112901d 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetCoreServerCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetCoreServerCodegen.java @@ -28,6 +28,7 @@ import org.slf4j.LoggerFactory; import java.io.File; +import java.lang.IllegalArgumentException; import java.net.URL; import java.util.Arrays; import java.util.Locale; @@ -38,6 +39,7 @@ public class AspNetCoreServerCodegen extends AbstractCSharpCodegen { public static final String USE_SWASHBUCKLE = "useSwashbuckle"; + public static final String ASPNET_CORE_VERSION = "aspnetCoreVersion"; private String packageGuid = "{" + randomUUID().toString().toUpperCase(Locale.ROOT) + "}"; @@ -47,7 +49,7 @@ public class AspNetCoreServerCodegen extends AbstractCSharpCodegen { private boolean useSwashbuckle = true; protected int serverPort = 8080; protected String serverHost = "0.0.0.0"; - + protected String aspnetCoreVersion= "2.1"; // default to 2.1 public AspNetCoreServerCodegen() { super(); @@ -57,6 +59,8 @@ public AspNetCoreServerCodegen() { modelTemplateFiles.put("model.mustache", ".cs"); apiTemplateFiles.put("controller.mustache", ".cs"); + embeddedTemplateDir = templateDir = "aspnetcore/2.1"; + // contextually reserved words // NOTE: C# uses camel cased reserved words, while models are title cased. We don't want lowercase comparisons. reservedWords.addAll( @@ -82,6 +86,10 @@ public AspNetCoreServerCodegen() { CodegenConstants.SOURCE_FOLDER_DESC, sourceFolder); + addOption(ASPNET_CORE_VERSION, + "ASP.NET Core version: 2.1 (default), 2.0 (deprecated)", + aspnetCoreVersion); + // CLI Switches addSwitch(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG, CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG_DESC, @@ -102,6 +110,7 @@ public AspNetCoreServerCodegen() { addSwitch(USE_SWASHBUCKLE, "Uses the Swashbuckle.AspNetCore NuGet package for documentation.", useSwashbuckle); + } @Override @@ -118,6 +127,7 @@ public String getName() { public String getHelp() { return "Generates an ASP.NET Core Web API server."; } + @Override public void preprocessOpenAPI(OpenAPI openAPI) { super.preprocessOpenAPI(openAPI); @@ -141,6 +151,11 @@ public void processOpts() { additionalProperties.put(USE_SWASHBUCKLE, useSwashbuckle); } + // determine the ASP.NET core version setting + if (additionalProperties.containsKey(ASPNET_CORE_VERSION)) { + setAspnetCoreVersion((String) additionalProperties.get(ASPNET_CORE_VERSION)); + } + additionalProperties.put("dockerTag", packageName.toLowerCase(Locale.ROOT)); apiPackage = packageName + ".Controllers"; @@ -148,6 +163,17 @@ public void processOpts() { String packageFolder = sourceFolder + File.separator + packageName; + if ("2.0".equals(aspnetCoreVersion)) { + embeddedTemplateDir = templateDir = "aspnetcore/2.0"; + supportingFiles.add(new SupportingFile("web.config", packageFolder, "web.config")); + LOGGER.info("ASP.NET core version: 2.0"); + } else if ("2.1".equals(aspnetCoreVersion)) { + // default, do nothing + LOGGER.info("ASP.NET core version: 2.1"); + } else { + throw new IllegalArgumentException("aspnetCoreVersion must be '2.1', '2.0' but found " + aspnetCoreVersion); + } + supportingFiles.add(new SupportingFile("build.sh.mustache", "", "build.sh")); supportingFiles.add(new SupportingFile("build.bat.mustache", "", "build.bat")); supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")); @@ -159,28 +185,34 @@ public void processOpts() { supportingFiles.add(new SupportingFile("Startup.mustache", packageFolder, "Startup.cs")); supportingFiles.add(new SupportingFile("Program.mustache", packageFolder, "Program.cs")); supportingFiles.add(new SupportingFile("validateModel.mustache", packageFolder + File.separator + "Attributes", "ValidateModelStateAttribute.cs")); - supportingFiles.add(new SupportingFile("web.config", packageFolder, "web.config")); - supportingFiles.add(new SupportingFile("Project.csproj.mustache", packageFolder, packageName + ".csproj")); - supportingFiles.add(new SupportingFile("Properties" + File.separator + "launchSettings.json", packageFolder + File.separator + "Properties", "launchSettings.json")); + supportingFiles.add(new SupportingFile("Properties" + File.separator + "launchSettings.json", + packageFolder + File.separator + "Properties", "launchSettings.json")); if (useSwashbuckle) { - supportingFiles.add(new SupportingFile("Filters" + File.separator + "BasePathFilter.mustache", packageFolder + File.separator + "Filters", "BasePathFilter.cs")); - supportingFiles.add(new SupportingFile("Filters" + File.separator + "GeneratePathParamsValidationFilter.mustache", packageFolder + File.separator + "Filters", "GeneratePathParamsValidationFilter.cs")); + supportingFiles.add(new SupportingFile("Filters" + File.separator + "BasePathFilter.mustache", + packageFolder + File.separator + "Filters", "BasePathFilter.cs")); + supportingFiles.add(new SupportingFile("Filters" + File.separator + "GeneratePathParamsValidationFilter.mustache", + packageFolder + File.separator + "Filters", "GeneratePathParamsValidationFilter.cs")); } supportingFiles.add(new SupportingFile("wwwroot" + File.separator + "README.md", packageFolder + File.separator + "wwwroot", "README.md")); supportingFiles.add(new SupportingFile("wwwroot" + File.separator + "index.html", packageFolder + File.separator + "wwwroot", "index.html")); supportingFiles.add(new SupportingFile("wwwroot" + File.separator + "web.config", packageFolder + File.separator + "wwwroot", "web.config")); - supportingFiles.add(new SupportingFile("wwwroot" + File.separator + "openapi-original.mustache", packageFolder + File.separator + "wwwroot", "openapi-original.json")); + supportingFiles.add(new SupportingFile("wwwroot" + File.separator + "openapi-original.mustache", + packageFolder + File.separator + "wwwroot", "openapi-original.json")); } public void setPackageGuid(String packageGuid) { this.packageGuid = packageGuid; } + public void setAspnetCoreVersion(String aspnetCoreVersion) { + this.aspnetCoreVersion= aspnetCoreVersion; + } + @Override public String apiFileFolder() { return outputFolder + File.separator + sourceFolder + File.separator + packageName + File.separator + "Controllers"; diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/Dockerfile.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/Dockerfile.mustache similarity index 100% rename from modules/openapi-generator/src/main/resources/aspnetcore/Dockerfile.mustache rename to modules/openapi-generator/src/main/resources/aspnetcore/2.0/Dockerfile.mustache diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/Filters/BasePathFilter.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/Filters/BasePathFilter.mustache similarity index 100% rename from modules/openapi-generator/src/main/resources/aspnetcore/Filters/BasePathFilter.mustache rename to modules/openapi-generator/src/main/resources/aspnetcore/2.0/Filters/BasePathFilter.mustache diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/Filters/GeneratePathParamsValidationFilter.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/Filters/GeneratePathParamsValidationFilter.mustache similarity index 100% rename from modules/openapi-generator/src/main/resources/aspnetcore/Filters/GeneratePathParamsValidationFilter.mustache rename to modules/openapi-generator/src/main/resources/aspnetcore/2.0/Filters/GeneratePathParamsValidationFilter.mustache diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/Program.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/Program.mustache similarity index 100% rename from modules/openapi-generator/src/main/resources/aspnetcore/Program.mustache rename to modules/openapi-generator/src/main/resources/aspnetcore/2.0/Program.mustache diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/Project.csproj.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/Project.csproj.mustache similarity index 100% rename from modules/openapi-generator/src/main/resources/aspnetcore/Project.csproj.mustache rename to modules/openapi-generator/src/main/resources/aspnetcore/2.0/Project.csproj.mustache diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/Properties/launchSettings.json b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/Properties/launchSettings.json similarity index 100% rename from modules/openapi-generator/src/main/resources/aspnetcore/Properties/launchSettings.json rename to modules/openapi-generator/src/main/resources/aspnetcore/2.0/Properties/launchSettings.json diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/README.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/README.mustache similarity index 100% rename from modules/openapi-generator/src/main/resources/aspnetcore/README.mustache rename to modules/openapi-generator/src/main/resources/aspnetcore/2.0/README.mustache diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/Solution.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/Solution.mustache similarity index 100% rename from modules/openapi-generator/src/main/resources/aspnetcore/Solution.mustache rename to modules/openapi-generator/src/main/resources/aspnetcore/2.0/Solution.mustache diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/Startup.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/Startup.mustache similarity index 100% rename from modules/openapi-generator/src/main/resources/aspnetcore/Startup.mustache rename to modules/openapi-generator/src/main/resources/aspnetcore/2.0/Startup.mustache diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/appsettings.json b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/appsettings.json similarity index 100% rename from modules/openapi-generator/src/main/resources/aspnetcore/appsettings.json rename to modules/openapi-generator/src/main/resources/aspnetcore/2.0/appsettings.json diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/bodyParam.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/bodyParam.mustache similarity index 100% rename from modules/openapi-generator/src/main/resources/aspnetcore/bodyParam.mustache rename to modules/openapi-generator/src/main/resources/aspnetcore/2.0/bodyParam.mustache diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/build.bat.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/build.bat.mustache similarity index 100% rename from modules/openapi-generator/src/main/resources/aspnetcore/build.bat.mustache rename to modules/openapi-generator/src/main/resources/aspnetcore/2.0/build.bat.mustache diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/build.sh.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/build.sh.mustache similarity index 100% rename from modules/openapi-generator/src/main/resources/aspnetcore/build.sh.mustache rename to modules/openapi-generator/src/main/resources/aspnetcore/2.0/build.sh.mustache diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/controller.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/controller.mustache similarity index 100% rename from modules/openapi-generator/src/main/resources/aspnetcore/controller.mustache rename to modules/openapi-generator/src/main/resources/aspnetcore/2.0/controller.mustache diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/enumClass.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/enumClass.mustache similarity index 100% rename from modules/openapi-generator/src/main/resources/aspnetcore/enumClass.mustache rename to modules/openapi-generator/src/main/resources/aspnetcore/2.0/enumClass.mustache diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/formParam.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/formParam.mustache similarity index 100% rename from modules/openapi-generator/src/main/resources/aspnetcore/formParam.mustache rename to modules/openapi-generator/src/main/resources/aspnetcore/2.0/formParam.mustache diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/gitignore b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/gitignore similarity index 100% rename from modules/openapi-generator/src/main/resources/aspnetcore/gitignore rename to modules/openapi-generator/src/main/resources/aspnetcore/2.0/gitignore diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/headerParam.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/headerParam.mustache similarity index 100% rename from modules/openapi-generator/src/main/resources/aspnetcore/headerParam.mustache rename to modules/openapi-generator/src/main/resources/aspnetcore/2.0/headerParam.mustache diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/listReturn.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/listReturn.mustache similarity index 100% rename from modules/openapi-generator/src/main/resources/aspnetcore/listReturn.mustache rename to modules/openapi-generator/src/main/resources/aspnetcore/2.0/listReturn.mustache diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/mapReturn.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/mapReturn.mustache similarity index 100% rename from modules/openapi-generator/src/main/resources/aspnetcore/mapReturn.mustache rename to modules/openapi-generator/src/main/resources/aspnetcore/2.0/mapReturn.mustache diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/model.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/model.mustache similarity index 100% rename from modules/openapi-generator/src/main/resources/aspnetcore/model.mustache rename to modules/openapi-generator/src/main/resources/aspnetcore/2.0/model.mustache diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/objectReturn.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/objectReturn.mustache similarity index 100% rename from modules/openapi-generator/src/main/resources/aspnetcore/objectReturn.mustache rename to modules/openapi-generator/src/main/resources/aspnetcore/2.0/objectReturn.mustache diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/partial_header.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/partial_header.mustache similarity index 100% rename from modules/openapi-generator/src/main/resources/aspnetcore/partial_header.mustache rename to modules/openapi-generator/src/main/resources/aspnetcore/2.0/partial_header.mustache diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/pathParam.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/pathParam.mustache similarity index 100% rename from modules/openapi-generator/src/main/resources/aspnetcore/pathParam.mustache rename to modules/openapi-generator/src/main/resources/aspnetcore/2.0/pathParam.mustache diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/queryParam.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/queryParam.mustache similarity index 100% rename from modules/openapi-generator/src/main/resources/aspnetcore/queryParam.mustache rename to modules/openapi-generator/src/main/resources/aspnetcore/2.0/queryParam.mustache diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/tags.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/tags.mustache similarity index 100% rename from modules/openapi-generator/src/main/resources/aspnetcore/tags.mustache rename to modules/openapi-generator/src/main/resources/aspnetcore/2.0/tags.mustache diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/validateModel.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/validateModel.mustache similarity index 100% rename from modules/openapi-generator/src/main/resources/aspnetcore/validateModel.mustache rename to modules/openapi-generator/src/main/resources/aspnetcore/2.0/validateModel.mustache diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/web.config b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/web.config similarity index 100% rename from modules/openapi-generator/src/main/resources/aspnetcore/web.config rename to modules/openapi-generator/src/main/resources/aspnetcore/2.0/web.config diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/wwwroot/README.md b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/wwwroot/README.md similarity index 100% rename from modules/openapi-generator/src/main/resources/aspnetcore/wwwroot/README.md rename to modules/openapi-generator/src/main/resources/aspnetcore/2.0/wwwroot/README.md diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/wwwroot/index.html b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/wwwroot/index.html similarity index 100% rename from modules/openapi-generator/src/main/resources/aspnetcore/wwwroot/index.html rename to modules/openapi-generator/src/main/resources/aspnetcore/2.0/wwwroot/index.html diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/wwwroot/openapi-original.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/wwwroot/openapi-original.mustache similarity index 100% rename from modules/openapi-generator/src/main/resources/aspnetcore/wwwroot/openapi-original.mustache rename to modules/openapi-generator/src/main/resources/aspnetcore/2.0/wwwroot/openapi-original.mustache diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/wwwroot/web.config b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/wwwroot/web.config similarity index 100% rename from modules/openapi-generator/src/main/resources/aspnetcore/wwwroot/web.config rename to modules/openapi-generator/src/main/resources/aspnetcore/2.0/wwwroot/web.config diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/Dockerfile.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/Dockerfile.mustache new file mode 100644 index 000000000000..e9d80c52833b --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/Dockerfile.mustache @@ -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"] diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/Filters/BasePathFilter.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/Filters/BasePathFilter.mustache new file mode 100644 index 000000000000..a3e1b9ce0804 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/Filters/BasePathFilter.mustache @@ -0,0 +1,50 @@ +using System.Linq; +using System.Text.RegularExpressions; +using Swashbuckle.AspNetCore.Swagger; +using Swashbuckle.AspNetCore.SwaggerGen; + +namespace {{packageName}}.Filters +{ + /// + /// BasePath Document Filter sets BasePath property of Swagger and removes it from the individual URL paths + /// + public class BasePathFilter : IDocumentFilter + { + /// + /// Constructor + /// + /// BasePath to remove from Operations + public BasePathFilter(string basePath) + { + BasePath = basePath; + } + + /// + /// Gets the BasePath of the Swagger Doc + /// + /// The BasePath of the Swagger Doc + public string BasePath { get; } + + /// + /// Apply the filter + /// + /// SwaggerDocument + /// FilterContext + 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); + } + } + } + } +} diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/Filters/GeneratePathParamsValidationFilter.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/Filters/GeneratePathParamsValidationFilter.mustache new file mode 100644 index 000000000000..d857a4a0f960 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/Filters/GeneratePathParamsValidationFilter.mustache @@ -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 +{ + /// + /// Path Parameter Validation Rules Filter + /// + public class GeneratePathParamsValidationFilter : IOperationFilter + { + /// + /// Constructor + /// + /// Operation + /// OperationFilterContext + 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; + } + } + } + } + } + } +} + diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/Program.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/Program.mustache new file mode 100644 index 000000000000..73cb81a3e411 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/Program.mustache @@ -0,0 +1,30 @@ +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore; + +namespace {{packageName}} +{ + /// + /// Program + /// + public class Program + { + /// + /// Main + /// + /// + public static void Main(string[] args) + { + CreateWebHostBuilder(args).Build().Run(); + } + + /// + /// Create the web host builder. + /// + /// + /// IWebHostBuilder + public static IWebHostBuilder CreateWebHostBuilder(string[] args) => + WebHost.CreateDefaultBuilder(args) + .UseStartup() + .UseUrls("http://0.0.0.0:{{#serverPort}}{{serverPort}}{{/serverPort}}{{^serverPort}}8080{{/serverPort}}/"); + } +} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/Project.csproj.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/Project.csproj.mustache new file mode 100644 index 000000000000..faa7dbd53b56 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/Project.csproj.mustache @@ -0,0 +1,21 @@ + + + {{packageName}} + {{packageName}} + netcoreapp2.1 + true + true + {{packageName}} + {{packageName}} + + + +{{#useSwashbuckle}} + + +{{/useSwashbuckle}} + + + + + diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/Properties/launchSettings.json b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/Properties/launchSettings.json new file mode 100644 index 000000000000..1b527df2c2b3 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/Properties/launchSettings.json @@ -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" + } + } + } +} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/README.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/README.mustache new file mode 100644 index 000000000000..71c4cfebb8b3 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/README.mustache @@ -0,0 +1,27 @@ +# {{packageName}} - ASP.NET Core 2.0 Server + +{{#appDescription}} +{{{appDescription}}} +{{/appDescription}} + +## Run + +Linux/OS X: + +``` +sh build.sh +``` + +Windows: + +``` +build.bat +``` + +## Run in Docker + +``` +cd {{sourceFolder}}/{{packageName}} +docker build -t {{dockerTag}} . +docker run -p 5000:5000 {{dockerTag}} +``` diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/Solution.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/Solution.mustache new file mode 100644 index 000000000000..8c6d69ea93da --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/Solution.mustache @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27428.2043 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "{{packageName}}", "{{sourceFolder}}\{{packageName}}\{{packageName}}.csproj", "{{packageGuid}}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {{packageGuid}}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {{packageGuid}}.Debug|Any CPU.Build.0 = Debug|Any CPU + {{packageGuid}}.Release|Any CPU.ActiveCfg = Release|Any CPU + {{packageGuid}}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/Startup.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/Startup.mustache new file mode 100644 index 000000000000..7a488bfcf170 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/Startup.mustache @@ -0,0 +1,120 @@ +{{>partial_header}} +using System; +using System.IO; +using System.Reflection; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Newtonsoft.Json.Converters; +using Newtonsoft.Json.Serialization;{{#useSwashbuckle}} +using Swashbuckle.AspNetCore.Swagger; +using Swashbuckle.AspNetCore.SwaggerGen; +using {{packageName}}.Filters;{{/useSwashbuckle}} + +namespace {{packageName}} +{ + /// + /// Startup + /// + public class Startup + { + /// + /// Constructor + /// + /// + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + /// + /// The application configuration. + /// + public IConfiguration Configuration { get; } + + /// + /// This method gets called by the runtime. Use this method to add services to the container. + /// + /// + public void ConfigureServices(IServiceCollection services) + { + // Add framework services. + services + .AddMvc() + .SetCompatibilityVersion (CompatibilityVersion.Version_2_1) + .AddJsonOptions(opts => + { + opts.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); + opts.SerializerSettings.Converters.Add(new StringEnumConverter + { + CamelCaseText = true + }); + });{{#useSwashbuckle}} + + services + .AddSwaggerGen(c => + { + c.SwaggerDoc("{{#version}}{{{version}}}{{/version}}{{^version}}v1{{/version}}", new Info + { + Version = "{{#version}}{{{version}}}{{/version}}{{^version}}v1{{/version}}", + Title = "{{#appName}}{{{appName}}}{{/appName}}{{^appName}}{{packageName}}{{/appName}}", + Description = "{{#appName}}{{{appName}}}{{/appName}}{{^appName}}{{packageName}}{{/appName}} (ASP.NET Core 2.0)", + Contact = new Contact() + { + Name = "{{#infoName}}{{{infoName}}}{{/infoName}}{{^infoName}}OpenAPI-Generator Contributors{{/infoName}}", + Url = "{{#infoUrl}}{{{infoUrl}}}{{/infoUrl}}{{^infoUrl}}https://github.com/openapitools/openapi-generator{{/infoUrl}}", + Email = "{{#infoEmail}}{{{infoEmail}}}{{/infoEmail}}" + }, + TermsOfService = "{{#termsOfService}}{{{termsOfService}}}{{/termsOfService}}" + }); + c.CustomSchemaIds(type => type.FriendlyId(true)); + c.DescribeAllEnumsAsStrings(); + c.IncludeXmlComments($"{AppContext.BaseDirectory}{Path.DirectorySeparatorChar}{Assembly.GetEntryAssembly().GetName().Name}.xml"); + {{#basePathWithoutHost}} + // Sets the basePath property in the Swagger document generated + c.DocumentFilter("{{{basePathWithoutHost}}}"); + {{/basePathWithoutHost}} + + // Include DataAnnotation attributes on Controller Action parameters as Swagger validation rules (e.g required, pattern, ..) + // Use [ValidateModelState] on Actions to actually validate it in C# as well! + c.OperationFilter(); + });{{/useSwashbuckle}} + } + + /// + /// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + /// + /// + public void Configure(IApplicationBuilder app, IHostingEnvironment env) + { + app.UseHttpsRedirection(); + app + .UseMvc() + .UseDefaultFiles() + .UseStaticFiles(){{#useSwashbuckle}} + .UseSwagger(c => + { + c.RouteTemplate = "swagger/{documentName}/openapi.json"; + }) + .UseSwaggerUI(c => + { + //TODO: Either use the SwaggerGen generated Swagger contract (generated from C# classes) + c.SwaggerEndpoint("/swagger/{{#version}}{{{version}}}{{/version}}{{^version}}v1{{/version}}/openapi.json", "{{#appName}}{{{appName}}}{{/appName}}{{^appName}}{{packageName}}{{/appName}}"); + + //TODO: Or alternatively use the original Swagger contract that's included in the static files + // c.SwaggerEndpoint("/openapi-original.json", "{{#appName}}{{{appName}}}{{/appName}}{{^appName}}{{packageName}}{{/appName}} Original"); + }){{/useSwashbuckle}}; + +if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + else + { + app.UseHsts(); + } + } + } +} diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/appsettings.Development.json b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/appsettings.Development.json new file mode 100644 index 000000000000..e203e9407e74 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/appsettings.Development.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Debug", + "System": "Information", + "Microsoft": "Information" + } + } +} diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/appsettings.json b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/appsettings.json new file mode 100644 index 000000000000..def9159a7d94 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/appsettings.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/bodyParam.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/bodyParam.mustache new file mode 100644 index 000000000000..02b0fa1d2dea --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/bodyParam.mustache @@ -0,0 +1 @@ +{{#isBodyParam}}[FromBody]{{&dataType}} {{paramName}}{{/isBodyParam}} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/build.bat.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/build.bat.mustache new file mode 100644 index 000000000000..e437bccf7d6f --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/build.bat.mustache @@ -0,0 +1,9 @@ +:: Generated by: https://openapi-generator.tech +:: + +@echo off + +dotnet restore {{sourceFolder}}\{{packageName}} +dotnet build {{sourceFolder}}\{{packageName}} +echo Now, run the following to start the project: dotnet run -p {{sourceFolder}}\{{packageName}}\{{packageName}}.csproj --launch-profile web. +echo. diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/build.sh.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/build.sh.mustache new file mode 100644 index 000000000000..3804359d7e26 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/build.sh.mustache @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +# +# Generated by: https://openapi-generator.tech +# + +dotnet restore {{sourceFolder}}/{{packageName}}/ && \ + dotnet build {{sourceFolder}}/{{packageName}}/ && \ + echo "Now, run the following to start the project: dotnet run -p {{sourceFolder}}/{{packageName}}/{{packageName}}.csproj --launch-profile web" diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/controller.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/controller.mustache new file mode 100644 index 000000000000..1805b7d2cc6e --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/controller.mustache @@ -0,0 +1,55 @@ +{{>partial_header}} +using System; +using System.Collections.Generic; +using Microsoft.AspNetCore.Mvc;{{#useSwashbuckle}} +using Swashbuckle.AspNetCore.Annotations; +using Swashbuckle.AspNetCore.SwaggerGen;{{/useSwashbuckle}} +using Newtonsoft.Json; +using System.ComponentModel.DataAnnotations; +using {{packageName}}.Attributes; +using {{packageName}}.Models; + +namespace {{packageName}}.Controllers +{ {{#operations}} + /// + /// {{description}} + /// {{#description}} + [Description("{{description}}")]{{/description}} + public class {{classname}}Controller : ControllerBase + { {{#operation}} + /// + /// {{#summary}}{{summary}}{{/summary}} + /// {{#notes}} + /// {{notes}}{{/notes}}{{#allParams}} + /// {{description}}{{/allParams}}{{#responses}} + /// {{message}}{{/responses}} + [{{httpMethod}}] + [Route("{{{basePathWithoutHost}}}{{{path}}}")] + [ValidateModelState]{{#useSwashbuckle}} + [SwaggerOperation("{{operationId}}")]{{#responses}}{{#dataType}} + [SwaggerResponse(statusCode: {{code}}, type: typeof({{&dataType}}), description: "{{message}}")]{{/dataType}}{{^dataType}}{{/dataType}}{{/responses}}{{/useSwashbuckle}} + public virtual IActionResult {{operationId}}({{#allParams}}{{>pathParam}}{{>queryParam}}{{>bodyParam}}{{>formParam}}{{>headerParam}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) + { {{#responses}} +{{#dataType}} + //TODO: Uncomment the next line to return response {{code}} or use other options such as return this.NotFound(), return this.BadRequest(..), ... + // return StatusCode({{code}}, default({{&dataType}})); +{{/dataType}} +{{^dataType}} + //TODO: Uncomment the next line to return response {{code}} or use other options such as return this.NotFound(), return this.BadRequest(..), ... + // return StatusCode({{code}}); +{{/dataType}}{{/responses}} +{{#returnType}} + string exampleJson = null; + {{#examples}} + exampleJson = "{{{example}}}"; + {{/examples}} + {{#isListCollection}}{{>listReturn}}{{/isListCollection}}{{^isListCollection}}{{#isMapContainer}}{{>mapReturn}}{{/isMapContainer}}{{^isMapContainer}}{{>objectReturn}}{{/isMapContainer}}{{/isListCollection}} + {{!TODO: defaultResponse, examples, auth, consumes, produces, nickname, externalDocs, imports, security}} + //TODO: Change the data returned + return new ObjectResult(example);{{/returnType}}{{^returnType}} + throw new NotImplementedException();{{/returnType}} + } + {{/operation}} + } +{{/operations}} +} diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/enumClass.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/enumClass.mustache new file mode 100644 index 000000000000..a8a68b998449 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/enumClass.mustache @@ -0,0 +1,18 @@ + + /// + /// {{^description}}Gets or Sets {{{name}}}{{/description}}{{#description}}{{{description}}}{{/description}} + /// + {{#description}} + /// {{{description}}} + {{/description}} + {{#allowableValues}}{{#enumVars}}{{#-first}}{{#isString}}[JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]{{/isString}}{{/-first}}{{/enumVars}}{{/allowableValues}} + public enum {{#datatypeWithEnum}}{{.}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}} + { + {{#allowableValues}}{{#enumVars}} + /// + /// Enum {{name}} for {{{value}}} + /// + {{#isString}}[EnumMember(Value = "{{{value}}}")]{{/isString}} + {{name}}{{^isString}} = {{{value}}}{{/isString}}{{#isString}} = {{-index}}{{/isString}}{{^-last}}, + {{/-last}}{{/enumVars}}{{/allowableValues}} + } diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/formParam.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/formParam.mustache new file mode 100644 index 000000000000..2d42dc2916ba --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/formParam.mustache @@ -0,0 +1 @@ +{{#isFormParam}}[FromForm]{{#required}}[Required()]{{/required}}{{#pattern}}[RegularExpression("{{{pattern}}}")]{{/pattern}}{{#minLength}}{{#maxLength}}[StringLength({{maxLength}}, MinimumLength={{minLength}}){{/maxLength}}{{/minLength}}{{#minLength}}{{^maxLength}} [MinLength({{minLength}})]{{/maxLength}}{{/minLength}}{{^minLength}}{{#maxLength}} [MaxLength({{maxLength}})]{{/maxLength}}{{/minLength}}{{#minimum}}{{#maximum}}[Range({{minimum}}, {{maximum}})]{{/maximum}}{{/minimum}}{{&dataType}} {{paramName}}{{/isFormParam}} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/gitignore b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/gitignore new file mode 100644 index 000000000000..cd9b840e5498 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/gitignore @@ -0,0 +1,208 @@ +PID + +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +build/ +bld/ +[Bb]in/ +[Oo]bj/ + +# Visual Studio 2015 cache/options directory +.vs/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# DNX +project.lock.json +artifacts/ + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf +*.cachefile + +# Visual Studio profiler +*.psess +*.vsp +*.vspx + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +_NCrunch_* +.*crunch*.local.xml + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# TODO: Comment the next line if you want to checkin your web deploy settings +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config + +# Windows Azure Build Output +csx/ +*.build.csdef + +# Windows Store app package directory +AppPackages/ + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +[Ss]tyle[Cc]op.* +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.pfx +*.publishsettings +node_modules/ +bower_components/ +orleans.codegen.cs + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +*.mdf +*.ldf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/headerParam.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/headerParam.mustache new file mode 100644 index 000000000000..45a5be9d7b5b --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/headerParam.mustache @@ -0,0 +1 @@ +{{#isHeaderParam}}[FromHeader]{{#required}}[Required()]{{/required}}{{#pattern}}[RegularExpression("{{{pattern}}}")]{{/pattern}}{{#minLength}}{{#maxLength}}[StringLength({{maxLength}}, MinimumLength={{minLength}}){{/maxLength}}{{/minLength}}{{#minLength}}{{^maxLength}} [MinLength({{minLength}})]{{/maxLength}}{{/minLength}}{{^minLength}}{{#maxLength}} [MaxLength({{maxLength}})]{{/maxLength}}{{/minLength}}{{#minimum}}{{#maximum}}[Range({{minimum}}, {{maximum}})]{{/maximum}}{{/minimum}}{{&dataType}} {{paramName}}{{/isHeaderParam}} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/listReturn.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/listReturn.mustache new file mode 100644 index 000000000000..d609e67148c2 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/listReturn.mustache @@ -0,0 +1,4 @@ + + var example = exampleJson != null + ? JsonConvert.DeserializeObject<{{returnContainer}}<{{#returnType}}{{{returnType}}}{{/returnType}}>>(exampleJson) + : Enumerable.Empty<{{#returnType}}{{{returnType}}}{{/returnType}}>(); \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/mapReturn.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/mapReturn.mustache new file mode 100644 index 000000000000..856fb1b3507c --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/mapReturn.mustache @@ -0,0 +1,4 @@ + + var example = exampleJson != null + ? JsonConvert.DeserializeObject>(exampleJson) + : new Dictionary<{{#returnType}}{{{returnType}}}{{/returnType}}>(); \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/model.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/model.mustache new file mode 100644 index 000000000000..7b06958ef679 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/model.mustache @@ -0,0 +1,136 @@ +{{>partial_header}} +using System; +using System.Linq; +using System.Text; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Runtime.Serialization; +using Newtonsoft.Json; + +{{#models}} +{{#model}} +namespace {{packageName}}.Models +{ {{#isEnum}}{{>enumClass}}{{/isEnum}}{{^isEnum}} + /// + /// {{description}} + /// + [DataContract] + public partial class {{classname}} : {{#parent}}{{{parent}}}, {{/parent}}IEquatable<{{classname}}> + { {{#vars}}{{#isEnum}}{{>enumClass}}{{/isEnum}}{{#items.isEnum}}{{#items}}{{>enumClass}}{{/items}}{{/items.isEnum}} + /// + /// {{^description}}Gets or Sets {{{name}}}{{/description}}{{#description}}{{description}}{{/description}} + /// + {{#description}} + /// {{description}} + {{/description}} + {{#required}} + [Required] + {{/required}} + [DataMember(Name="{{baseName}}")] + {{#isEnum}} + public {{{datatypeWithEnum}}}{{#isEnum}}{{^isContainer}}?{{/isContainer}}{{/isEnum}} {{name}} { get; set; } + {{/isEnum}} + {{^isEnum}} + public {{{dataType}}} {{name}} { get; {{#isReadOnly}}private {{/isReadOnly}}set; } + {{/isEnum}} + {{#hasMore}} + {{/hasMore}} + {{/vars}} + + /// + /// Returns the string presentation of the object + /// + /// String presentation of the object + public override string ToString() + { + var sb = new StringBuilder(); + sb.Append("class {{classname}} {\n"); + {{#vars}} + sb.Append(" {{name}}: ").Append({{name}}).Append("\n"); + {{/vars}} + sb.Append("}\n"); + return sb.ToString(); + } + + /// + /// Returns the JSON string presentation of the object + /// + /// JSON string presentation of the object + public {{#parent}}{{^isMapModel}}{{^isArrayModel}}new {{/isArrayModel}}{{/isMapModel}}{{/parent}}string ToJson() + { + return JsonConvert.SerializeObject(this, Formatting.Indented); + } + + /// + /// Returns true if objects are equal + /// + /// Object to be compared + /// Boolean + public override bool Equals(object obj) + { + if (obj is null) return false; + if (ReferenceEquals(this, obj)) return true; + return obj.GetType() == GetType() && Equals(({{classname}})obj); + } + + /// + /// Returns true if {{classname}} instances are equal + /// + /// Instance of {{classname}} to be compared + /// Boolean + public bool Equals({{classname}} other) + { + if (other is null) return false; + if (ReferenceEquals(this, other)) return true; + + return {{#vars}}{{#isNotContainer}} + ( + {{name}} == other.{{name}} || + {{name}} != null && + {{name}}.Equals(other.{{name}}) + ){{#hasMore}} && {{/hasMore}}{{/isNotContainer}}{{^isNotContainer}} + ( + {{name}} == other.{{name}} || + {{name}} != null && + {{name}}.SequenceEqual(other.{{name}}) + ){{#hasMore}} && {{/hasMore}}{{/isNotContainer}}{{/vars}}{{^vars}}false{{/vars}}; + } + + /// + /// Gets the hash code + /// + /// Hash code + public override int GetHashCode() + { + unchecked // Overflow is fine, just wrap + { + var hashCode = 41; + // Suitable nullity checks etc, of course :) + {{#vars}} + if ({{name}} != null) + hashCode = hashCode * 59 + {{name}}.GetHashCode(); + {{/vars}} + return hashCode; + } + } + + #region Operators + #pragma warning disable 1591 + + public static bool operator ==({{classname}} left, {{classname}} right) + { + return Equals(left, right); + } + + public static bool operator !=({{classname}} left, {{classname}} right) + { + return !Equals(left, right); + } + + #pragma warning restore 1591 + #endregion Operators + } +{{/isEnum}} +{{/model}} +{{/models}} +} diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/objectReturn.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/objectReturn.mustache new file mode 100644 index 000000000000..4059a61ac0b8 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/objectReturn.mustache @@ -0,0 +1,4 @@ + + var example = exampleJson != null + ? JsonConvert.DeserializeObject<{{#returnType}}{{{returnType}}}{{/returnType}}>(exampleJson) + : default({{#returnType}}{{{returnType}}}{{/returnType}}); \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/partial_header.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/partial_header.mustache new file mode 100644 index 000000000000..4a682818a37a --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/partial_header.mustache @@ -0,0 +1,13 @@ +/* + {{#appName}} + * {{{appName}}} + * + {{/appName}} + {{#appDescription}} + * {{{appDescription}}} + * + {{/appDescription}} + * {{#version}}OpenAPI spec version: {{{version}}}{{/version}} + * {{#infoEmail}}Contact: {{{infoEmail}}}{{/infoEmail}} + * Generated by: https://openapi-generator.tech + */ diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/pathParam.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/pathParam.mustache new file mode 100644 index 000000000000..70303432d48d --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/pathParam.mustache @@ -0,0 +1 @@ +{{#isPathParam}}[FromRoute]{{#required}}[Required]{{/required}}{{#pattern}}[RegularExpression("{{{pattern}}}")]{{/pattern}}{{#minLength}}{{#maxLength}}[StringLength({{maxLength}}, MinimumLength={{minLength}}){{/maxLength}}{{/minLength}}{{#minLength}}{{^maxLength}} [MinLength({{minLength}})]{{/maxLength}}{{/minLength}}{{^minLength}}{{#maxLength}} [MaxLength({{maxLength}})]{{/maxLength}}{{/minLength}}{{#minimum}}{{#maximum}}[Range({{minimum}}, {{maximum}})]{{/maximum}}{{/minimum}}{{&dataType}} {{paramName}}{{/isPathParam}} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/queryParam.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/queryParam.mustache new file mode 100644 index 000000000000..e9fa09b005d3 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/queryParam.mustache @@ -0,0 +1 @@ +{{#isQueryParam}}[FromQuery]{{#required}}[Required()]{{/required}}{{#pattern}}[RegularExpression("{{{pattern}}}")]{{/pattern}}{{#minLength}}{{#maxLength}}[StringLength({{maxLength}}, MinimumLength={{minLength}}){{/maxLength}}{{/minLength}}{{#minLength}}{{^maxLength}} [MinLength({{minLength}})]{{/maxLength}}{{/minLength}}{{^minLength}}{{#maxLength}} [MaxLength({{maxLength}})]{{/maxLength}}{{/minLength}}{{#minimum}}{{#maximum}}[Range({{minimum}}, {{maximum}})]{{/maximum}}{{/minimum}}{{&dataType}} {{paramName}}{{/isQueryParam}} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/tags.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/tags.mustache new file mode 100644 index 000000000000..c97df19949e6 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/tags.mustache @@ -0,0 +1 @@ +{{!TODO: Need iterable tags object...}}{{#tags}}, Tags = new[] { {{/tags}}"{{#tags}}{{tag}} {{/tags}}"{{#tags}} }{{/tags}} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/validateModel.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/validateModel.mustache new file mode 100644 index 000000000000..e11aaa5d2708 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/validateModel.mustache @@ -0,0 +1,61 @@ +using System.ComponentModel.DataAnnotations; +using System.Reflection; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Controllers; +using Microsoft.AspNetCore.Mvc.Filters; +using Microsoft.AspNetCore.Mvc.ModelBinding; + +namespace {{packageName}}.Attributes +{ + /// + /// Model state validation attribute + /// + public class ValidateModelStateAttribute : ActionFilterAttribute + { + /// + /// Called before the action method is invoked + /// + /// + public override void OnActionExecuting(ActionExecutingContext context) + { + // Per https://blog.markvincze.com/how-to-validate-action-parameters-with-dataannotation-attributes/ + var descriptor = context.ActionDescriptor as ControllerActionDescriptor; + if (descriptor != null) + { + foreach (var parameter in descriptor.MethodInfo.GetParameters()) + { + object args = null; + if (context.ActionArguments.ContainsKey(parameter.Name)) + { + args = context.ActionArguments[parameter.Name]; + } + + ValidateAttributes(parameter, args, context.ModelState); + } + } + + if (!context.ModelState.IsValid) + { + context.Result = new BadRequestObjectResult(context.ModelState); + } + } + + private void ValidateAttributes(ParameterInfo parameter, object args, ModelStateDictionary modelState) + { + foreach (var attributeData in parameter.CustomAttributes) + { + var attributeInstance = parameter.GetCustomAttribute(attributeData.AttributeType); + + var validationAttribute = attributeInstance as ValidationAttribute; + if (validationAttribute != null) + { + var isValid = validationAttribute.IsValid(args); + if (!isValid) + { + modelState.AddModelError(parameter.Name, validationAttribute.FormatErrorMessage(parameter.Name)); + } + } + } + } + } +} diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/wwwroot/README.md b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/wwwroot/README.md new file mode 100644 index 000000000000..6a0b78471a33 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/wwwroot/README.md @@ -0,0 +1,42 @@ +# Welcome to ASP.NET 5 Preview + +We've made some big updates in this release, so it’s **important** that you spend a few minutes to learn what’s new. + +ASP.NET 5 has been rearchitected to make it **lean** and **composable**. It's fully **open source** and available on [GitHub](http://go.microsoft.com/fwlink/?LinkID=517854). +Your new project automatically takes advantage of modern client-side utilities like [Bower](http://go.microsoft.com/fwlink/?LinkId=518004) and [npm](http://go.microsoft.com/fwlink/?LinkId=518005) (to add client-side libraries) and [Gulp](http://go.microsoft.com/fwlink/?LinkId=518007) (for client-side build and automation tasks). + +We hope you enjoy the new capabilities in ASP.NET 5 and Visual Studio 2015. +The ASP.NET Team + +### You've created a new ASP.NET 5 project. [Learn what's new](http://go.microsoft.com/fwlink/?LinkId=518016) + +### This application consists of: +* Sample pages using ASP.NET MVC 6 +* [Gulp](http://go.microsoft.com/fwlink/?LinkId=518007) and [Bower](http://go.microsoft.com/fwlink/?LinkId=518004) for managing client-side resources +* Theming using [Bootstrap](http://go.microsoft.com/fwlink/?LinkID=398939) + +#### NEW CONCEPTS +* [The 'wwwroot' explained](http://go.microsoft.com/fwlink/?LinkId=518008) +* [Configuration in ASP.NET 5](http://go.microsoft.com/fwlink/?LinkId=518012) +* [Dependency Injection](http://go.microsoft.com/fwlink/?LinkId=518013) +* [Razor TagHelpers](http://go.microsoft.com/fwlink/?LinkId=518014) +* [Manage client packages using Gulp](http://go.microsoft.com/fwlink/?LinkID=517849) +* [Develop on different platforms](http://go.microsoft.com/fwlink/?LinkID=517850) + +#### CUSTOMIZE APP +* [Add Controllers and Views](http://go.microsoft.com/fwlink/?LinkID=398600) +* [Add Data using EntityFramework](http://go.microsoft.com/fwlink/?LinkID=398602) +* [Add Authentication using Identity](http://go.microsoft.com/fwlink/?LinkID=398603) +* [Add real time support using SignalR](http://go.microsoft.com/fwlink/?LinkID=398606) +* [Add Class library](http://go.microsoft.com/fwlink/?LinkID=398604) +* [Add Web APIs with MVC 6](http://go.microsoft.com/fwlink/?LinkId=518009) +* [Add client packages using Bower](http://go.microsoft.com/fwlink/?LinkID=517848) + +#### DEPLOY +* [Run your app locally](http://go.microsoft.com/fwlink/?LinkID=517851) +* [Run your app on ASP.NET Core 5](http://go.microsoft.com/fwlink/?LinkID=517852) +* [Run commands in your 'project.json'](http://go.microsoft.com/fwlink/?LinkID=517853) +* [Publish to Microsoft Azure Web Sites](http://go.microsoft.com/fwlink/?LinkID=398609) +* [Publish to the file system](http://go.microsoft.com/fwlink/?LinkId=518019) + +We would love to hear your [feedback](http://go.microsoft.com/fwlink/?LinkId=518015) diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/wwwroot/index.html b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/wwwroot/index.html new file mode 100644 index 000000000000..cde1f2f90b92 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/wwwroot/index.html @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/wwwroot/openapi-original.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/wwwroot/openapi-original.mustache new file mode 100644 index 000000000000..2c1b461cf000 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/wwwroot/openapi-original.mustache @@ -0,0 +1 @@ +{{{openapi-json}}} diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/wwwroot/web.config b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/wwwroot/web.config new file mode 100644 index 000000000000..e70a7778d60b --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/wwwroot/web.config @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/samples/server/petstore/aspnetcore/.openapi-generator/VERSION b/samples/server/petstore/aspnetcore/.openapi-generator/VERSION index 096bf47efe31..6d94c9c2e12a 100644 --- a/samples/server/petstore/aspnetcore/.openapi-generator/VERSION +++ b/samples/server/petstore/aspnetcore/.openapi-generator/VERSION @@ -1 +1 @@ -3.0.0-SNAPSHOT \ No newline at end of file +3.3.0-SNAPSHOT \ No newline at end of file diff --git a/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Controllers/PetApi.cs b/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Controllers/PetApi.cs index 0be78aacc4fe..9ea99af7cdf6 100644 --- a/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Controllers/PetApi.cs +++ b/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Controllers/PetApi.cs @@ -11,6 +11,7 @@ using System; using System.Collections.Generic; using Microsoft.AspNetCore.Mvc; +using Swashbuckle.AspNetCore.Annotations; using Swashbuckle.AspNetCore.SwaggerGen; using Newtonsoft.Json; using System.ComponentModel.DataAnnotations; @@ -22,7 +23,7 @@ namespace Org.OpenAPITools.Controllers /// /// /// - public class PetApiController : Controller + public class PetApiController : ControllerBase { /// /// Add a new pet to the store diff --git a/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Controllers/StoreApi.cs b/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Controllers/StoreApi.cs index ed5f6e327ee3..2b46191a7925 100644 --- a/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Controllers/StoreApi.cs +++ b/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Controllers/StoreApi.cs @@ -11,6 +11,7 @@ using System; using System.Collections.Generic; using Microsoft.AspNetCore.Mvc; +using Swashbuckle.AspNetCore.Annotations; using Swashbuckle.AspNetCore.SwaggerGen; using Newtonsoft.Json; using System.ComponentModel.DataAnnotations; @@ -22,7 +23,7 @@ namespace Org.OpenAPITools.Controllers /// /// /// - public class StoreApiController : Controller + public class StoreApiController : ControllerBase { /// /// Delete purchase order by ID diff --git a/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Controllers/UserApi.cs b/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Controllers/UserApi.cs index 463f0d8ecfd0..99fc6e4ea243 100644 --- a/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Controllers/UserApi.cs +++ b/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Controllers/UserApi.cs @@ -11,6 +11,7 @@ using System; using System.Collections.Generic; using Microsoft.AspNetCore.Mvc; +using Swashbuckle.AspNetCore.Annotations; using Swashbuckle.AspNetCore.SwaggerGen; using Newtonsoft.Json; using System.ComponentModel.DataAnnotations; @@ -22,7 +23,7 @@ namespace Org.OpenAPITools.Controllers /// /// /// - public class UserApiController : Controller + public class UserApiController : ControllerBase { /// /// Create user diff --git a/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Org.OpenAPITools.csproj b/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Org.OpenAPITools.csproj index 48868bd103b1..05d9192229fa 100644 --- a/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Org.OpenAPITools.csproj +++ b/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Org.OpenAPITools.csproj @@ -2,17 +2,16 @@ Org.OpenAPITools Org.OpenAPITools - netcoreapp2.0 + netcoreapp2.1 true true Org.OpenAPITools Org.OpenAPITools - - - - + + + diff --git a/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Program.cs b/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Program.cs index 3da6fd0a6d0d..fc39443af250 100644 --- a/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Program.cs +++ b/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Program.cs @@ -14,18 +14,17 @@ public class Program /// public static void Main(string[] args) { - BuildWebHost(args).Run(); + CreateWebHostBuilder(args).Build().Run(); } /// - /// Build Web Host + /// Create the web host builder. /// /// - /// Webhost - public static IWebHost BuildWebHost(string[] args) => + /// IWebHostBuilder + public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) - .UseStartup() - .UseUrls("http://0.0.0.0:8080/") - .Build(); + .UseStartup() + .UseUrls("http://0.0.0.0:8080/"); } -} +} \ No newline at end of file diff --git a/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Properties/launchSettings.json b/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Properties/launchSettings.json index 21acfed207b9..1b527df2c2b3 100644 --- a/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Properties/launchSettings.json +++ b/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Properties/launchSettings.json @@ -1,25 +1,27 @@ -{ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", "iisSettings": { - "windowsAuthentication": false, - "anonymousAuthentication": true, + "windowsAuthentication": false, + "anonymousAuthentication": true, "iisExpress": { - "applicationUrl": "http://localhost:50352/", - "sslPort": 0 + "applicationUrl": "http://localhost:61788", + "sslPort": 44301 } }, "profiles": { "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, - "launchUrl": "swagger/", + "launchUrl": "api/values", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, - "web": { + "WebApplication1": { "commandName": "Project", "launchBrowser": true, - "launchUrl": "http://localhost:5000/swagger/", + "launchUrl": "api/values", + "applicationUrl": "https://localhost:5001;http://localhost:5000", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Startup.cs b/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Startup.cs index 4f76c4ed6d1c..0df10a0b0cb7 100644 --- a/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Startup.cs +++ b/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Startup.cs @@ -10,8 +10,10 @@ using System; using System.IO; +using System.Reflection; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Newtonsoft.Json.Converters; @@ -27,20 +29,20 @@ namespace Org.OpenAPITools /// public class Startup { - private readonly IHostingEnvironment _hostingEnv; - private readonly IConfiguration _configuration; - /// /// Constructor /// - /// /// - public Startup(IHostingEnvironment env, IConfiguration configuration) + public Startup(IConfiguration configuration) { - _hostingEnv = env; - _configuration = configuration; + Configuration = configuration; } + /// + /// The application configuration. + /// + public IConfiguration Configuration { get; } + /// /// This method gets called by the runtime. Use this method to add services to the container. /// @@ -50,6 +52,7 @@ public void ConfigureServices(IServiceCollection services) // Add framework services. services .AddMvc() + .SetCompatibilityVersion (CompatibilityVersion.Version_2_1) .AddJsonOptions(opts => { opts.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); @@ -77,7 +80,7 @@ public void ConfigureServices(IServiceCollection services) }); c.CustomSchemaIds(type => type.FriendlyId(true)); c.DescribeAllEnumsAsStrings(); - c.IncludeXmlComments($"{AppContext.BaseDirectory}{Path.DirectorySeparatorChar}{_hostingEnv.ApplicationName}.xml"); + c.IncludeXmlComments($"{AppContext.BaseDirectory}{Path.DirectorySeparatorChar}{Assembly.GetEntryAssembly().GetName().Name}.xml"); // Sets the basePath property in the Swagger document generated c.DocumentFilter("/v2"); @@ -91,8 +94,9 @@ public void ConfigureServices(IServiceCollection services) /// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. /// /// - public void Configure(IApplicationBuilder app) + public void Configure(IApplicationBuilder app, IHostingEnvironment env) { + app.UseHttpsRedirection(); app .UseMvc() .UseDefaultFiles() @@ -110,14 +114,13 @@ public void Configure(IApplicationBuilder app) // c.SwaggerEndpoint("/openapi-original.json", "OpenAPI Petstore Original"); }); - if (_hostingEnv.IsDevelopment()) +if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { - //TODO: Enable production exception handling (https://docs.microsoft.com/en-us/aspnet/core/fundamentals/error-handling) - // app.UseExceptionHandler("/Home/Error"); + app.UseHsts(); } } } diff --git a/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/appsettings.json b/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/appsettings.json index c6af7d9b0695..def9159a7d94 100644 --- a/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/appsettings.json +++ b/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/appsettings.json @@ -1,10 +1,8 @@ { "Logging": { - "IncludeScopes": false, "LogLevel": { - "Default": "Information", - "System": "Information", - "Microsoft": "Information" + "Default": "Warning" } - } + }, + "AllowedHosts": "*" } diff --git a/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/wwwroot/openapi-original.json b/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/wwwroot/openapi-original.json index 34eb757240dd..82d674b55753 100644 --- a/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/wwwroot/openapi-original.json +++ b/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/wwwroot/openapi-original.json @@ -104,6 +104,7 @@ "in" : "query", "description" : "Status values that need to be considered for filter", "required" : true, + "style" : "form", "explode" : false, "schema" : { "type" : "array", @@ -157,6 +158,7 @@ "in" : "query", "description" : "Tags to filter by", "required" : true, + "style" : "form", "explode" : false, "schema" : { "type" : "array",