diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/NodeJSServerCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/NodeJSServerCodegen.java index f5bac826b227..8812bcf67346 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/NodeJSServerCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/NodeJSServerCodegen.java @@ -28,10 +28,11 @@ public class NodeJSServerCodegen extends DefaultCodegen implements CodegenConfig protected String implFolder = "service"; public static final String GOOGLE_CLOUD_FUNCTIONS = "googleCloudFunctions"; public static final String EXPORTED_NAME = "exportedName"; + public static final String SERVER_PORT = "serverPort"; protected String apiVersion = "1.0.0"; - protected int serverPort = 8080; protected String projectName = "swagger-server"; + protected String defaultServerPort = "8080"; protected boolean googleCloudFunctions; protected String exportedName; @@ -82,7 +83,6 @@ public NodeJSServerCodegen() { * are available in models, apis, and supporting files */ additionalProperties.put("apiVersion", apiVersion); - additionalProperties.put("serverPort", serverPort); additionalProperties.put("implFolder", implFolder); supportingFiles.add(new SupportingFile("writer.mustache", ("utils").replace(".", File.separator), "writer.js")); @@ -96,6 +96,8 @@ public NodeJSServerCodegen() { "When the generated code will be deployed to Google Cloud Functions, this option can be " + "used to update the name of the exported function. By default, it refers to the " + "basePath. This does not affect normal standalone nodejs server code.")); + cliOptions.add(new CliOption(SERVER_PORT, + "TCP port to listen on.")); } @Override @@ -318,7 +320,7 @@ public void processOpts() { @Override public void preprocessSwagger(Swagger swagger) { String host = swagger.getHost(); - String port = "8080"; + String port = defaultServerPort; if (!StringUtils.isEmpty(host)) { String[] parts = host.split(":"); @@ -331,7 +333,10 @@ public void preprocessSwagger(Swagger swagger) { LOGGER.warn("'host' in the specification is empty or undefined. Default to http://localhost."); } - this.additionalProperties.put("serverPort", port); + if (additionalProperties.containsKey(SERVER_PORT)) { + port = additionalProperties.get(SERVER_PORT).toString(); + } + this.additionalProperties.put(SERVER_PORT, port); if (swagger.getInfo() != null) { Info info = swagger.getInfo(); diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/options/NodeJSServerOptionsProvider.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/options/NodeJSServerOptionsProvider.java index 992460d314c7..afb3e5d0bd6f 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/options/NodeJSServerOptionsProvider.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/options/NodeJSServerOptionsProvider.java @@ -12,6 +12,7 @@ public class NodeJSServerOptionsProvider implements OptionsProvider { public static final String GOOGLE_CLOUD_FUNCTIONS = "false"; public static final String EXPORTED_NAME = "exported"; public static final String ALLOW_UNICODE_IDENTIFIERS_VALUE = "false"; + public static final String SERVER_PORT = "8080"; @Override @@ -26,6 +27,7 @@ public Map createOptions() { .put(CodegenConstants.ENSURE_UNIQUE_PARAMS, ENSURE_UNIQUE_PARAMS_VALUE) .put(NodeJSServerCodegen.GOOGLE_CLOUD_FUNCTIONS, GOOGLE_CLOUD_FUNCTIONS) .put(NodeJSServerCodegen.EXPORTED_NAME, EXPORTED_NAME) + .put(NodeJSServerCodegen.SERVER_PORT, SERVER_PORT) .put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE) .build(); }