Skip to content

Commit 6d88d07

Browse files
ackintoshwing328
authored andcommitted
[NodeJS] make serverPort configurable via CLI option (#7899)
* Add "serverPort" option * Use port number passed via CLI option if specified * Replace hand-written param name with the constant * Rename serverPort -> defaultServerPort * Fix failed test https://travis-ci.org/swagger-api/swagger-codegen/builds/357674590
1 parent fc7e083 commit 6d88d07

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/NodeJSServerCodegen.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ public class NodeJSServerCodegen extends DefaultCodegen implements CodegenConfig
2828
protected String implFolder = "service";
2929
public static final String GOOGLE_CLOUD_FUNCTIONS = "googleCloudFunctions";
3030
public static final String EXPORTED_NAME = "exportedName";
31+
public static final String SERVER_PORT = "serverPort";
3132

3233
protected String apiVersion = "1.0.0";
33-
protected int serverPort = 8080;
3434
protected String projectName = "swagger-server";
35+
protected String defaultServerPort = "8080";
3536

3637
protected boolean googleCloudFunctions;
3738
protected String exportedName;
@@ -82,7 +83,6 @@ public NodeJSServerCodegen() {
8283
* are available in models, apis, and supporting files
8384
*/
8485
additionalProperties.put("apiVersion", apiVersion);
85-
additionalProperties.put("serverPort", serverPort);
8686
additionalProperties.put("implFolder", implFolder);
8787

8888
supportingFiles.add(new SupportingFile("writer.mustache", ("utils").replace(".", File.separator), "writer.js"));
@@ -96,6 +96,8 @@ public NodeJSServerCodegen() {
9696
"When the generated code will be deployed to Google Cloud Functions, this option can be "
9797
+ "used to update the name of the exported function. By default, it refers to the "
9898
+ "basePath. This does not affect normal standalone nodejs server code."));
99+
cliOptions.add(new CliOption(SERVER_PORT,
100+
"TCP port to listen on."));
99101
}
100102

101103
@Override
@@ -318,7 +320,7 @@ public void processOpts() {
318320
@Override
319321
public void preprocessSwagger(Swagger swagger) {
320322
String host = swagger.getHost();
321-
String port = "8080";
323+
String port = defaultServerPort;
322324

323325
if (!StringUtils.isEmpty(host)) {
324326
String[] parts = host.split(":");
@@ -331,7 +333,10 @@ public void preprocessSwagger(Swagger swagger) {
331333
LOGGER.warn("'host' in the specification is empty or undefined. Default to http://localhost.");
332334
}
333335

334-
this.additionalProperties.put("serverPort", port);
336+
if (additionalProperties.containsKey(SERVER_PORT)) {
337+
port = additionalProperties.get(SERVER_PORT).toString();
338+
}
339+
this.additionalProperties.put(SERVER_PORT, port);
335340

336341
if (swagger.getInfo() != null) {
337342
Info info = swagger.getInfo();

modules/swagger-codegen/src/test/java/io/swagger/codegen/options/NodeJSServerOptionsProvider.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class NodeJSServerOptionsProvider implements OptionsProvider {
1212
public static final String GOOGLE_CLOUD_FUNCTIONS = "false";
1313
public static final String EXPORTED_NAME = "exported";
1414
public static final String ALLOW_UNICODE_IDENTIFIERS_VALUE = "false";
15+
public static final String SERVER_PORT = "8080";
1516

1617

1718
@Override
@@ -26,6 +27,7 @@ public Map<String, String> createOptions() {
2627
.put(CodegenConstants.ENSURE_UNIQUE_PARAMS, ENSURE_UNIQUE_PARAMS_VALUE)
2728
.put(NodeJSServerCodegen.GOOGLE_CLOUD_FUNCTIONS, GOOGLE_CLOUD_FUNCTIONS)
2829
.put(NodeJSServerCodegen.EXPORTED_NAME, EXPORTED_NAME)
30+
.put(NodeJSServerCodegen.SERVER_PORT, SERVER_PORT)
2931
.put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE)
3032
.build();
3133
}

0 commit comments

Comments
 (0)