From bece8d2a3984ee7b44d519251b7e629732df4df5 Mon Sep 17 00:00:00 2001 From: sunn <33183834+etherealjoy@users.noreply.github.com> Date: Tue, 3 Jul 2018 08:23:43 +0200 Subject: [PATCH] [aspnetcore] Add processing of Port defined in spec (#368) --- .../languages/AspNetCoreServerCodegen.java | 15 ++++++++++++++- .../main/resources/aspnetcore/Program.mustache | 3 ++- .../aspnetcore/src/Org.OpenAPITools/Program.cs | 3 ++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetCoreServerCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetCoreServerCodegen.java index ab6941032e..1c269a15d5 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetCoreServerCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetCoreServerCodegen.java @@ -21,13 +21,16 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.samskivert.mustache.Mustache; +import io.swagger.v3.oas.models.OpenAPI; + import org.openapitools.codegen.*; import org.openapitools.codegen.utils.ModelUtils; - +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.*; import static java.util.UUID.randomUUID; @@ -42,6 +45,9 @@ public class AspNetCoreServerCodegen extends AbstractCSharpCodegen { protected Logger LOGGER = LoggerFactory.getLogger(AspNetCoreServerCodegen.class); private boolean useSwashbuckle = true; + protected int serverPort = 8080; + protected String serverHost = "0.0.0.0"; + public AspNetCoreServerCodegen() { super(); @@ -112,6 +118,13 @@ public class AspNetCoreServerCodegen extends AbstractCSharpCodegen { 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() { diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/Program.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/Program.mustache index b71c858489..7de8377998 100644 --- a/modules/openapi-generator/src/main/resources/aspnetcore/Program.mustache +++ b/modules/openapi-generator/src/main/resources/aspnetcore/Program.mustache @@ -24,7 +24,8 @@ namespace {{packageName}} /// Webhost public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) - .UseStartup() + .UseStartup() + .UseUrls("http://0.0.0.0:{{#serverPort}}{{serverPort}}{{/serverPort}}{{^serverPort}}8080{{/serverPort}}/") .Build(); } } diff --git a/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Program.cs b/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Program.cs index d4fb205bac..3da6fd0a6d 100644 --- a/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Program.cs +++ b/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Program.cs @@ -24,7 +24,8 @@ namespace Org.OpenAPITools /// Webhost public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) - .UseStartup() + .UseStartup() + .UseUrls("http://0.0.0.0:8080/") .Build(); } }