Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle generic types in Dubbo request bodies #320

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.bson.json.JsonObject;
import org.springframework.http.HttpHeaders;

/**
Expand Down Expand Up @@ -181,9 +182,54 @@ private DubboParameters getDubboParameters(ReplayActionCaseItem caseItem) {
DubboParameters dubboParameters = new DubboParameters();
dubboParameters.setParameterTypes(toParameterTypes(type));
dubboParameters.setParameters(toParameters(body, type));
annotateParameterDataWithClass(dubboParameters, getRecordRequestGenericParameterType(caseItem));
return dubboParameters;
}

private String getRecordRequestGenericParameterType(ReplayActionCaseItem caseItem) {
if (caseItem == null || caseItem.getTargetRequest() == null) {
return null;
}
String recordRequestType = (String) caseItem.getTargetRequest().getAttribute("recordRequestType");
if (recordRequestType == null) {
return null;
}
recordRequestType = recordRequestType.replaceAll("[\"\\]]+$", "");
String[] parts = recordRequestType.split("-");
return parts.length > 0 ? parts[parts.length - 1] : null;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

“recordRequestType” may have more than one and should needs to correspond to origin parameterTypes.

}

private void annotateParameterDataWithClass(DubboParameters dubboParameters, String recordRequestGenericParameterType) {
if (recordRequestGenericParameterType == null) {
return;
}
List<Object> parameters = dubboParameters.getParameters();
if (parameters.isEmpty()) {
return;
}
Object firstParameter = parameters.get(0);
if (firstParameter instanceof JSONObject) {
JSONObject parameter = (JSONObject) firstParameter;
modifyJsonObjectWithClass(parameter, "data", recordRequestGenericParameterType);
}
}

private void modifyJsonObjectWithClass(JSONObject jsonObject, String key, String className) {
if (!jsonObject.containsKey(key)) {
return;
}
Object dataObject = jsonObject.get(key);
if (dataObject instanceof JSONObject) {
((JSONObject) dataObject).put("class", className);
} else if (dataObject instanceof JSONArray) {
for (Object item : (JSONArray) dataObject) {
if (item instanceof JSONObject) {
((JSONObject) item).put("class", className);
}
}
}
}

public static List<String> toParameterTypes(String type) {
List<String> parameterTypes = new ArrayList<>();
if (StringUtils.isNotEmpty(type)) {
Expand Down
Loading