Skip to content

Commit 4824fe1

Browse files
authored
Merge pull request #457 from ChinthakaJ98/api-port-fix
Update OpenAPI spec generation flow
2 parents 241ce50 + bcb010f commit 4824fe1

File tree

4 files changed

+22
-17
lines changed

4 files changed

+22
-17
lines changed

org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/customservice/synapse/api/generator/OpenAPIProcessor.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ private void updateServersSection(OpenAPI openAPI) {
192192
}
193193
}
194194
} else {
195-
addServersSection(openAPI, SwaggerConstants.DEFAULT_PORT);
195+
addServersSection(openAPI, SwaggerConstants.DEFAULT_HTTP_PORT);
196196
}
197197
}
198198

@@ -209,16 +209,20 @@ private void addServersSection(OpenAPI openAPI, int port) {
209209
} else {
210210
basePath = api.getContext();
211211
}
212-
String host;
213-
String scheme = SwaggerConstants.PROTOCOL_HTTP;
212+
String httpHost, httpsHost;
213+
int offset = SwaggerConstants.DEFAULT_HTTP_PORT - port;
214214
if (StringUtils.isNotBlank(api.getHostname()) && !"-1".equals(api.getPort())) {
215-
host = api.getHostname() + ":" + api.getPort();
215+
httpHost = api.getHostname() + ":" + port;
216+
httpsHost = api.getHostname() + ":" + (SwaggerConstants.DEFAULT_HTTPS_PORT + offset);
216217
} else {
217-
host = SwaggerConstants.DEFAULT_HOST + ":" + port;
218+
httpHost = SwaggerConstants.DEFAULT_HOST + ":" + port;
219+
httpsHost = SwaggerConstants.DEFAULT_HOST + ":" + (SwaggerConstants.DEFAULT_HTTPS_PORT + offset);
218220
}
219-
Server server = new Server();
220-
server.setUrl(scheme + "://" + host + basePath);
221-
openAPI.setServers(Arrays.asList(server));
221+
Server httpServer = new Server();
222+
httpServer.setUrl(SwaggerConstants.PROTOCOL_HTTP + "://" + httpHost + basePath);
223+
Server httpsServer = new Server();
224+
httpsServer.setUrl(SwaggerConstants.PROTOCOL_HTTPS + "://" + httpsHost + basePath);
225+
openAPI.setServers(Arrays.asList(httpServer, httpsServer));
222226
}
223227

224228
/**
@@ -337,7 +341,7 @@ private void addDefaultRequestBody(Operation operation, Map.Entry methodEntry) {
337341
* @param isJSONOut output swagger data type JSON / YAML.
338342
* @return updated swagger definition as string.
339343
*/
340-
public String getUpdatedSwaggerFromApi(String existingSwagger, boolean isJSONIn, boolean isJSONOut)
344+
public String getUpdatedSwaggerFromApi(String existingSwagger, boolean isJSONIn, boolean isJSONOut, int port)
341345
throws APIGenException {
342346

343347
if (api == null) {
@@ -489,7 +493,7 @@ public String getUpdatedSwaggerFromApi(String existingSwagger, boolean isJSONIn,
489493
// Adding the new path map
490494
openAPI.setPaths(newPaths);
491495
updateInfoSection(openAPI);
492-
updateServersSection(openAPI);
496+
addServersSection(openAPI, port);
493497

494498
try {
495499
if (isJSONOut) {

org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/customservice/synapse/api/generator/RestApiAdmin.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ public GenerateSwaggerResponse generateSwaggerFromAPI(GenerateSwaggerParam param
352352
File existingSwaggerFile = new File(param.swaggerPath);
353353
if (existingSwaggerFile.exists()) {
354354
return generateUpdatedSwaggerFromAPI(existingSwaggerFile, param.isJsonIn, param.isJsonOut,
355-
param.apiPath);
355+
param.apiPath, param.port);
356356
}
357357
}
358358
return generateSwaggerFromSynapseAPIByFormat(param.apiPath, param.isJsonOut, param.port);
@@ -416,7 +416,7 @@ public String generateSwaggerFromSynapseAPIByFormat(API api, boolean isJSON, int
416416
}
417417

418418
private GenerateSwaggerResponse generateUpdatedSwaggerFromAPI(File existingSwaggerFile, boolean isJSONIn,
419-
boolean isJSONOut, String apiPath) {
419+
boolean isJSONOut, String apiPath, int port) {
420420

421421
GenerateSwaggerResponse response = new GenerateSwaggerResponse();
422422
API api;
@@ -432,7 +432,7 @@ private GenerateSwaggerResponse generateUpdatedSwaggerFromAPI(File existingSwagg
432432

433433
try {
434434
String swaggerContent = Utils.readFile(existingSwaggerFile);
435-
String generatedSwagger = generateUpdatedSwaggerFromAPI(swaggerContent, isJSONIn, isJSONOut, api);
435+
String generatedSwagger = generateUpdatedSwaggerFromAPI(swaggerContent, isJSONIn, isJSONOut, api, port);
436436
response.setSwagger(generatedSwagger);
437437
} catch (IOException e) {
438438
LOGGER.log(Level.SEVERE, "Error occurred while reading the existing Swagger file.", e);
@@ -454,10 +454,10 @@ private GenerateSwaggerResponse generateUpdatedSwaggerFromAPI(File existingSwagg
454454
* @return OpenApi definition of the updated API.
455455
* @throws APIGenException Error occurred while generating the updated definition.
456456
*/
457-
public String generateUpdatedSwaggerFromAPI(String existingSwagger, boolean isJSONIn, boolean isJSONOut, API api)
457+
public String generateUpdatedSwaggerFromAPI(String existingSwagger, boolean isJSONIn, boolean isJSONOut, API api, int port)
458458
throws APIGenException {
459459

460460
OpenAPIProcessor openAPIProcessor = new OpenAPIProcessor(api);
461-
return openAPIProcessor.getUpdatedSwaggerFromApi(existingSwagger, isJSONIn, isJSONOut);
461+
return openAPIProcessor.getUpdatedSwaggerFromApi(existingSwagger, isJSONIn, isJSONOut, port);
462462
}
463463
}

org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/customservice/synapse/api/generator/SwaggerConstants.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ public class SwaggerConstants {
178178
/**
179179
* Default port for Swagger API
180180
*/
181-
public static int DEFAULT_PORT = 8290;
181+
public static int DEFAULT_HTTP_PORT = 8290;
182+
public static int DEFAULT_HTTPS_PORT = 8253;
182183

183184
/**
184185
* Path param regex

org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/customservice/synapse/api/generator/pojo/GenerateSwaggerParam.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ public class GenerateSwaggerParam {
2222
public String swaggerPath;
2323
public boolean isJsonIn;
2424
public boolean isJsonOut;
25-
public int port = SwaggerConstants.DEFAULT_PORT;
25+
public int port = SwaggerConstants.DEFAULT_HTTP_PORT;
2626
}

0 commit comments

Comments
 (0)