mirror of
https://github.com/jlengrand/openapi-generator.git
synced 2026-05-16 08:31:26 +00:00
JavaScript client: handle response deserialization
to support array of models and Date property in model Closes #1951
This commit is contained in:
@@ -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);
|
||||
}
|
||||
@@ -320,10 +319,18 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
|
||||
if (p instanceof RefProperty) {
|
||||
return ".constructFromObject(data['" + name + "']);";
|
||||
} else {
|
||||
return " = data['" + name + "'];";
|
||||
String type = normalizeType(getTypeDeclaration(p));
|
||||
return " = ApiClient.convertToType(data['" + name + "'], " + type + ");";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
public String getSwaggerType(Property p) {
|
||||
String swaggerType = super.getSwaggerType(p);
|
||||
@@ -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