Skip to content

Commit

Permalink
bug fix for firstChar upper case getter method, field annotation not …
Browse files Browse the repository at this point in the history
…work, for issue #638
  • Loading branch information
wenshao committed Aug 6, 2022
1 parent 7ff203d commit 3a24b40
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,6 @@ public void getBeanInfo(BeanInfo beanInfo, Class objectClass) {

if (jsonType == null) {
Class mixInSource = provider.mixInCache.get(objectClass);
// if (mixInSource == null) {
// String typeName = objectClass.getName();
// switch (typeName) {
// case "org.apache.commons.lang3.tuple.ImmutablePair":
// provider.mixIn(objectClass, mixInSource = ApacheLang3Support.PairMixIn.class);
// break;
// case "org.apache.commons.lang3.tuple.MutablePair":
// provider.mixIn(objectClass, mixInSource = ApacheLang3Support.MutablePairMixIn.class);
// break;
// default:
// break;
// }
// }

if (mixInSource != null) {
beanInfo.mixIn = true;
Expand Down Expand Up @@ -201,16 +188,6 @@ public void getBeanInfo(BeanInfo beanInfo, Class objectClass) {
@Override
public void getFieldInfo(BeanInfo beanInfo, FieldInfo fieldInfo, Class objectType, Field field) {
Class mixInSource = provider.mixInCache.get(objectType);
// if (objectType != null) {
// String typeName = objectType.getName();
// switch (typeName) {
// case "org.apache.commons.lang3.tuple.ImmutablePair":
// provider.mixIn(objectType, mixInSource = ApacheLang3Support.PairMixIn.class);
// break;
// default:
// break;
// }
// }

if (mixInSource != null && mixInSource != objectType) {
Field mixInField = null;
Expand Down Expand Up @@ -575,8 +552,18 @@ public void getFieldInfo(BeanInfo beanInfo, FieldInfo fieldInfo, Class objectCla
if (!objectClass.getName().startsWith("java.lang") && !BeanUtils.isRecord(objectClass)) {
String fieldName = BeanUtils.getterName(methodName, null);

char firstChar = fieldName.charAt(0);
final String fieldName0;
if (firstChar >= 'A' && firstChar <= 'Z') {
char[] chars = fieldName.toCharArray();
chars[0] = (char) (firstChar + 32);
fieldName0 = new String(chars);
} else {
fieldName0 = null;
}
BeanUtils.declaredFields(objectClass, field -> {
if (field.getName().equals(fieldName)) {
String name = field.getName();
if (name.equals(fieldName) || (fieldName0 != null && name.equals(fieldName0))) {
int modifiers = field.getModifiers();
if ((!Modifier.isPublic(modifiers)) && !Modifier.isStatic(modifiers)) {
getFieldInfo(beanInfo, fieldInfo, objectClass, field);
Expand Down
31 changes: 31 additions & 0 deletions core/src/test/java/com/alibaba/fastjson2/issues/Issue638.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.alibaba.fastjson2.issues;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.annotation.JSONField;
import lombok.Getter;
import lombok.Setter;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class Issue638 {
@Test
public void test() {
Clothing clothing = new Clothing();
if (clothing.getTShirtType() == null) {
clothing.setTShirtType(new ArrayList<>());
}
clothing.getTShirtType().add("Sweater Vest");
assertEquals("{\"tShirtType\":[\"Sweater Vest\"]}", JSON.toJSONString(clothing));
}

@Getter
@Setter
public class Clothing {
@JSONField(name = "tShirtType")
private List<String> tShirtType;
}
}

0 comments on commit 3a24b40

Please sign in to comment.