Skip to content

Commit

Permalink
[Refactor] create a static function to build map of codegen model (Op…
Browse files Browse the repository at this point in the history
…enAPITools#18138)

* refactor to build map of codegen model

* update
  • Loading branch information
wing328 authored and zapodot committed Mar 21, 2024
1 parent a1bf5e5 commit fd918f7
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,8 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo

additionalProperties.put("x-http-statuses-with-return", httpStatusesWithReturn);

HashMap<String, CodegenModel> modelMaps = ModelMap.toCodegenModelMap(allModels);

if (objs != null) {
OperationMap operations = objs.getOperations();
if (operations != null) {
Expand Down Expand Up @@ -1041,57 +1043,57 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
}

for (CodegenParameter parameter : operation.allParams) {
CodegenModel model = getModelFromParameter(allModels, parameter);
CodegenModel model = getModelFromParameter(modelMaps, parameter);
patchParameter(model, parameter);
}

for (CodegenParameter parameter : operation.bodyParams) {
CodegenModel model = getModelFromParameter(allModels, parameter);
CodegenModel model = getModelFromParameter(modelMaps, parameter);
patchParameter(model, parameter);
}

for (CodegenParameter parameter : operation.cookieParams) {
CodegenModel model = getModelFromParameter(allModels, parameter);
CodegenModel model = getModelFromParameter(modelMaps, parameter);
patchParameter(model, parameter);
}

for (CodegenParameter parameter : operation.formParams) {
CodegenModel model = getModelFromParameter(allModels, parameter);
CodegenModel model = getModelFromParameter(modelMaps, parameter);
patchParameter(model, parameter);
}

for (CodegenParameter parameter : operation.headerParams) {
CodegenModel model = getModelFromParameter(allModels, parameter);
CodegenModel model = getModelFromParameter(modelMaps, parameter);
patchParameter(model, parameter);
}

for (CodegenParameter parameter : operation.implicitHeadersParams) {
CodegenModel model = getModelFromParameter(allModels, parameter);
CodegenModel model = getModelFromParameter(modelMaps, parameter);
patchParameter(model, parameter);
}

for (CodegenParameter parameter : operation.optionalParams) {
CodegenModel model = getModelFromParameter(allModels, parameter);
CodegenModel model = getModelFromParameter(modelMaps, parameter);
patchParameter(model, parameter);
}

for (CodegenParameter parameter : operation.pathParams) {
CodegenModel model = getModelFromParameter(allModels, parameter);
CodegenModel model = getModelFromParameter(modelMaps, parameter);
patchParameter(model, parameter);
}

for (CodegenParameter parameter : operation.queryParams) {
CodegenModel model = getModelFromParameter(allModels, parameter);
CodegenModel model = getModelFromParameter(modelMaps, parameter);
patchParameter(model, parameter);
}

for (CodegenParameter parameter : operation.notNullableParams) {
CodegenModel model = getModelFromParameter(allModels, parameter);
CodegenModel model = getModelFromParameter(modelMaps, parameter);
patchParameter(model, parameter);
}

for (CodegenParameter parameter : operation.requiredParams) {
CodegenModel model = getModelFromParameter(allModels, parameter);
CodegenModel model = getModelFromParameter(modelMaps, parameter);
patchParameter(model, parameter);
}

Expand All @@ -1118,10 +1120,12 @@ protected void patchVendorExtensionNullableValueType(CodegenParameter parameter)
/**
* Returns the model related to the given parameter
*/
private CodegenModel getModelFromParameter(List<ModelMap> allModels, CodegenParameter parameter) {
return parameter.isModel || parameter.getIsEnumOrRef()
? allModels.stream().map(m -> m.getModel()).filter(m -> m.getClassname().equals(parameter.dataType)).findFirst().orElse(null)
: null;
private CodegenModel getModelFromParameter(HashMap<String, CodegenModel> allModels, CodegenParameter parameter) {
if (parameter.isModel || parameter.getIsEnumOrRef()) {
return allModels.getOrDefault(parameter.dataType, null);
} else {
return null;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -599,14 +599,9 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
return objs;
}
OperationMap operations = objs.getOperations();
HashMap<String, CodegenModel> modelMaps = new HashMap<>();
HashMap<String, CodegenModel> modelMaps = ModelMap.toCodegenModelMap(allModels);
HashMap<String, Integer> processedModelMaps = new HashMap<>();

for (ModelMap modelMap : allModels) {
CodegenModel m = modelMap.getModel();
modelMaps.put(m.classname, m);
}

List<CodegenOperation> operationList = operations.getOperation();
for (CodegenOperation op : operationList) {
for (CodegenParameter p : op.allParams) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -533,14 +533,9 @@ public ModelsMap postProcessModels(ModelsMap objs) {
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> allModels) {
objs = super.postProcessOperationsWithModels(objs, allModels);
OperationMap operations = objs.getOperations();
HashMap<String, CodegenModel> modelMaps = new HashMap<>();
HashMap<String, CodegenModel> modelMaps = ModelMap.toCodegenModelMap(allModels);
HashMap<String, ArrayList<Integer>> processedModelMaps = new HashMap<>();

for (ModelMap modelMap : allModels) {
CodegenModel m = modelMap.getModel();
modelMaps.put(m.classname, m);
}

List<CodegenOperation> operationList = operations.getOperation();
for (CodegenOperation op : operationList) {
for (CodegenParameter p : op.allParams) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1043,14 +1043,9 @@ public String toParamName(String name) {
@Override
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> allModels) {
OperationMap operations = objs.getOperations();
HashMap<String, CodegenModel> modelMaps = new HashMap<>();
HashMap<String, CodegenModel> modelMaps = ModelMap.toCodegenModelMap(allModels);
HashMap<String, Integer> processedModelMaps = new HashMap<>();

for (ModelMap modelMap : allModels) {
CodegenModel m = modelMap.getModel();
modelMaps.put(m.classname, m);
}

List<CodegenOperation> operationList = operations.getOperation();
for (CodegenOperation op : operationList) {
int index = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -911,13 +911,7 @@ public String toModelTestFilename(String name) {
@Override
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> allModels) {
OperationMap objectMap = objs.getOperations();

HashMap<String, CodegenModel> modelMaps = new HashMap<>();
for (ModelMap modelMap : allModels) {
CodegenModel m = modelMap.getModel();
modelMaps.put(m.classname, m);
}

HashMap<String, CodegenModel> modelMaps = ModelMap.toCodegenModelMap(allModels);
List<CodegenOperation> operations = objectMap.getOperation();
for (CodegenOperation operation : operations) {
for (CodegenParameter cp : operation.allParams) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -651,14 +651,9 @@ protected void addAdditionPropertiesToCodeGenModel(CodegenModel codegenModel, Sc
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> allModels) {
objs = super.postProcessOperationsWithModels(objs, allModels);
OperationMap operations = objs.getOperations();
HashMap<String, CodegenModel> modelMaps = new HashMap<>();
HashMap<String, CodegenModel> modelMaps = ModelMap.toCodegenModelMap(allModels);
HashMap<String, Integer> processedModelMaps = new HashMap<>();

for (ModelMap modelMap : allModels) {
CodegenModel m = modelMap.getModel();
modelMaps.put(m.classname, m);
}

List<CodegenOperation> operationList = operations.getOperation();
for (CodegenOperation op : operationList) {
for (CodegenParameter p : op.allParams) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1256,11 +1256,7 @@ public void postProcessFile(File file, String fileType) {
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> allModels) {
OperationMap objectMap = objs.getOperations();

HashMap<String, CodegenModel> modelMaps = new HashMap<>();
for (ModelMap modelMap : allModels) {
CodegenModel m = modelMap.getModel();
modelMaps.put(m.classname, m);
}
HashMap<String, CodegenModel> modelMaps = ModelMap.toCodegenModelMap(allModels);

List<CodegenOperation> operations = objectMap.getOperation();
for (CodegenOperation operation : operations) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -729,12 +729,7 @@ public void postProcessFile(File file, String fileType) {
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> allModels) {
Map<String, Object> objectMap = (Map<String, Object>) objs.get("operations");

HashMap<String, CodegenModel> modelMaps = new HashMap<String, CodegenModel>();
for (Object o : allModels) {
HashMap<String, Object> h = (HashMap<String, Object>) o;
CodegenModel m = (CodegenModel) h.get("model");
modelMaps.put(m.classname, m);
}
HashMap<String, CodegenModel> modelMaps = ModelMap.toCodegenModelMap(allModels);

List<CodegenOperation> operations = (List<CodegenOperation>) objectMap.get("operation");
for (CodegenOperation operation : operations) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.openapitools.codegen.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.openapitools.codegen.CodegenModel;
Expand All @@ -22,4 +23,21 @@ public void setModel(CodegenModel model) {
public CodegenModel getModel() {
return (CodegenModel) get("model");
}

/**
* Convert a list of ModelMap to map of CodegenModel.
*
* @param allModels list of model map
* @return map of Codegen Model
*/
static public HashMap<String, CodegenModel> toCodegenModelMap(List<ModelMap> allModels) {
HashMap<String, CodegenModel> modelMaps = new HashMap<>();

for (ModelMap modelMap : allModels) {
CodegenModel m = modelMap.getModel();
modelMaps.put(m.classname, m);
}

return modelMaps;
}
}

0 comments on commit fd918f7

Please sign in to comment.