Repository: alibaba/fastjson2
Pull request #3222 (2024-12-26 23:35:39)

fix remove the field writer which only differ in first character that one is upper case the other is lower case
### What this PR does / why we need it?

fix remove the field writer which only differ in first character that one is upper case the other is lower case

### Summary of your change



#### Please indicate you've done the following:

- [ ] Made sure tests are passing and test coverage is added if needed.
- [ ] Made sure commit message follow the rule of [Conventional Commits specification](https://www.conventionalcommits.org/).
- [ ] Considered the docs impact and opened a new docs issue or PR with docs changes if needed.


codecov-commenter: ## [Codecov](https://app.codecov.io/gh/alibaba/fastjson2/pull/3222?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=alibaba) Report
Attention: Patch coverage is `61.11111%` with `7 lines` in your changes missing coverage. Please review.
> Project coverage is 72.83%. Comparing base [(`f6cdd54`)](https://app.codecov.io/gh/alibaba/fastjson2/commit/f6cdd545c0847b3867afa3e76a89e681d112f72f?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=alibaba) to head [(`fdb4e06`)](https://app.codecov.io/gh/alibaba/fastjson2/commit/fdb4e06672a0ce27b5db09a9eaca9f7fb30ec745?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=alibaba).
> Report is 58 commits behind head on main.

| [Files with missing lines](https://app.codecov.io/gh/alibaba/fastjson2/pull/3222?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=alibaba) | Patch % | Lines |
|---|---|---|
| [.../alibaba/fastjson2/writer/ObjectWriterCreator.java](https://app.codecov.io/gh/alibaba/fastjson2/pull/3222?src=pr&el=tree&filepath=core%2Fsrc%2Fmain%2Fjava%2Fcom%2Falibaba%2Ffastjson2%2Fwriter%2FObjectWriterCreator.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=alibaba#diff-Y29yZS9zcmMvbWFpbi9qYXZhL2NvbS9hbGliYWJhL2Zhc3Rqc29uMi93cml0ZXIvT2JqZWN0V3JpdGVyQ3JlYXRvci5qYXZh) | 55.55% | [1 Missing and 3 partials :warning: ](https://app.codecov.io/gh/alibaba/fastjson2/pull/3222?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=alibaba) |
| [...ibaba/fastjson2/writer/ObjectWriterCreatorASM.java](https://app.codecov.io/gh/alibaba/fastjson2/pull/3222?src=pr&el=tree&filepath=core%2Fsrc%2Fmain%2Fjava%2Fcom%2Falibaba%2Ffastjson2%2Fwriter%2FObjectWriterCreatorASM.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=alibaba#diff-Y29yZS9zcmMvbWFpbi9qYXZhL2NvbS9hbGliYWJhL2Zhc3Rqc29uMi93cml0ZXIvT2JqZWN0V3JpdGVyQ3JlYXRvckFTTS5qYXZh) | 66.66% | [1 Missing and 2 partials :warning: ](https://app.codecov.io/gh/alibaba/fastjson2/pull/3222?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=alibaba) |

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##               main    #3222      +/-   ##
============================================
- Coverage     74.27%   72.83%   -1.44%     
+ Complexity    23542    22781     -761     
============================================
  Files           659      656       -3     
  Lines         91870    91574     -296     
  Branches      20539    20427     -112     
============================================
- Hits          68238    66702    -1536     
- Misses        15240    16312    +1072     
- Partials       8392     8560     +168     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/alibaba/fastjson2/pull/3222?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=alibaba).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=alibaba).

wenshao: 没有单测？

Patch:
diff --git a/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreator.java b/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreator.java
index dba7a8e0b..713f753f7 100644
--- a/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreator.java
+++ b/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreator.java
@@ -479,6 +479,20 @@ public class ObjectWriterCreator {
                     if (origin != null && origin.compareTo(fieldWriter) > 0) {
                         fieldWriterMap.put(fieldName, fieldWriter);
                     }
+
+                    // the sameFieldName means only differ in first character that one is upper case the other is lower case
+                    if (origin == null) {
+                        String sameFieldName = null;
+                        char firstChar = fieldName.charAt(0);
+                        if (firstChar >= 'A' && firstChar <= 'Z') {
+                            sameFieldName = (char) (firstChar + 32) + fieldName.substring(1);
+                        } else if (firstChar >= 'a' && firstChar <= 'z') {
+                            sameFieldName = (char) (firstChar - 32) + fieldName.substring(1);
+                        }
+                        if (sameFieldName != null && fieldWriterMap.containsKey(sameFieldName)) {
+                            fieldWriterMap.remove(sameFieldName);
+                        }
+                    }
                 });
 
                 fieldWriters = new ArrayList<>(fieldWriterMap.values());
diff --git a/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreatorASM.java b/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreatorASM.java
index 85ef8693c..92d22a4d1 100644
--- a/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreatorASM.java
+++ b/core/src/main/java/com/alibaba/fastjson2/writer/ObjectWriterCreatorASM.java
@@ -345,6 +345,20 @@ public class ObjectWriterCreatorASM
                     if (origin != null && origin.compareTo(fieldWriter) > 0) {
                         fieldWriterMap.put(fieldName, fieldWriter);
                     }
+
+                    // the sameFieldName means only differ in first character that one is upper case the other is lower case
+                    if (origin == null) {
+                        String sameFieldName = null;
+                        char firstChar = fieldName.charAt(0);
+                        if (firstChar >= 'A' && firstChar <= 'Z') {
+                            sameFieldName = (char) (firstChar + 32) + fieldName.substring(1);
+                        } else if (firstChar >= 'a' && firstChar <= 'z') {
+                            sameFieldName = (char) (firstChar - 32) + fieldName.substring(1);
+                        }
+                        if (sameFieldName != null && fieldWriterMap.containsKey(sameFieldName)) {
+                            fieldWriterMap.remove(sameFieldName);
+                        }
+                    }
                 });
             }
         } else {
diff --git a/pom.xml b/pom.xml
index 01a9b2030..d65b6c103 100644
--- a/pom.xml
+++ b/pom.xml
@@ -663,7 +663,8 @@
                                 com/alibaba/fastjson2/issues_1000/Issue1395.java,
                                 com/alibaba/fastjson2/issues_2100/Issue2164.java,
                                 com/alibaba/fastjson2/issues_2100/Issue2181.java,
-                                com/alibaba/fastjson/v2issues/Issue1432.java
+                                com/alibaba/fastjson/v2issues/Issue1432.java,
+                                com/alibaba/fastjson2/issues_3200/Issue3220.java
                             </excludes>
                         </configuration>
                     </execution>


Test patch:
diff --git a/core/src/test/java/com/alibaba/fastjson2/issues_3200/Issue3220.java b/core/src/test/java/com/alibaba/fastjson2/issues_3200/Issue3220.java
new file mode 100644
index 000000000..c88b9b2e4
--- /dev/null
+++ b/core/src/test/java/com/alibaba/fastjson2/issues_3200/Issue3220.java
@@ -0,0 +1,27 @@
+package com.alibaba.fastjson2.issues_3200;
+
+import com.alibaba.fastjson2.JSON;
+import lombok.Data;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class Issue3220 {
+    @Test
+    public void test() {
+        Param param = new Param();
+        param.setUserName("张三");
+        param.setCode("001");
+        param.setSort("1");
+        param.setStatus("正常");
+        assertEquals("{\"code\":\"001\",\"sort\":\"1\",\"status\":\"正常\",\"userName\":\"张三\"}", JSON.toJSONString(param));
+    }
+
+    @Data
+    private static class Param {
+        public String UserName;
+        String Code;
+        String Sort;
+        String Status;
+    }
+}