Copilot commented on code in PR #8046:
URL: https://github.com/apache/incubator-seata/pull/8046#discussion_r3062630124


##########
json-common/json-common-core/src/main/java/org/apache/seata/common/json/impl/Fastjson2JsonSerializer.java:
##########
@@ -0,0 +1,174 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.seata.common.json.impl;
+
+import com.alibaba.fastjson2.JSON;
+import com.alibaba.fastjson2.JSONReader;
+import com.alibaba.fastjson2.JSONWriter;
+import com.alibaba.fastjson2.filter.ContextAutoTypeBeforeHandler;
+import com.alibaba.fastjson2.util.TypeUtils;
+import org.apache.seata.common.exception.JsonParseException;
+import org.apache.seata.common.json.JsonAllowlistManager;
+import org.apache.seata.common.json.JsonSerializer;
+import org.apache.seata.common.loader.LoadLevel;
+
+import java.lang.reflect.Type;
+
+/**
+ * Fastjson2 implementation of JsonSerializer
+ */
+@LoadLevel(name = Fastjson2JsonSerializer.NAME)
+public class Fastjson2JsonSerializer implements JsonSerializer {
+
+    public static final String NAME = "fastjson2";
+
+    private static final JSONWriter.Feature[] SERIALIZER_FEATURES =
+            new JSONWriter.Feature[] {JSONWriter.Feature.WriteClassName};
+
+    private static final JSONWriter.Feature[] SERIALIZER_FEATURES_PRETTY =
+            new JSONWriter.Feature[] {JSONWriter.Feature.WriteClassName, 
JSONWriter.Feature.PrettyFormat};
+
+    private static final JSONWriter.Feature[] FEATURES_PRETTY =
+            new JSONWriter.Feature[] {JSONWriter.Feature.PrettyFormat};
+
+    private static final AllowlistAutoTypeHandler ALLOWLIST_HANDLER =
+            new AllowlistAutoTypeHandler("org.apache.seata.", "io.seata.");
+
+    @Override
+    public String toJSONString(Object object) {
+        try {
+            return JSON.toJSONString(object);
+        } catch (Exception e) {
+            throw new JsonParseException("Fastjson2 serialize error", e);
+        }
+    }
+
+    @Override
+    public <T> T parseObject(String text, Class<T> clazz) {
+        if (text == null || clazz == null) {
+            return null;
+        }
+        try {
+            return JSON.parseObject(text, clazz);
+        } catch (Exception e) {
+            throw new JsonParseException("Fastjson2 deserialize error", e);
+        }
+    }
+
+    @Override
+    public <T> T parseObjectWithType(String text, Type type) {
+        if (text == null || type == null) {
+            return null;
+        }
+        try {
+            return JSON.parseObject(text, type, ALLOWLIST_HANDLER, 
JSONReader.Feature.SupportAutoType);
+        } catch (SecurityException e) {
+            throw e;
+        } catch (Exception e) {
+            rethrowIfSecurityException(e);
+            throw new JsonParseException("Fastjson2 deserialize error", e);
+        }
+    }
+
+    @Override
+    public boolean useAutoType(String json) {
+        return json != null && json.contains("\"@type\"");
+    }
+
+    @Override
+    public String toJSONString(Object object, boolean prettyPrint) {
+        return toJSONString(object, false, prettyPrint);
+    }

Review Comment:
   Similar to the Jackson3 implementation, `toJSONString(Object)` here omits 
`WriteClassName`, but `toJSONString(object, false, prettyPrint)` includes it 
(because it uses `SERIALIZER_FEATURES*`). These overloads produce materially 
different JSON even when `prettyPrint=false`, which is surprising for callers. 
Align the default `toJSONString(Object)` behavior with the other overload 
defaults (either always include auto-type by default, or never include it by 
default) to keep the API consistent.



##########
json-common/json-common-core/src/main/java/org/apache/seata/common/json/impl/Fastjson2JsonSerializer.java:
##########
@@ -0,0 +1,174 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.seata.common.json.impl;
+
+import com.alibaba.fastjson2.JSON;
+import com.alibaba.fastjson2.JSONReader;
+import com.alibaba.fastjson2.JSONWriter;
+import com.alibaba.fastjson2.filter.ContextAutoTypeBeforeHandler;
+import com.alibaba.fastjson2.util.TypeUtils;
+import org.apache.seata.common.exception.JsonParseException;
+import org.apache.seata.common.json.JsonAllowlistManager;
+import org.apache.seata.common.json.JsonSerializer;
+import org.apache.seata.common.loader.LoadLevel;
+
+import java.lang.reflect.Type;
+
+/**
+ * Fastjson2 implementation of JsonSerializer
+ */
+@LoadLevel(name = Fastjson2JsonSerializer.NAME)
+public class Fastjson2JsonSerializer implements JsonSerializer {
+
+    public static final String NAME = "fastjson2";
+
+    private static final JSONWriter.Feature[] SERIALIZER_FEATURES =
+            new JSONWriter.Feature[] {JSONWriter.Feature.WriteClassName};
+
+    private static final JSONWriter.Feature[] SERIALIZER_FEATURES_PRETTY =
+            new JSONWriter.Feature[] {JSONWriter.Feature.WriteClassName, 
JSONWriter.Feature.PrettyFormat};
+
+    private static final JSONWriter.Feature[] FEATURES_PRETTY =
+            new JSONWriter.Feature[] {JSONWriter.Feature.PrettyFormat};
+
+    private static final AllowlistAutoTypeHandler ALLOWLIST_HANDLER =
+            new AllowlistAutoTypeHandler("org.apache.seata.", "io.seata.");
+
+    @Override
+    public String toJSONString(Object object) {
+        try {
+            return JSON.toJSONString(object);
+        } catch (Exception e) {
+            throw new JsonParseException("Fastjson2 serialize error", e);
+        }
+    }
+
+    @Override
+    public <T> T parseObject(String text, Class<T> clazz) {
+        if (text == null || clazz == null) {
+            return null;
+        }
+        try {
+            return JSON.parseObject(text, clazz);
+        } catch (Exception e) {
+            throw new JsonParseException("Fastjson2 deserialize error", e);
+        }
+    }
+
+    @Override
+    public <T> T parseObjectWithType(String text, Type type) {
+        if (text == null || type == null) {
+            return null;
+        }
+        try {
+            return JSON.parseObject(text, type, ALLOWLIST_HANDLER, 
JSONReader.Feature.SupportAutoType);
+        } catch (SecurityException e) {
+            throw e;
+        } catch (Exception e) {
+            rethrowIfSecurityException(e);
+            throw new JsonParseException("Fastjson2 deserialize error", e);
+        }
+    }
+
+    @Override
+    public boolean useAutoType(String json) {
+        return json != null && json.contains("\"@type\"");
+    }

Review Comment:
   Same issue as in the Jackson3 serializer: this implementation can report 
auto-type usage when `\"@type\"` appears in a string value rather than as a 
JSON property name. Consider checking for an actual key occurrence (e.g., 
`\"@type\"\\s*:`) or parsing the JSON to confirm `@type` is present as a field.



##########
json-common/json-common-core/src/main/java/org/apache/seata/common/json/impl/Fastjson2JsonSerializer.java:
##########
@@ -0,0 +1,174 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.seata.common.json.impl;
+
+import com.alibaba.fastjson2.JSON;
+import com.alibaba.fastjson2.JSONReader;
+import com.alibaba.fastjson2.JSONWriter;
+import com.alibaba.fastjson2.filter.ContextAutoTypeBeforeHandler;
+import com.alibaba.fastjson2.util.TypeUtils;
+import org.apache.seata.common.exception.JsonParseException;
+import org.apache.seata.common.json.JsonAllowlistManager;
+import org.apache.seata.common.json.JsonSerializer;
+import org.apache.seata.common.loader.LoadLevel;
+
+import java.lang.reflect.Type;
+
+/**
+ * Fastjson2 implementation of JsonSerializer
+ */
+@LoadLevel(name = Fastjson2JsonSerializer.NAME)
+public class Fastjson2JsonSerializer implements JsonSerializer {
+
+    public static final String NAME = "fastjson2";
+
+    private static final JSONWriter.Feature[] SERIALIZER_FEATURES =
+            new JSONWriter.Feature[] {JSONWriter.Feature.WriteClassName};
+
+    private static final JSONWriter.Feature[] SERIALIZER_FEATURES_PRETTY =
+            new JSONWriter.Feature[] {JSONWriter.Feature.WriteClassName, 
JSONWriter.Feature.PrettyFormat};
+
+    private static final JSONWriter.Feature[] FEATURES_PRETTY =
+            new JSONWriter.Feature[] {JSONWriter.Feature.PrettyFormat};
+
+    private static final AllowlistAutoTypeHandler ALLOWLIST_HANDLER =
+            new AllowlistAutoTypeHandler("org.apache.seata.", "io.seata.");
+
+    @Override
+    public String toJSONString(Object object) {
+        try {
+            return JSON.toJSONString(object);
+        } catch (Exception e) {
+            throw new JsonParseException("Fastjson2 serialize error", e);
+        }
+    }

Review Comment:
   Similar to the Jackson3 implementation, `toJSONString(Object)` here omits 
`WriteClassName`, but `toJSONString(object, false, prettyPrint)` includes it 
(because it uses `SERIALIZER_FEATURES*`). These overloads produce materially 
different JSON even when `prettyPrint=false`, which is surprising for callers. 
Align the default `toJSONString(Object)` behavior with the other overload 
defaults (either always include auto-type by default, or never include it by 
default) to keep the API consistent.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to