mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2026-03-19 11:59:31 +00:00
Merge pull request #2030 from xhh/javascript-improvements
[JavaScript] some improvements and enhancements to JavaScript client
This commit is contained in:
@@ -164,11 +164,11 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
|
||||
|
||||
if (swagger.getInfo() != null) {
|
||||
Info info = swagger.getInfo();
|
||||
if (projectName == null && info.getTitle() != null) {
|
||||
if (StringUtils.isBlank(projectName) && info.getTitle() != null) {
|
||||
// when projectName is not specified, generate it from info.title
|
||||
projectName = dashize(info.getTitle());
|
||||
}
|
||||
if (projectVersion == null) {
|
||||
if (StringUtils.isBlank(projectVersion)) {
|
||||
// when projectVersion is not specified, use info.version
|
||||
projectVersion = info.getVersion();
|
||||
}
|
||||
@@ -185,13 +185,13 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
|
||||
}
|
||||
|
||||
// default values
|
||||
if (projectName == null) {
|
||||
if (StringUtils.isBlank(projectName)) {
|
||||
projectName = "swagger-js-client";
|
||||
}
|
||||
if (moduleName == null) {
|
||||
if (StringUtils.isBlank(moduleName)) {
|
||||
moduleName = camelize(underscore(projectName));
|
||||
}
|
||||
if (projectVersion == null) {
|
||||
if (StringUtils.isBlank(projectVersion)) {
|
||||
projectVersion = "1.0.0";
|
||||
}
|
||||
if (projectDescription == null) {
|
||||
@@ -290,14 +290,13 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
|
||||
@Override
|
||||
public String getTypeDeclaration(Property p) {
|
||||
if (p instanceof ArrayProperty) {
|
||||
// ArrayProperty ap = (ArrayProperty) p;
|
||||
// Property inner = ap.getItems();
|
||||
return getSwaggerType(p); // TODO: + "/* <" + getTypeDeclaration(inner) + "> */";
|
||||
ArrayProperty ap = (ArrayProperty) p;
|
||||
Property inner = ap.getItems();
|
||||
return "[" + getTypeDeclaration(inner) + "]";
|
||||
} else if (p instanceof MapProperty) {
|
||||
MapProperty mp = (MapProperty) p;
|
||||
Property inner = mp.getAdditionalProperties();
|
||||
|
||||
return getSwaggerType(p) + "<String, " + getTypeDeclaration(inner) + ">";
|
||||
return "{String: " + getTypeDeclaration(inner) + "}";
|
||||
}
|
||||
return super.getTypeDeclaration(p);
|
||||
}
|
||||
@@ -318,10 +317,18 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
|
||||
@Override
|
||||
public String toDefaultValueWithParam(String name, Property p) {
|
||||
if (p instanceof RefProperty) {
|
||||
return ".constructFromObject(data." + name + ");";
|
||||
return ".constructFromObject(data['" + name + "']);";
|
||||
} else {
|
||||
String type = normalizeType(getTypeDeclaration(p));
|
||||
return " = ApiClient.convertToType(data['" + name + "'], " + type + ");";
|
||||
}
|
||||
}
|
||||
|
||||
return super.toDefaultValueWithParam(name, p);
|
||||
/**
|
||||
* Normalize type by wrapping primitive types with single quotes.
|
||||
*/
|
||||
public String normalizeType(String type) {
|
||||
return type.replaceAll("\\b(Boolean|Integer|Number|String|Date)\\b", "'$1'");
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -357,6 +364,15 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
|
||||
return camelize(sanitizeName(operationId), true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Model> definitions, Swagger swagger) {
|
||||
CodegenOperation op = super.fromOperation(path, httpMethod, operation, definitions, swagger);
|
||||
if (op.returnType != null) {
|
||||
op.returnType = normalizeType(op.returnType);
|
||||
}
|
||||
return op;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CodegenModel fromModel(String name, Model model, Map<String, Model> allDefinitions) {
|
||||
CodegenModel codegenModel = super.fromModel(name, model, allDefinitions);
|
||||
@@ -417,11 +433,6 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
|
||||
return objs;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
|
||||
return objs;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean needToImport(String type) {
|
||||
return !defaultIncludes.contains(type)
|
||||
|
||||
Reference in New Issue
Block a user