From ec7ea4176ccfd6520a2344dc366f49d860b962ad Mon Sep 17 00:00:00 2001 From: Aaron Long Date: Mon, 7 Jan 2019 10:26:15 -0500 Subject: [PATCH 1/5] Begin Asp.Net Core Lib option - Copied and modified asp.net core template under new flag - Begin to edit logic generation to support dependency injected solution --- .../AspNetCoreServerLibraryCodegen.java | 223 ++++++++++++++++++ .../org.openapitools.codegen.CodegenConfig | 1 + .../aspnetcorelib/2.0/Dockerfile.mustache | 18 ++ .../2.0/Filters/BasePathFilter.mustache | 50 ++++ ...eneratePathParamsValidationFilter.mustache | 97 ++++++++ .../aspnetcorelib/2.0/Program.mustache | 31 +++ .../aspnetcorelib/2.0/Project.csproj.mustache | 20 ++ .../2.0/Properties/launchSettings.json | 28 +++ .../aspnetcorelib/2.0/README.mustache | 27 +++ .../aspnetcorelib/2.0/Solution.mustache | 22 ++ .../aspnetcorelib/2.0/Startup.mustache | 117 +++++++++ .../aspnetcorelib/2.0/appsettings.json | 10 + .../aspnetcorelib/2.0/bodyParam.mustache | 1 + .../aspnetcorelib/2.0/build.bat.mustache | 9 + .../aspnetcorelib/2.0/build.sh.mustache | 8 + .../aspnetcorelib/2.0/controller.mustache | 54 +++++ .../aspnetcorelib/2.0/enumClass.mustache | 18 ++ .../aspnetcorelib/2.0/formParam.mustache | 1 + .../resources/aspnetcorelib/2.0/gitignore | 208 ++++++++++++++++ .../aspnetcorelib/2.0/headerParam.mustache | 1 + .../aspnetcorelib/2.0/listReturn.mustache | 4 + .../aspnetcorelib/2.0/mapReturn.mustache | 4 + .../aspnetcorelib/2.0/model.mustache | 136 +++++++++++ .../aspnetcorelib/2.0/objectReturn.mustache | 4 + .../aspnetcorelib/2.0/partial_header.mustache | 13 + .../aspnetcorelib/2.0/pathParam.mustache | 1 + .../aspnetcorelib/2.0/queryParam.mustache | 1 + .../resources/aspnetcorelib/2.0/tags.mustache | 1 + .../aspnetcorelib/2.0/validateModel.mustache | 61 +++++ .../resources/aspnetcorelib/2.0/web.config | 14 ++ .../aspnetcorelib/2.0/wwwroot/README.md | 42 ++++ .../aspnetcorelib/2.0/wwwroot/index.html | 1 + .../2.0/wwwroot/openapi-original.mustache | 1 + .../aspnetcorelib/2.0/wwwroot/web.config | 9 + .../aspnetcorelib/2.1/Project.csproj.mustache | 18 ++ .../aspnetcorelib/2.1/README.mustache | 27 +++ .../aspnetcorelib/2.1/Solution.mustache | 22 ++ .../aspnetcorelib/2.1/bodyParam.mustache | 1 + .../aspnetcorelib/2.1/controller.mustache | 60 +++++ .../aspnetcorelib/2.1/enumClass.mustache | 18 ++ .../aspnetcorelib/2.1/formParam.mustache | 1 + .../resources/aspnetcorelib/2.1/gitignore | 208 ++++++++++++++++ .../aspnetcorelib/2.1/headerParam.mustache | 1 + .../aspnetcorelib/2.1/listReturn.mustache | 4 + .../aspnetcorelib/2.1/mapReturn.mustache | 4 + .../aspnetcorelib/2.1/model.mustache | 136 +++++++++++ .../aspnetcorelib/2.1/objectReturn.mustache | 4 + .../aspnetcorelib/2.1/partial_header.mustache | 13 + .../aspnetcorelib/2.1/pathParam.mustache | 1 + .../aspnetcorelib/2.1/queryParam.mustache | 1 + .../resources/aspnetcorelib/2.1/tags.mustache | 1 + .../aspnetcorelib/2.1/validateModel.mustache | 61 +++++ 52 files changed, 1817 insertions(+) create mode 100644 modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetCoreServerLibraryCodegen.java create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Dockerfile.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Filters/BasePathFilter.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Filters/GeneratePathParamsValidationFilter.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Program.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Project.csproj.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Properties/launchSettings.json create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/README.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Solution.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Startup.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/appsettings.json create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/bodyParam.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/build.bat.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/build.sh.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/controller.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/enumClass.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/formParam.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/gitignore create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/headerParam.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/listReturn.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/mapReturn.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/model.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/objectReturn.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/partial_header.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/pathParam.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/queryParam.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/tags.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/validateModel.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/web.config create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/wwwroot/README.md create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/wwwroot/index.html create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/wwwroot/openapi-original.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/wwwroot/web.config create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/Project.csproj.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/README.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/Solution.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/bodyParam.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/controller.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/enumClass.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/formParam.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/gitignore create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/headerParam.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/listReturn.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/mapReturn.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/model.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/objectReturn.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/partial_header.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/pathParam.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/queryParam.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/tags.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/validateModel.mustache diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetCoreServerLibraryCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetCoreServerLibraryCodegen.java new file mode 100644 index 000000000000..1fffec695905 --- /dev/null +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetCoreServerLibraryCodegen.java @@ -0,0 +1,223 @@ +/* + * Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech) + * Copyright 2018 SmartBear Software + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openapitools.codegen.languages; + +import com.samskivert.mustache.Mustache; +import io.swagger.v3.oas.models.OpenAPI; +import org.openapitools.codegen.CodegenConstants; +import org.openapitools.codegen.CodegenOperation; +import org.openapitools.codegen.CodegenType; +import org.openapitools.codegen.SupportingFile; +import org.openapitools.codegen.utils.URLPathUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.net.URL; +import java.util.Arrays; +import java.util.Locale; +import java.util.Map; + +import static java.util.UUID.randomUUID; + +public class AspNetCoreServerLibraryCodegen 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) + "}"; + + @SuppressWarnings("hiding") + protected Logger LOGGER = LoggerFactory.getLogger(AspNetCoreServerLibraryCodegen.class); + + 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 AspNetCoreServerLibraryCodegen() { + super(); + + outputFolder = "generated-code" + File.separator + getName(); + + modelTemplateFiles.put("model.mustache", ".cs"); + apiTemplateFiles.put("controller.mustache", ".cs"); + + embeddedTemplateDir = templateDir = "aspnetcorelib/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( + Arrays.asList("var", "async", "await", "dynamic", "yield") + ); + + cliOptions.clear(); + + // CLI options + addOption(CodegenConstants.PACKAGE_NAME, + "C# package name (convention: Title.Case).", + packageName); + + addOption(CodegenConstants.PACKAGE_VERSION, + "C# package version.", + packageVersion); + + addOption(CodegenConstants.OPTIONAL_PROJECT_GUID, + CodegenConstants.OPTIONAL_PROJECT_GUID_DESC, + null); + + addOption(CodegenConstants.SOURCE_FOLDER, + 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, + sortParamsByRequiredFlag); + + addSwitch(CodegenConstants.USE_DATETIME_OFFSET, + CodegenConstants.USE_DATETIME_OFFSET_DESC, + useDateTimeOffsetFlag); + + addSwitch(CodegenConstants.USE_COLLECTION, + CodegenConstants.USE_COLLECTION_DESC, + useCollection); + + addSwitch(CodegenConstants.RETURN_ICOLLECTION, + CodegenConstants.RETURN_ICOLLECTION_DESC, + returnICollection); + + addSwitch(USE_SWASHBUCKLE, + "Uses the Swashbuckle.AspNetCore NuGet package for documentation.", + useSwashbuckle); + } + + @Override + public CodegenType getTag() { + return CodegenType.SERVER; + } + + @Override + public String getName() { + return "aspnetcorelib"; + } + + @Override + public String getHelp() { + return "Generates an ASP.NET Core Web API server."; + } + + @Override + public void preprocessOpenAPI(OpenAPI openAPI) { + super.preprocessOpenAPI(openAPI); + URL url = URLPathUtils.getServerURL(openAPI); + additionalProperties.put("serverHost", url.getHost()); + additionalProperties.put("serverPort", URLPathUtils.getPort(url, 8080)); + } + + @Override + public void processOpts() { + super.processOpts(); + + if (additionalProperties.containsKey(CodegenConstants.OPTIONAL_PROJECT_GUID)) { + setPackageGuid((String) additionalProperties.get(CodegenConstants.OPTIONAL_PROJECT_GUID)); + } + additionalProperties.put("packageGuid", packageGuid); + + if (additionalProperties.containsKey(USE_SWASHBUCKLE)) { + useSwashbuckle = convertPropertyToBooleanAndWriteBack(USE_SWASHBUCKLE); + } else { + 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"; + modelPackage = packageName + ".Models"; + + String packageFolder = sourceFolder + File.separator + packageName; + + supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")); + supportingFiles.add(new SupportingFile("Solution.mustache", "", packageName + ".sln")); + supportingFiles.add(new SupportingFile("gitignore", packageFolder, ".gitignore")); + + supportingFiles.add(new SupportingFile("validateModel.mustache", packageFolder + File.separator + "Attributes", "ValidateModelStateAttribute.cs")); + supportingFiles.add(new SupportingFile("Project.csproj.mustache", packageFolder, packageName + ".csproj")); + } + + 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"; + } + + @Override + public String modelFileFolder() { + return outputFolder + File.separator + sourceFolder + File.separator + packageName + File.separator + "Models"; + } + + @Override + public Map postProcessSupportingFileData(Map objs) { + generateJSONSpecFile(objs); + return super.postProcessSupportingFileData(objs); + } + + @Override + protected void processOperation(CodegenOperation operation) { + super.processOperation(operation); + + // HACK: Unlikely in the wild, but we need to clean operation paths for MVC Routing + if (operation.path != null) { + String original = operation.path; + operation.path = operation.path.replace("?", "/"); + if (!original.equals(operation.path)) { + LOGGER.warn("Normalized " + original + " to " + operation.path + ". Please verify generated source."); + } + } + + // Converts, for example, PUT to HttpPut for controller attributes + operation.httpMethod = "Http" + operation.httpMethod.substring(0, 1) + operation.httpMethod.substring(1).toLowerCase(Locale.ROOT); + } + + @Override + public Mustache.Compiler processCompiler(Mustache.Compiler compiler) { + // To avoid unexpected behaviors when options are passed programmatically such as { "useCollection": "" } + return super.processCompiler(compiler).emptyStringIsFalse(true); + } + + @Override + public String toRegularExpression(String pattern) { + return escapeText(pattern); + } +} diff --git a/modules/openapi-generator/src/main/resources/META-INF/services/org.openapitools.codegen.CodegenConfig b/modules/openapi-generator/src/main/resources/META-INF/services/org.openapitools.codegen.CodegenConfig index fd1884948ce1..b7267f1a4d59 100644 --- a/modules/openapi-generator/src/main/resources/META-INF/services/org.openapitools.codegen.CodegenConfig +++ b/modules/openapi-generator/src/main/resources/META-INF/services/org.openapitools.codegen.CodegenConfig @@ -4,6 +4,7 @@ org.openapitools.codegen.languages.AndroidClientCodegen org.openapitools.codegen.languages.Apache2ConfigCodegen org.openapitools.codegen.languages.ApexClientCodegen org.openapitools.codegen.languages.AspNetCoreServerCodegen +org.openapitools.codegen.languages.AspNetCoreServerLibraryCodegen org.openapitools.codegen.languages.BashClientCodegen org.openapitools.codegen.languages.CLibcurlClientCodegen org.openapitools.codegen.languages.ClojureClientCodegen diff --git a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Dockerfile.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Dockerfile.mustache new file mode 100644 index 000000000000..e9d80c52833b --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/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/aspnetcorelib/2.0/Filters/BasePathFilter.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Filters/BasePathFilter.mustache new file mode 100644 index 000000000000..a3e1b9ce0804 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/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/aspnetcorelib/2.0/Filters/GeneratePathParamsValidationFilter.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Filters/GeneratePathParamsValidationFilter.mustache new file mode 100644 index 000000000000..d857a4a0f960 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/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/aspnetcorelib/2.0/Program.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Program.mustache new file mode 100644 index 000000000000..7de837799830 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Program.mustache @@ -0,0 +1,31 @@ +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore; + +namespace {{packageName}} +{ + /// + /// Program + /// + public class Program + { + /// + /// Main + /// + /// + public static void Main(string[] args) + { + BuildWebHost(args).Run(); + } + + /// + /// Build Web Host + /// + /// + /// Webhost + public static IWebHost BuildWebHost(string[] args) => + WebHost.CreateDefaultBuilder(args) + .UseStartup() + .UseUrls("http://0.0.0.0:{{#serverPort}}{{serverPort}}{{/serverPort}}{{^serverPort}}8080{{/serverPort}}/") + .Build(); + } +} diff --git a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Project.csproj.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Project.csproj.mustache new file mode 100644 index 000000000000..b4e85fe168f4 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Project.csproj.mustache @@ -0,0 +1,20 @@ + + + {{packageName}} + {{packageName}} + netcoreapp2.0 + true + true + {{packageName}} + {{packageName}} + + + {{#useSwashbuckle}} + {{/useSwashbuckle}} + + + + + + + diff --git a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Properties/launchSettings.json b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Properties/launchSettings.json new file mode 100644 index 000000000000..21acfed207b9 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Properties/launchSettings.json @@ -0,0 +1,28 @@ +{ + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:50352/", + "sslPort": 0 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger/", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "web": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "http://localhost:5000/swagger/", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/README.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/README.mustache new file mode 100644 index 000000000000..71c4cfebb8b3 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/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/aspnetcorelib/2.0/Solution.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Solution.mustache new file mode 100644 index 000000000000..8c6d69ea93da --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/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/aspnetcorelib/2.0/Startup.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Startup.mustache new file mode 100644 index 000000000000..2a4c1f53cf2a --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Startup.mustache @@ -0,0 +1,117 @@ +{{>partial_header}} +using System; +using System.IO; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +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 + { + private readonly IHostingEnvironment _hostingEnv; + private readonly IConfiguration _configuration; + + /// + /// Constructor + /// + /// + /// + public Startup(IHostingEnvironment env, IConfiguration configuration) + { + _hostingEnv = env; + _configuration = configuration; + } + + /// + /// 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() + .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}{_hostingEnv.ApplicationName}.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) + { + 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 (_hostingEnv.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + else + { + //TODO: Enable production exception handling (https://docs.microsoft.com/en-us/aspnet/core/fundamentals/error-handling) + // app.UseExceptionHandler("/Home/Error"); + } + } + } +} diff --git a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/appsettings.json b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/appsettings.json new file mode 100644 index 000000000000..c6af7d9b0695 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/appsettings.json @@ -0,0 +1,10 @@ +{ + "Logging": { + "IncludeScopes": false, + "LogLevel": { + "Default": "Information", + "System": "Information", + "Microsoft": "Information" + } + } +} diff --git a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/bodyParam.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/bodyParam.mustache new file mode 100644 index 000000000000..02b0fa1d2dea --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/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/aspnetcorelib/2.0/build.bat.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/build.bat.mustache new file mode 100644 index 000000000000..e437bccf7d6f --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/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/aspnetcorelib/2.0/build.sh.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/build.sh.mustache new file mode 100644 index 000000000000..3804359d7e26 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/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/aspnetcorelib/2.0/controller.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/controller.mustache new file mode 100644 index 000000000000..ba152878544e --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/controller.mustache @@ -0,0 +1,54 @@ +{{>partial_header}} +using System; +using System.Collections.Generic; +using Microsoft.AspNetCore.Mvc;{{#useSwashbuckle}} +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 : Controller + { {{#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/aspnetcorelib/2.0/enumClass.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/enumClass.mustache new file mode 100644 index 000000000000..a8a68b998449 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/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/aspnetcorelib/2.0/formParam.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/formParam.mustache new file mode 100644 index 000000000000..2d42dc2916ba --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/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/aspnetcorelib/2.0/gitignore b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/gitignore new file mode 100644 index 000000000000..cd9b840e5498 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/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/aspnetcorelib/2.0/headerParam.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/headerParam.mustache new file mode 100644 index 000000000000..45a5be9d7b5b --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/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/aspnetcorelib/2.0/listReturn.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/listReturn.mustache new file mode 100644 index 000000000000..d609e67148c2 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/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/aspnetcorelib/2.0/mapReturn.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/mapReturn.mustache new file mode 100644 index 000000000000..856fb1b3507c --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/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/aspnetcorelib/2.0/model.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/model.mustache new file mode 100644 index 000000000000..4ff19b963b8a --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/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}}{{^isContainer}} + ( + {{name}} == other.{{name}} || + {{name}} != null && + {{name}}.Equals(other.{{name}}) + ){{#hasMore}} && {{/hasMore}}{{/isContainer}}{{#isContainer}} + ( + {{name}} == other.{{name}} || + {{name}} != null && + {{name}}.SequenceEqual(other.{{name}}) + ){{#hasMore}} && {{/hasMore}}{{/isContainer}}{{/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/aspnetcorelib/2.0/objectReturn.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/objectReturn.mustache new file mode 100644 index 000000000000..4059a61ac0b8 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/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/aspnetcorelib/2.0/partial_header.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/partial_header.mustache new file mode 100644 index 000000000000..4a682818a37a --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/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/aspnetcorelib/2.0/pathParam.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/pathParam.mustache new file mode 100644 index 000000000000..70303432d48d --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/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/aspnetcorelib/2.0/queryParam.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/queryParam.mustache new file mode 100644 index 000000000000..e9fa09b005d3 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/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/aspnetcorelib/2.0/tags.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/tags.mustache new file mode 100644 index 000000000000..c97df19949e6 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/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/aspnetcorelib/2.0/validateModel.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/validateModel.mustache new file mode 100644 index 000000000000..e11aaa5d2708 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/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/aspnetcorelib/2.0/web.config b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/web.config new file mode 100644 index 000000000000..a3b9f6add9c3 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/web.config @@ -0,0 +1,14 @@ + + + + + + + + + + + + diff --git a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/wwwroot/README.md b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/wwwroot/README.md new file mode 100644 index 000000000000..6a0b78471a33 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/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/aspnetcorelib/2.0/wwwroot/index.html b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/wwwroot/index.html new file mode 100644 index 000000000000..cde1f2f90b92 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/wwwroot/index.html @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/wwwroot/openapi-original.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/wwwroot/openapi-original.mustache new file mode 100644 index 000000000000..2c1b461cf000 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/wwwroot/openapi-original.mustache @@ -0,0 +1 @@ +{{{openapi-json}}} diff --git a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/wwwroot/web.config b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/wwwroot/web.config new file mode 100644 index 000000000000..e70a7778d60b --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/wwwroot/web.config @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/Project.csproj.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/Project.csproj.mustache new file mode 100644 index 000000000000..ecbf96630a15 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/Project.csproj.mustache @@ -0,0 +1,18 @@ + + + + {{packageName}} + {{packageName}} + netstandard2.0 + true + true + {{packageName}} + {{packageName}} + + + + + + + + diff --git a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/README.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/README.mustache new file mode 100644 index 000000000000..71c4cfebb8b3 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/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/aspnetcorelib/2.1/Solution.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/Solution.mustache new file mode 100644 index 000000000000..8c6d69ea93da --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/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/aspnetcorelib/2.1/bodyParam.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/bodyParam.mustache new file mode 100644 index 000000000000..02b0fa1d2dea --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/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/aspnetcorelib/2.1/controller.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/controller.mustache new file mode 100644 index 000000000000..0e402a6c0d95 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/controller.mustache @@ -0,0 +1,60 @@ +{{>partial_header}} +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Net; +using {{packageName}}.Attributes; +using {{packageName}}.Models; +using Microsoft.AspNetCore.Mvc; + +namespace {{packageName}}.Controllers +{ {{#operations}} + + /// + /// {{description}} + /// {{#description}} + [Description("{{description}}")]{{/description}} + public interface I{{classname}}Implementation + { + {{#operation}} + /// + /// {{#summary}}{{summary}}{{/summary}} + /// {{#notes}} + /// {{notes}}{{/notes}}{{#allParams}} + /// {{description}}{{/allParams}}{{#responses}} + /// {{message}}{{/responses}} + (HttpStatusCode statusCode, object response) {{operationId}}({{#allParams}}{{&dataType}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}); + {{/operation}} + } + + /// + /// {{description}} + /// {{#description}} + [Description("{{description}}")]{{/description}} + public class {{classname}}Controller : ControllerBase + { + private readonly I{{classname}}Implementation _impl; + + public {{classname}}Controller(I{{classname}}Implementation impl) + { + _impl = impl; + } + + {{#operation}} + /// + /// {{#summary}}{{summary}}{{/summary}} + /// {{#notes}} + /// {{notes}}{{/notes}}{{#allParams}} + /// {{description}}{{/allParams}}{{#responses}} + /// {{message}}{{/responses}} + [{{httpMethod}}] + [Route("{{{basePathWithoutHost}}}{{{path}}}")] + [ValidateModelState] + public virtual IActionResult {{operationId}}({{#allParams}}{{>pathParam}}{{>queryParam}}{{>bodyParam}}{{>formParam}}{{>headerParam}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) + { + throw new NotImplementedException(); + } + {{/operation}} + } +{{/operations}} +} diff --git a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/enumClass.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/enumClass.mustache new file mode 100644 index 000000000000..a8a68b998449 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/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/aspnetcorelib/2.1/formParam.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/formParam.mustache new file mode 100644 index 000000000000..2d42dc2916ba --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/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/aspnetcorelib/2.1/gitignore b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/gitignore new file mode 100644 index 000000000000..cd9b840e5498 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/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/aspnetcorelib/2.1/headerParam.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/headerParam.mustache new file mode 100644 index 000000000000..45a5be9d7b5b --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/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/aspnetcorelib/2.1/listReturn.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/listReturn.mustache new file mode 100644 index 000000000000..d609e67148c2 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/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/aspnetcorelib/2.1/mapReturn.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/mapReturn.mustache new file mode 100644 index 000000000000..856fb1b3507c --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/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/aspnetcorelib/2.1/model.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/model.mustache new file mode 100644 index 000000000000..4ff19b963b8a --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/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}}{{^isContainer}} + ( + {{name}} == other.{{name}} || + {{name}} != null && + {{name}}.Equals(other.{{name}}) + ){{#hasMore}} && {{/hasMore}}{{/isContainer}}{{#isContainer}} + ( + {{name}} == other.{{name}} || + {{name}} != null && + {{name}}.SequenceEqual(other.{{name}}) + ){{#hasMore}} && {{/hasMore}}{{/isContainer}}{{/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/aspnetcorelib/2.1/objectReturn.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/objectReturn.mustache new file mode 100644 index 000000000000..4059a61ac0b8 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/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/aspnetcorelib/2.1/partial_header.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/partial_header.mustache new file mode 100644 index 000000000000..4a682818a37a --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/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/aspnetcorelib/2.1/pathParam.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/pathParam.mustache new file mode 100644 index 000000000000..70303432d48d --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/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/aspnetcorelib/2.1/queryParam.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/queryParam.mustache new file mode 100644 index 000000000000..e9fa09b005d3 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/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/aspnetcorelib/2.1/tags.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/tags.mustache new file mode 100644 index 000000000000..c97df19949e6 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/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/aspnetcorelib/2.1/validateModel.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/validateModel.mustache new file mode 100644 index 000000000000..e11aaa5d2708 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/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)); + } + } + } + } + } +} From 9944aabadc851bdad9d56bac4d7cf0fc951e5779 Mon Sep 17 00:00:00 2001 From: Aaron Long Date: Mon, 7 Jan 2019 13:12:20 -0500 Subject: [PATCH 2/5] Fix template to support async Taks --- .../resources/aspnetcorelib/2.1/controller.mustache | 11 ++++++++--- .../resources/aspnetcorelib/2.1/tupleReturn.mustache | 8 ++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/tupleReturn.mustache diff --git a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/controller.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/controller.mustache index 0e402a6c0d95..8a627abe9444 100644 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/controller.mustache +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/controller.mustache @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Net; +using System.Threading.Tasks; using {{packageName}}.Attributes; using {{packageName}}.Models; using Microsoft.AspNetCore.Mvc; @@ -23,7 +24,7 @@ namespace {{packageName}}.Controllers /// {{notes}}{{/notes}}{{#allParams}} /// {{description}}{{/allParams}}{{#responses}} /// {{message}}{{/responses}} - (HttpStatusCode statusCode, object response) {{operationId}}({{#allParams}}{{&dataType}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}); + Task<(HttpStatusCode statusCode, object response)> {{operationId}}({{#allParams}}{{&dataType}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}); {{/operation}} } @@ -35,6 +36,10 @@ namespace {{packageName}}.Controllers { private readonly I{{classname}}Implementation _impl; + /// + /// The IOC injected implementation + /// Controller's implementation + /// public {{classname}}Controller(I{{classname}}Implementation impl) { _impl = impl; @@ -50,9 +55,9 @@ namespace {{packageName}}.Controllers [{{httpMethod}}] [Route("{{{basePathWithoutHost}}}{{{path}}}")] [ValidateModelState] - public virtual IActionResult {{operationId}}({{#allParams}}{{>pathParam}}{{>queryParam}}{{>bodyParam}}{{>formParam}}{{>headerParam}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) + public virtual async Task {{operationId}}({{#allParams}}{{>pathParam}}{{>queryParam}}{{>bodyParam}}{{>formParam}}{{>headerParam}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) { - throw new NotImplementedException(); + {{>tupleReturn}} } {{/operation}} } diff --git a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/tupleReturn.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/tupleReturn.mustache new file mode 100644 index 000000000000..9f22ad67167d --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/tupleReturn.mustache @@ -0,0 +1,8 @@ +var (statusCode, result) = await _impl.{{operationId}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}); + + //TODO: Use spec to validate that the resultType matches the status code and that the status code was declared + + return new ObjectResult(result) + { + StatusCode = (int)statusCode + }; From 6c50922abb03b566f52a2b3a039bcd6592f8f080 Mon Sep 17 00:00:00 2001 From: Aaron Long Date: Mon, 7 Jan 2019 15:51:57 -0500 Subject: [PATCH 3/5] Add Extension method to bind the controllers - Cleaned up necessary dll bits and corresponding code - Provide extension method to bind controllers within project --- .../AspNetCoreServerLibraryCodegen.java | 27 +-- .../aspnetcorelib/2.0/Dockerfile.mustache | 18 -- .../2.0/Filters/BasePathFilter.mustache | 50 ----- ...eneratePathParamsValidationFilter.mustache | 97 -------- .../aspnetcorelib/2.0/Program.mustache | 31 --- .../aspnetcorelib/2.0/Project.csproj.mustache | 20 -- .../2.0/Properties/launchSettings.json | 28 --- .../aspnetcorelib/2.0/README.mustache | 27 --- .../aspnetcorelib/2.0/Solution.mustache | 22 -- .../aspnetcorelib/2.0/Startup.mustache | 117 ---------- .../aspnetcorelib/2.0/appsettings.json | 10 - .../aspnetcorelib/2.0/bodyParam.mustache | 1 - .../aspnetcorelib/2.0/build.bat.mustache | 9 - .../aspnetcorelib/2.0/build.sh.mustache | 8 - .../aspnetcorelib/2.0/controller.mustache | 54 ----- .../aspnetcorelib/2.0/enumClass.mustache | 18 -- .../aspnetcorelib/2.0/formParam.mustache | 1 - .../resources/aspnetcorelib/2.0/gitignore | 208 ------------------ .../aspnetcorelib/2.0/headerParam.mustache | 1 - .../aspnetcorelib/2.0/listReturn.mustache | 4 - .../aspnetcorelib/2.0/mapReturn.mustache | 4 - .../aspnetcorelib/2.0/model.mustache | 136 ------------ .../aspnetcorelib/2.0/objectReturn.mustache | 4 - .../aspnetcorelib/2.0/partial_header.mustache | 13 -- .../aspnetcorelib/2.0/pathParam.mustache | 1 - .../aspnetcorelib/2.0/queryParam.mustache | 1 - .../resources/aspnetcorelib/2.0/tags.mustache | 1 - .../aspnetcorelib/2.0/validateModel.mustache | 61 ----- .../resources/aspnetcorelib/2.0/web.config | 14 -- .../aspnetcorelib/2.0/wwwroot/README.md | 42 ---- .../aspnetcorelib/2.0/wwwroot/index.html | 1 - .../2.0/wwwroot/openapi-original.mustache | 1 - .../aspnetcorelib/2.0/wwwroot/web.config | 9 - .../aspnetcorelib/2.1/Project.csproj.mustache | 3 +- .../aspnetcorelib/2.1/Solution.mustache | 22 -- .../2.1/bindControllers.mustache | 24 ++ 36 files changed, 34 insertions(+), 1054 deletions(-) delete mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Dockerfile.mustache delete mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Filters/BasePathFilter.mustache delete mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Filters/GeneratePathParamsValidationFilter.mustache delete mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Program.mustache delete mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Project.csproj.mustache delete mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Properties/launchSettings.json delete mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/README.mustache delete mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Solution.mustache delete mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Startup.mustache delete mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/appsettings.json delete mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/bodyParam.mustache delete mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/build.bat.mustache delete mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/build.sh.mustache delete mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/controller.mustache delete mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/enumClass.mustache delete mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/formParam.mustache delete mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/gitignore delete mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/headerParam.mustache delete mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/listReturn.mustache delete mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/mapReturn.mustache delete mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/model.mustache delete mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/objectReturn.mustache delete mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/partial_header.mustache delete mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/pathParam.mustache delete mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/queryParam.mustache delete mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/tags.mustache delete mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/validateModel.mustache delete mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/web.config delete mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/wwwroot/README.md delete mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/wwwroot/index.html delete mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/wwwroot/openapi-original.mustache delete mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/wwwroot/web.config delete mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/Solution.mustache create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/bindControllers.mustache diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetCoreServerLibraryCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetCoreServerLibraryCodegen.java index 1fffec695905..e7d684801ee0 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetCoreServerLibraryCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetCoreServerLibraryCodegen.java @@ -50,6 +50,8 @@ public class AspNetCoreServerLibraryCodegen extends AbstractCSharpCodegen { protected String serverHost = "0.0.0.0"; protected String aspnetCoreVersion= "2.1"; // default to 2.1 + private String controllerBindingMethodName; + public AspNetCoreServerLibraryCodegen() { super(); @@ -60,6 +62,8 @@ public AspNetCoreServerLibraryCodegen() { embeddedTemplateDir = templateDir = "aspnetcorelib/2.1"; + controllerBindingMethodName =packageName.replace(".", ""); + // contextually reserved words // NOTE: C# uses camel cased reserved words, while models are title cased. We don't want lowercase comparisons. reservedWords.addAll( @@ -105,10 +109,6 @@ public AspNetCoreServerLibraryCodegen() { addSwitch(CodegenConstants.RETURN_ICOLLECTION, CodegenConstants.RETURN_ICOLLECTION_DESC, returnICollection); - - addSwitch(USE_SWASHBUCKLE, - "Uses the Swashbuckle.AspNetCore NuGet package for documentation.", - useSwashbuckle); } @Override @@ -138,35 +138,22 @@ public void preprocessOpenAPI(OpenAPI openAPI) { public void processOpts() { super.processOpts(); - if (additionalProperties.containsKey(CodegenConstants.OPTIONAL_PROJECT_GUID)) { - setPackageGuid((String) additionalProperties.get(CodegenConstants.OPTIONAL_PROJECT_GUID)); - } - additionalProperties.put("packageGuid", packageGuid); - - if (additionalProperties.containsKey(USE_SWASHBUCKLE)) { - useSwashbuckle = convertPropertyToBooleanAndWriteBack(USE_SWASHBUCKLE); - } else { - 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"; modelPackage = packageName + ".Models"; String packageFolder = sourceFolder + File.separator + packageName; - supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")); - supportingFiles.add(new SupportingFile("Solution.mustache", "", packageName + ".sln")); + supportingFiles.add(new SupportingFile("README.mustache", packageFolder, "README.md")); supportingFiles.add(new SupportingFile("gitignore", packageFolder, ".gitignore")); supportingFiles.add(new SupportingFile("validateModel.mustache", packageFolder + File.separator + "Attributes", "ValidateModelStateAttribute.cs")); supportingFiles.add(new SupportingFile("Project.csproj.mustache", packageFolder, packageName + ".csproj")); + supportingFiles.add(new SupportingFile("bindControllers.mustache", packageFolder, controllerBindingMethodName + ".cs")); } public void setPackageGuid(String packageGuid) { @@ -206,6 +193,8 @@ protected void processOperation(CodegenOperation operation) { } } + additionalProperties.put("controllerBindingMethodName", controllerBindingMethodName); + // Converts, for example, PUT to HttpPut for controller attributes operation.httpMethod = "Http" + operation.httpMethod.substring(0, 1) + operation.httpMethod.substring(1).toLowerCase(Locale.ROOT); } diff --git a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Dockerfile.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Dockerfile.mustache deleted file mode 100644 index e9d80c52833b..000000000000 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Dockerfile.mustache +++ /dev/null @@ -1,18 +0,0 @@ -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/aspnetcorelib/2.0/Filters/BasePathFilter.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Filters/BasePathFilter.mustache deleted file mode 100644 index a3e1b9ce0804..000000000000 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Filters/BasePathFilter.mustache +++ /dev/null @@ -1,50 +0,0 @@ -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/aspnetcorelib/2.0/Filters/GeneratePathParamsValidationFilter.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Filters/GeneratePathParamsValidationFilter.mustache deleted file mode 100644 index d857a4a0f960..000000000000 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Filters/GeneratePathParamsValidationFilter.mustache +++ /dev/null @@ -1,97 +0,0 @@ -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/aspnetcorelib/2.0/Program.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Program.mustache deleted file mode 100644 index 7de837799830..000000000000 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Program.mustache +++ /dev/null @@ -1,31 +0,0 @@ -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore; - -namespace {{packageName}} -{ - /// - /// Program - /// - public class Program - { - /// - /// Main - /// - /// - public static void Main(string[] args) - { - BuildWebHost(args).Run(); - } - - /// - /// Build Web Host - /// - /// - /// Webhost - public static IWebHost BuildWebHost(string[] args) => - WebHost.CreateDefaultBuilder(args) - .UseStartup() - .UseUrls("http://0.0.0.0:{{#serverPort}}{{serverPort}}{{/serverPort}}{{^serverPort}}8080{{/serverPort}}/") - .Build(); - } -} diff --git a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Project.csproj.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Project.csproj.mustache deleted file mode 100644 index b4e85fe168f4..000000000000 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Project.csproj.mustache +++ /dev/null @@ -1,20 +0,0 @@ - - - {{packageName}} - {{packageName}} - netcoreapp2.0 - true - true - {{packageName}} - {{packageName}} - - - {{#useSwashbuckle}} - {{/useSwashbuckle}} - - - - - - - diff --git a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Properties/launchSettings.json b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Properties/launchSettings.json deleted file mode 100644 index 21acfed207b9..000000000000 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Properties/launchSettings.json +++ /dev/null @@ -1,28 +0,0 @@ -{ - "iisSettings": { - "windowsAuthentication": false, - "anonymousAuthentication": true, - "iisExpress": { - "applicationUrl": "http://localhost:50352/", - "sslPort": 0 - } - }, - "profiles": { - "IIS Express": { - "commandName": "IISExpress", - "launchBrowser": true, - "launchUrl": "swagger/", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - }, - "web": { - "commandName": "Project", - "launchBrowser": true, - "launchUrl": "http://localhost:5000/swagger/", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - } - } -} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/README.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/README.mustache deleted file mode 100644 index 71c4cfebb8b3..000000000000 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/README.mustache +++ /dev/null @@ -1,27 +0,0 @@ -# {{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/aspnetcorelib/2.0/Solution.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Solution.mustache deleted file mode 100644 index 8c6d69ea93da..000000000000 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Solution.mustache +++ /dev/null @@ -1,22 +0,0 @@ - -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/aspnetcorelib/2.0/Startup.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Startup.mustache deleted file mode 100644 index 2a4c1f53cf2a..000000000000 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/Startup.mustache +++ /dev/null @@ -1,117 +0,0 @@ -{{>partial_header}} -using System; -using System.IO; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -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 - { - private readonly IHostingEnvironment _hostingEnv; - private readonly IConfiguration _configuration; - - /// - /// Constructor - /// - /// - /// - public Startup(IHostingEnvironment env, IConfiguration configuration) - { - _hostingEnv = env; - _configuration = configuration; - } - - /// - /// 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() - .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}{_hostingEnv.ApplicationName}.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) - { - 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 (_hostingEnv.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - } - else - { - //TODO: Enable production exception handling (https://docs.microsoft.com/en-us/aspnet/core/fundamentals/error-handling) - // app.UseExceptionHandler("/Home/Error"); - } - } - } -} diff --git a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/appsettings.json b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/appsettings.json deleted file mode 100644 index c6af7d9b0695..000000000000 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/appsettings.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "Logging": { - "IncludeScopes": false, - "LogLevel": { - "Default": "Information", - "System": "Information", - "Microsoft": "Information" - } - } -} diff --git a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/bodyParam.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/bodyParam.mustache deleted file mode 100644 index 02b0fa1d2dea..000000000000 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/bodyParam.mustache +++ /dev/null @@ -1 +0,0 @@ -{{#isBodyParam}}[FromBody]{{&dataType}} {{paramName}}{{/isBodyParam}} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/build.bat.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/build.bat.mustache deleted file mode 100644 index e437bccf7d6f..000000000000 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/build.bat.mustache +++ /dev/null @@ -1,9 +0,0 @@ -:: 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/aspnetcorelib/2.0/build.sh.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/build.sh.mustache deleted file mode 100644 index 3804359d7e26..000000000000 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/build.sh.mustache +++ /dev/null @@ -1,8 +0,0 @@ -#!/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/aspnetcorelib/2.0/controller.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/controller.mustache deleted file mode 100644 index ba152878544e..000000000000 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/controller.mustache +++ /dev/null @@ -1,54 +0,0 @@ -{{>partial_header}} -using System; -using System.Collections.Generic; -using Microsoft.AspNetCore.Mvc;{{#useSwashbuckle}} -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 : Controller - { {{#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/aspnetcorelib/2.0/enumClass.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/enumClass.mustache deleted file mode 100644 index a8a68b998449..000000000000 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/enumClass.mustache +++ /dev/null @@ -1,18 +0,0 @@ - - /// - /// {{^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/aspnetcorelib/2.0/formParam.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/formParam.mustache deleted file mode 100644 index 2d42dc2916ba..000000000000 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/formParam.mustache +++ /dev/null @@ -1 +0,0 @@ -{{#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/aspnetcorelib/2.0/gitignore b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/gitignore deleted file mode 100644 index cd9b840e5498..000000000000 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/gitignore +++ /dev/null @@ -1,208 +0,0 @@ -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/aspnetcorelib/2.0/headerParam.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/headerParam.mustache deleted file mode 100644 index 45a5be9d7b5b..000000000000 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/headerParam.mustache +++ /dev/null @@ -1 +0,0 @@ -{{#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/aspnetcorelib/2.0/listReturn.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/listReturn.mustache deleted file mode 100644 index d609e67148c2..000000000000 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/listReturn.mustache +++ /dev/null @@ -1,4 +0,0 @@ - - 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/aspnetcorelib/2.0/mapReturn.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/mapReturn.mustache deleted file mode 100644 index 856fb1b3507c..000000000000 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/mapReturn.mustache +++ /dev/null @@ -1,4 +0,0 @@ - - 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/aspnetcorelib/2.0/model.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/model.mustache deleted file mode 100644 index 4ff19b963b8a..000000000000 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/model.mustache +++ /dev/null @@ -1,136 +0,0 @@ -{{>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}}{{^isContainer}} - ( - {{name}} == other.{{name}} || - {{name}} != null && - {{name}}.Equals(other.{{name}}) - ){{#hasMore}} && {{/hasMore}}{{/isContainer}}{{#isContainer}} - ( - {{name}} == other.{{name}} || - {{name}} != null && - {{name}}.SequenceEqual(other.{{name}}) - ){{#hasMore}} && {{/hasMore}}{{/isContainer}}{{/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/aspnetcorelib/2.0/objectReturn.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/objectReturn.mustache deleted file mode 100644 index 4059a61ac0b8..000000000000 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/objectReturn.mustache +++ /dev/null @@ -1,4 +0,0 @@ - - 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/aspnetcorelib/2.0/partial_header.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/partial_header.mustache deleted file mode 100644 index 4a682818a37a..000000000000 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/partial_header.mustache +++ /dev/null @@ -1,13 +0,0 @@ -/* - {{#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/aspnetcorelib/2.0/pathParam.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/pathParam.mustache deleted file mode 100644 index 70303432d48d..000000000000 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/pathParam.mustache +++ /dev/null @@ -1 +0,0 @@ -{{#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/aspnetcorelib/2.0/queryParam.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/queryParam.mustache deleted file mode 100644 index e9fa09b005d3..000000000000 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/queryParam.mustache +++ /dev/null @@ -1 +0,0 @@ -{{#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/aspnetcorelib/2.0/tags.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/tags.mustache deleted file mode 100644 index c97df19949e6..000000000000 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/tags.mustache +++ /dev/null @@ -1 +0,0 @@ -{{!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/aspnetcorelib/2.0/validateModel.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/validateModel.mustache deleted file mode 100644 index e11aaa5d2708..000000000000 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/validateModel.mustache +++ /dev/null @@ -1,61 +0,0 @@ -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/aspnetcorelib/2.0/web.config b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/web.config deleted file mode 100644 index a3b9f6add9c3..000000000000 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/web.config +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - - - diff --git a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/wwwroot/README.md b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/wwwroot/README.md deleted file mode 100644 index 6a0b78471a33..000000000000 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/wwwroot/README.md +++ /dev/null @@ -1,42 +0,0 @@ -# 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/aspnetcorelib/2.0/wwwroot/index.html b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/wwwroot/index.html deleted file mode 100644 index cde1f2f90b92..000000000000 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/wwwroot/index.html +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/wwwroot/openapi-original.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/wwwroot/openapi-original.mustache deleted file mode 100644 index 2c1b461cf000..000000000000 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/wwwroot/openapi-original.mustache +++ /dev/null @@ -1 +0,0 @@ -{{{openapi-json}}} diff --git a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/wwwroot/web.config b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/wwwroot/web.config deleted file mode 100644 index e70a7778d60b..000000000000 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.0/wwwroot/web.config +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - diff --git a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/Project.csproj.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/Project.csproj.mustache index ecbf96630a15..c8670925a4ec 100644 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/Project.csproj.mustache +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/Project.csproj.mustache @@ -11,7 +11,8 @@ - + + diff --git a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/Solution.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/Solution.mustache deleted file mode 100644 index 8c6d69ea93da..000000000000 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/Solution.mustache +++ /dev/null @@ -1,22 +0,0 @@ - -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/aspnetcorelib/2.1/bindControllers.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/bindControllers.mustache new file mode 100644 index 000000000000..61e79e05a46e --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/bindControllers.mustache @@ -0,0 +1,24 @@ +{{>partial_header}} +using System; +using System.Reflection; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.DependencyInjection; + +namespace {{packageName}} +{ + /// + /// + /// + public static class ControllerBinder + { + /// + /// + /// + public static IMvcCoreBuilder Add{{controllerBindingMethodName}}(this IMvcCoreBuilder builder) + { + var assembly = Assembly.Load("{{packageName}}"); + builder.AddApplicationPart(assembly); + return builder; + } + } +} \ No newline at end of file From 845276d6eb8578e059b93192c02d979f39cacdd2 Mon Sep 17 00:00:00 2001 From: Aaron Long Date: Tue, 8 Jan 2019 11:00:35 -0500 Subject: [PATCH 4/5] Refactored failed attempt - Make controller abstract so they aren't automatically picked up --- .../AspNetCoreServerLibraryCodegen.java | 19 +-------------- .../aspnetcorelib/2.1/Project.csproj.mustache | 2 +- .../2.1/bindControllers.mustache | 24 ------------------- .../aspnetcorelib/2.1/controller.mustache | 2 +- .../aspnetcorelib/2.1/listReturn.mustache | 4 ---- .../aspnetcorelib/2.1/mapReturn.mustache | 4 ---- .../aspnetcorelib/2.1/objectReturn.mustache | 4 ---- 7 files changed, 3 insertions(+), 56 deletions(-) delete mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/bindControllers.mustache delete mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/listReturn.mustache delete mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/mapReturn.mustache delete mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/objectReturn.mustache diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetCoreServerLibraryCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetCoreServerLibraryCodegen.java index e7d684801ee0..fa0d0ac4c5a2 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetCoreServerLibraryCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetCoreServerLibraryCodegen.java @@ -33,25 +33,17 @@ import java.util.Locale; import java.util.Map; -import static java.util.UUID.randomUUID; - public class AspNetCoreServerLibraryCodegen 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) + "}"; - @SuppressWarnings("hiding") protected Logger LOGGER = LoggerFactory.getLogger(AspNetCoreServerLibraryCodegen.class); - private boolean useSwashbuckle = true; protected int serverPort = 8080; protected String serverHost = "0.0.0.0"; protected String aspnetCoreVersion= "2.1"; // default to 2.1 - private String controllerBindingMethodName; - public AspNetCoreServerLibraryCodegen() { super(); @@ -62,8 +54,6 @@ public AspNetCoreServerLibraryCodegen() { embeddedTemplateDir = templateDir = "aspnetcorelib/2.1"; - controllerBindingMethodName =packageName.replace(".", ""); - // contextually reserved words // NOTE: C# uses camel cased reserved words, while models are title cased. We don't want lowercase comparisons. reservedWords.addAll( @@ -123,7 +113,7 @@ public String getName() { @Override public String getHelp() { - return "Generates an ASP.NET Core Web API server."; + return "Generates an ASP.NET Core Web API Library."; } @Override @@ -153,11 +143,6 @@ public void processOpts() { supportingFiles.add(new SupportingFile("validateModel.mustache", packageFolder + File.separator + "Attributes", "ValidateModelStateAttribute.cs")); supportingFiles.add(new SupportingFile("Project.csproj.mustache", packageFolder, packageName + ".csproj")); - supportingFiles.add(new SupportingFile("bindControllers.mustache", packageFolder, controllerBindingMethodName + ".cs")); - } - - public void setPackageGuid(String packageGuid) { - this.packageGuid = packageGuid; } public void setAspnetCoreVersion(String aspnetCoreVersion) { @@ -193,8 +178,6 @@ protected void processOperation(CodegenOperation operation) { } } - additionalProperties.put("controllerBindingMethodName", controllerBindingMethodName); - // Converts, for example, PUT to HttpPut for controller attributes operation.httpMethod = "Http" + operation.httpMethod.substring(0, 1) + operation.httpMethod.substring(1).toLowerCase(Locale.ROOT); } diff --git a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/Project.csproj.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/Project.csproj.mustache index c8670925a4ec..ae7fdd806fc6 100644 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/Project.csproj.mustache +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/Project.csproj.mustache @@ -12,7 +12,7 @@ - + diff --git a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/bindControllers.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/bindControllers.mustache deleted file mode 100644 index 61e79e05a46e..000000000000 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/bindControllers.mustache +++ /dev/null @@ -1,24 +0,0 @@ -{{>partial_header}} -using System; -using System.Reflection; -using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.DependencyInjection; - -namespace {{packageName}} -{ - /// - /// - /// - public static class ControllerBinder - { - /// - /// - /// - public static IMvcCoreBuilder Add{{controllerBindingMethodName}}(this IMvcCoreBuilder builder) - { - var assembly = Assembly.Load("{{packageName}}"); - builder.AddApplicationPart(assembly); - return builder; - } - } -} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/controller.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/controller.mustache index 8a627abe9444..820125d2d322 100644 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/controller.mustache +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/controller.mustache @@ -32,7 +32,7 @@ namespace {{packageName}}.Controllers /// {{description}} /// {{#description}} [Description("{{description}}")]{{/description}} - public class {{classname}}Controller : ControllerBase + public abstract class {{classname}}AbstractController : ControllerBase { private readonly I{{classname}}Implementation _impl; diff --git a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/listReturn.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/listReturn.mustache deleted file mode 100644 index d609e67148c2..000000000000 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/listReturn.mustache +++ /dev/null @@ -1,4 +0,0 @@ - - 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/aspnetcorelib/2.1/mapReturn.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/mapReturn.mustache deleted file mode 100644 index 856fb1b3507c..000000000000 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/mapReturn.mustache +++ /dev/null @@ -1,4 +0,0 @@ - - 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/aspnetcorelib/2.1/objectReturn.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/objectReturn.mustache deleted file mode 100644 index 4059a61ac0b8..000000000000 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/objectReturn.mustache +++ /dev/null @@ -1,4 +0,0 @@ - - var example = exampleJson != null - ? JsonConvert.DeserializeObject<{{#returnType}}{{{returnType}}}{{/returnType}}>(exampleJson) - : default({{#returnType}}{{{returnType}}}{{/returnType}}); \ No newline at end of file From 69ef0603c074809deb24291ad34a928d1f71fae2 Mon Sep 17 00:00:00 2001 From: Aaron Long Date: Tue, 8 Jan 2019 13:46:44 -0500 Subject: [PATCH 5/5] Make controllers overrideable by config --- .../main/java/org/openapitools/codegen/CodegenConstants.java | 3 +++ .../codegen/languages/AspNetCoreServerLibraryCodegen.java | 5 +++++ .../aspnetcorelib/2.1/abstractConcreteClassName.mustache | 1 + .../src/main/resources/aspnetcorelib/2.1/controller.mustache | 4 ++-- 4 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/abstractConcreteClassName.mustache diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java index 21f921fc6551..609cac2ffdd2 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java @@ -160,6 +160,9 @@ public class CodegenConstants { public static final String NETCORE_PROJECT_FILE = "netCoreProjectFile"; public static final String NETCORE_PROJECT_FILE_DESC = "Use the new format (.NET Core) for .NET project files (.csproj)."; + public static final String OVERRIDE_CONTROLLERS = "overrideControllers"; + public static final String OVERRIDE_CONTROLLERS_DESC = "Make generated controllers for AspNetCoreLibrary abstract and not automatically bound"; + public static final String USE_COLLECTION = "useCollection"; public static final String USE_COLLECTION_DESC = "Deserialize array types to Collection instead of List."; diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetCoreServerLibraryCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetCoreServerLibraryCodegen.java index fa0d0ac4c5a2..eedc0c63ea56 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetCoreServerLibraryCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetCoreServerLibraryCodegen.java @@ -99,6 +99,11 @@ public AspNetCoreServerLibraryCodegen() { addSwitch(CodegenConstants.RETURN_ICOLLECTION, CodegenConstants.RETURN_ICOLLECTION_DESC, returnICollection); + + + addSwitch(CodegenConstants.OVERRIDE_CONTROLLERS, + CodegenConstants.OVERRIDE_CONTROLLERS_DESC, + true); } @Override diff --git a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/abstractConcreteClassName.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/abstractConcreteClassName.mustache new file mode 100644 index 000000000000..cd8a9476f581 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/abstractConcreteClassName.mustache @@ -0,0 +1 @@ +{{classname}}{{#overrideControllers}}Abstract{{/overrideControllers}}Controller \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/controller.mustache b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/controller.mustache index 820125d2d322..6828d62d0422 100644 --- a/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/controller.mustache +++ b/modules/openapi-generator/src/main/resources/aspnetcorelib/2.1/controller.mustache @@ -32,7 +32,7 @@ namespace {{packageName}}.Controllers /// {{description}} /// {{#description}} [Description("{{description}}")]{{/description}} - public abstract class {{classname}}AbstractController : ControllerBase + public {{#overrideControllers}}abstract {{/overrideControllers}}class {{>abstractConcreteClassName}} : ControllerBase { private readonly I{{classname}}Implementation _impl; @@ -40,7 +40,7 @@ namespace {{packageName}}.Controllers /// The IOC injected implementation /// Controller's implementation /// - public {{classname}}Controller(I{{classname}}Implementation impl) + public {{>abstractConcreteClassName}}(I{{classname}}Implementation impl) { _impl = impl; }