Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix remove the field writer which only differ in first character that one is upper case the other is lower case #3222

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading