This is an automated email from the ASF dual-hosted git repository.

Aias00 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new fe541de70b fix: reuse a static ObjectMapper in the ai response 
transformer plugin (#6670) (#7173)
fe541de70b is described below

commit fe541de70bffc7fc6e2d6ffd8a2c34536afd5a21
Author: wy471x <[email protected]>
AuthorDate: Sat Sep 26 16:51:18 2026 +0800

    fix: reuse a static ObjectMapper in the ai response transformer plugin 
(#6670) (#7173)
    
    Co-authored-by: aias00 <[email protected]>
---
 .../transformer/response/AiResponseTransformerPlugin.java |  9 +++++----
 .../response/AiResponseTransformerPluginTest.java         | 15 +++++++++++++++
 2 files changed, 20 insertions(+), 4 deletions(-)

diff --git 
a/shenyu-plugin/shenyu-plugin-ai/shenyu-plugin-ai-response-transformer/src/main/java/org/apache/shenyu/plugin/ai/transformer/response/AiResponseTransformerPlugin.java
 
b/shenyu-plugin/shenyu-plugin-ai/shenyu-plugin-ai-response-transformer/src/main/java/org/apache/shenyu/plugin/ai/transformer/response/AiResponseTransformerPlugin.java
index 8718bc1b3b..c8db6f4419 100644
--- 
a/shenyu-plugin/shenyu-plugin-ai/shenyu-plugin-ai-response-transformer/src/main/java/org/apache/shenyu/plugin/ai/transformer/response/AiResponseTransformerPlugin.java
+++ 
b/shenyu-plugin/shenyu-plugin-ai/shenyu-plugin-ai-response-transformer/src/main/java/org/apache/shenyu/plugin/ai/transformer/response/AiResponseTransformerPlugin.java
@@ -72,6 +72,8 @@ public class AiResponseTransformerPlugin extends 
AbstractShenyuPlugin {
 
     private static final Logger LOG = 
LoggerFactory.getLogger(AiResponseTransformerPlugin.class);
 
+    private static final ObjectMapper MAPPER = new ObjectMapper();
+
     private final List<HttpMessageReader<?>> messageReaders;
 
     private final AiModelFactoryRegistry aiModelFactoryRegistry;
@@ -220,7 +222,7 @@ public class AiResponseTransformerPlugin extends 
AbstractShenyuPlugin {
 
             if (body.startsWith("{") && body.endsWith("}") || 
body.startsWith("[") && body.endsWith("]")) {
                 try {
-                    new ObjectMapper().readTree(body);
+                    MAPPER.readTree(body);
                     return body;
                 } catch (Exception e) {
                     LOG.warn("Body is not valid JSON: {}", body);
@@ -333,15 +335,14 @@ public class AiResponseTransformerPlugin extends 
AbstractShenyuPlugin {
 
                             String messageWithResponseBody;
                             try {
-                                ObjectMapper objectMapper = new ObjectMapper();
-                                JsonNode messageNode = 
objectMapper.readTree(message);
+                                JsonNode messageNode = 
MAPPER.readTree(message);
 
                                 if (messageNode.has("response") && 
messageNode.get("response").isObject()) {
                                     ObjectNode responseNode = (ObjectNode) 
messageNode.get("response");
                                     responseNode.put("body", 
finalResponseBody);
                                 }
                                 
-                                messageWithResponseBody = 
objectMapper.writeValueAsString(messageNode);
+                                messageWithResponseBody = 
MAPPER.writeValueAsString(messageNode);
                             } catch (Exception e) {
                                 LOG.error("Failed to update message with 
response body", e);
                                 messageWithResponseBody = 
message.replace("\"body\":\"\"", "\"body\":\"" + 
finalResponseBody.replace("\"", "\\\"") + "\"");
diff --git 
a/shenyu-plugin/shenyu-plugin-ai/shenyu-plugin-ai-response-transformer/src/test/java/org/apache/shenyu/plugin/ai/transformer/response/AiResponseTransformerPluginTest.java
 
b/shenyu-plugin/shenyu-plugin-ai/shenyu-plugin-ai-response-transformer/src/test/java/org/apache/shenyu/plugin/ai/transformer/response/AiResponseTransformerPluginTest.java
index 875ecc3ba9..df27629feb 100644
--- 
a/shenyu-plugin/shenyu-plugin-ai/shenyu-plugin-ai-response-transformer/src/test/java/org/apache/shenyu/plugin/ai/transformer/response/AiResponseTransformerPluginTest.java
+++ 
b/shenyu-plugin/shenyu-plugin-ai/shenyu-plugin-ai-response-transformer/src/test/java/org/apache/shenyu/plugin/ai/transformer/response/AiResponseTransformerPluginTest.java
@@ -57,6 +57,8 @@ import java.nio.charset.StandardCharsets;
 import java.util.Collections;
 import java.util.zip.GZIPInputStream;
 
+import com.fasterxml.jackson.databind.ObjectMapper;
+
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertNull;
@@ -322,4 +324,17 @@ class AiResponseTransformerPluginTest {
         String body = 
AiResponseTransformerPlugin.extractBodyFromAiResponse(aiResponse);
         assertEquals("[{\"id\":1,\"name\":\"test\"}]", body);
     }
+
+    @Test
+    void testExtractBodyFromAiResponseReusesObjectMapper() {
+        // initialize the plugin class (and its static mapper) before the 
constructor instrumentation is installed
+        
AiResponseTransformerPlugin.extractBodyFromAiResponse("{\"status\":\"success\"}");
+
+        try (MockedConstruction<ObjectMapper> mockedMappers = 
mockConstruction(ObjectMapper.class)) {
+            String body = AiResponseTransformerPlugin
+                    .extractBodyFromAiResponse("HTTP/1.1 200 OK\nContent-Type: 
application/json\n\n{\"status\":\"success\"}");
+            assertEquals("{\"status\":\"success\"}", body);
+            assertEquals(0, mockedMappers.constructed().size());
+        }
+    }
 }

Reply via email to