Skip to content

Commit d14a423

Browse files
MaMengzhenAias00loongs-zhang
authored
fix 'addCustomHeaders' header value replace bug (#6257)
Co-authored-by: aias00 <[email protected]> Co-authored-by: loongs-zhang <[email protected]>
1 parent dcdd727 commit d14a423

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

shenyu-plugin/shenyu-plugin-mcp-server/src/main/java/org/apache/shenyu/plugin/mcp/server/callback/ShenyuToolCallback.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ private RequestConfig buildRequestConfig(final String configStr, final JsonObjec
346346
// Build body with parameter formatting (only format body parameters that need it)
347347
final JsonObject bodyJson = buildFormattedBodyJson(argsToJsonBody, argsPosition, inputJson);
348348

349-
return new RequestConfig(method, path, bodyJson, requestTemplate, argsToJsonBody);
349+
return new RequestConfig(method, path, bodyJson, requestTemplate, argsToJsonBody, inputJson);
350350
}
351351

352352
/**
@@ -470,10 +470,20 @@ private void addCustomHeaders(final ServerHttpRequest.Builder requestBuilder,
470470
if (requestConfig.getRequestTemplate().has("headers")) {
471471
for (final var headerElem : requestConfig.getRequestTemplate().getAsJsonArray("headers")) {
472472
final JsonObject headerObj = headerElem.getAsJsonObject();
473-
requestBuilder.header(
474-
headerObj.get("key").getAsString(),
475-
headerObj.get("value").getAsString()
476-
);
473+
String headerKey = headerObj.get("key").getAsString();
474+
String headerValue = headerObj.get("value").getAsString();
475+
if (headerValue.contains("{{.")) {
476+
JsonObject inputJson = requestConfig.getInputJson();
477+
if (Objects.nonNull(inputJson)) {
478+
for (String key : inputJson.keySet()) {
479+
if (inputJson.get(key).isJsonPrimitive() && inputJson.get(key).getAsJsonPrimitive().isString()) {
480+
String value = inputJson.get(key).getAsString();
481+
headerValue = headerValue.replace("{{." + key + "}}", value);
482+
}
483+
}
484+
}
485+
}
486+
requestBuilder.header(headerKey, headerValue);
477487
}
478488
}
479489
}

shenyu-plugin/shenyu-plugin-mcp-server/src/main/java/org/apache/shenyu/plugin/mcp/server/request/RequestConfig.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@ public class RequestConfig {
3030
private JsonObject requestTemplate;
3131

3232
private boolean argsToJsonBody;
33+
34+
private JsonObject inputJson;
3335

34-
public RequestConfig(final String method, final String path, final JsonObject bodyJson, final JsonObject requestTemplate, final boolean argsToJsonBody) {
36+
public RequestConfig(final String method, final String path, final JsonObject bodyJson, final JsonObject requestTemplate, final boolean argsToJsonBody, final JsonObject inputJson) {
3537
this.method = method;
3638
this.path = path;
3739
this.bodyJson = bodyJson;
3840
this.requestTemplate = requestTemplate;
3941
this.argsToJsonBody = argsToJsonBody;
42+
this.inputJson = inputJson;
4043
}
4144

4245
public String getMethod() {
@@ -78,4 +81,12 @@ public boolean isArgsToJsonBody() {
7881
public void setArgsToJsonBody(final boolean argsToJsonBody) {
7982
this.argsToJsonBody = argsToJsonBody;
8083
}
84+
85+
public JsonObject getInputJson() {
86+
return inputJson;
87+
}
88+
89+
public void setInputJson(final JsonObject inputJson) {
90+
this.inputJson = inputJson;
91+
}
8192
}

0 commit comments

Comments
 (0)