forked from loafle/openapi-generator-original
add post processing to nodejs express server (#5369)
This commit is contained in:
@@ -887,8 +887,8 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
|
||||
String itemType = getSchemaType(ModelUtils.getAdditionalProperties(model));
|
||||
codegenModel.vendorExtensions.put("x-isMap", true); // TODO: 5.0 Remove
|
||||
codegenModel.vendorExtensions.put("x-is-map", true);
|
||||
codegenModel.vendorExtensions.put("x-itemType",itemType); // TODO: 5.0 Remove
|
||||
codegenModel.vendorExtensions.put("x-item-type",itemType);
|
||||
codegenModel.vendorExtensions.put("x-itemType", itemType); // TODO: 5.0 Remove
|
||||
codegenModel.vendorExtensions.put("x-item-type", itemType);
|
||||
} else {
|
||||
String type = model.getType();
|
||||
if (codegenModel != null && isPrimitiveType(type)) {
|
||||
|
||||
@@ -25,6 +25,8 @@ import io.swagger.v3.oas.models.PathItem;
|
||||
import io.swagger.v3.oas.models.PathItem.HttpMethod;
|
||||
import io.swagger.v3.oas.models.Paths;
|
||||
import io.swagger.v3.oas.models.info.Info;
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.openapitools.codegen.*;
|
||||
import org.openapitools.codegen.meta.GeneratorMetadata;
|
||||
import org.openapitools.codegen.meta.Stability;
|
||||
@@ -38,7 +40,7 @@ import java.net.URL;
|
||||
import java.util.*;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import static org.openapitools.codegen.utils.StringUtils.*;
|
||||
import static org.openapitools.codegen.utils.StringUtils.camelize;
|
||||
|
||||
public class NodeJSExpressServerCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
|
||||
@@ -76,8 +78,8 @@ public class NodeJSExpressServerCodegen extends DefaultCodegen implements Codege
|
||||
.build();
|
||||
|
||||
generatorMetadata = GeneratorMetadata.newBuilder(generatorMetadata)
|
||||
.stability(Stability.BETA)
|
||||
.build();
|
||||
.stability(Stability.BETA)
|
||||
.build();
|
||||
|
||||
outputFolder = "generated-code/nodejs-express-server";
|
||||
embeddedTemplateDir = templateDir = "nodejs-express-server";
|
||||
@@ -303,6 +305,11 @@ public class NodeJSExpressServerCodegen extends DefaultCodegen implements Codege
|
||||
public void processOpts() {
|
||||
super.processOpts();
|
||||
|
||||
if (StringUtils.isEmpty(System.getenv("JS_POST_PROCESS_FILE"))) {
|
||||
LOGGER.info("Environment variable JS_POST_PROCESS_FILE not defined so the JS code may not be properly formatted. To define it, try 'export JS_POST_PROCESS_FILE=\"/usr/local/bin/js-beautify -r -f\"' (Linux/Mac)");
|
||||
LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).");
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey(EXPORTED_NAME)) {
|
||||
setExportedName((String) additionalProperties.get(EXPORTED_NAME));
|
||||
}
|
||||
@@ -321,8 +328,8 @@ public class NodeJSExpressServerCodegen extends DefaultCodegen implements Codege
|
||||
@Override
|
||||
public void preprocessOpenAPI(OpenAPI openAPI) {
|
||||
URL url = URLPathUtils.getServerURL(openAPI, serverVariableOverrides());
|
||||
String host = URLPathUtils.getProtocolAndHost(url);
|
||||
String port = URLPathUtils.getPort(url, defaultServerPort) ;
|
||||
String host = URLPathUtils.getProtocolAndHost(url);
|
||||
String port = URLPathUtils.getPort(url, defaultServerPort);
|
||||
String basePath = url.getPath();
|
||||
|
||||
if (additionalProperties.containsKey(SERVER_PORT)) {
|
||||
@@ -407,4 +414,32 @@ public class NodeJSExpressServerCodegen extends DefaultCodegen implements Codege
|
||||
// remove " to avoid code injection
|
||||
return input.replace("\"", "");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postProcessFile(File file, String fileType) {
|
||||
if (file == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
String jsPostProcessFile = System.getenv("JS_POST_PROCESS_FILE");
|
||||
if (StringUtils.isEmpty(jsPostProcessFile)) {
|
||||
return; // skip if JS_POST_PROCESS_FILE env variable is not defined
|
||||
}
|
||||
|
||||
// only process files with js extension
|
||||
if ("js".equals(FilenameUtils.getExtension(file.toString()))) {
|
||||
String command = jsPostProcessFile + " " + file.toString();
|
||||
try {
|
||||
Process p = Runtime.getRuntime().exec(command);
|
||||
p.waitFor();
|
||||
int exitValue = p.exitValue();
|
||||
if (exitValue != 0) {
|
||||
LOGGER.error("Error running the command ({}). Exit code: {}", command, exitValue);
|
||||
}
|
||||
LOGGER.info("Successfully executed: " + command);
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user