Skip to content

Commit

Permalink
fix remove the field writer which only differ in first character that…
Browse files Browse the repository at this point in the history
… one is upper case the other is lower case (alibaba#3222)

* fix remove the field writer which only differ in first character that one is upper case the other is lower case, for issue 3220

* fix build error

* add test
  • Loading branch information
yanxutao89 authored Dec 29, 2024
1 parent 3ce6eea commit 9d3e91c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,20 @@ boolean record = BeanUtils.isRecord(objectClass);
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,20 @@ boolean record = BeanUtils.isRecord(objectClass);
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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit 9d3e91c

Please sign in to comment.