Skip to content

Commit

Permalink
fix BeanUtils#getEnumValueField, for issue #2682 (#2821)
Browse files Browse the repository at this point in the history
* fix BeanUtils#getEnumValueField, for issue #2682

* fix #2821

* fix #2821
  • Loading branch information
poo0054 authored Jul 23, 2024
1 parent 4287c3b commit bcccf7b
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
11 changes: 10 additions & 1 deletion core/src/main/java/com/alibaba/fastjson2/util/BeanUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* @author Bob Lee
* @author Jesse Wilson
* @author Shaojin Wen
* @author poo0054
*/
public abstract class BeanUtils {
static final Type[] EMPTY_TYPE_ARRAY = new Type[]{};
Expand Down Expand Up @@ -797,7 +798,15 @@ public static Member getEnumValueField(Class enumClass, ObjectCodecProvider mixi
if (field != null && isJSONField(field)) {
if (valueMember == null) {
valueMember = method;
} else if (!valueMember.getName().equals(method.getName())) {
} else if (valueMember.getName().equals(method.getName())) {
// Using Subclasses #2682
if (valueMember instanceof Method) {
Method valueMethod = (Method) valueMember;
if (valueMethod.getReturnType().isAssignableFrom(method.getReturnType())) {
valueMember = method;
}
}
} else {
// multi annotation
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@
import lombok.Data;
import org.junit.jupiter.api.Test;

import java.io.IOException;

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

/**
* @author yanxutao89
* @author poo00
*/
public class Issue2682 {
@Test
public void test() {
Expand All @@ -17,9 +23,18 @@ public void test() {
assertEquals(BizType.COMMON, vm.getBizType());
}

@Test
public void test1() throws IOException {
String str = "{'bizType':'common','baseType':'123啊'}";
VM vm = JSON.parseObject(str, VM.class);
assertEquals(RobotActEnum.CARTON_SCAN, vm.getBaseType());
}

@Data
public static class VM {
private BizType bizType;

private RobotActEnum baseType;
}

public enum BizType
Expand All @@ -43,8 +58,61 @@ public String getValue() {
public String getName() {
return name;
}
}

public enum RobotActEnum implements IEnum<String> {
/**
* 123啊
*/
CARTON_SCAN("123啊", "我是123"),
/**
* 456啊
*/
PALLET_ON_SHELF("456啊", "我是456"),
/**
* 789啊
*/
OUTBOUND_BIND_DOCK("789啊", "我是789"),
/**
*147啊
*/
GET_PICK_BILL("147啊", "我是147");

@JSONField(value = true)
private final String value;

private final String message;

RobotActEnum(String value, String message) {
this.value = value;
this.message = message;
}

@Override
public String getValue() {
return value;
}

public String getMessage() {
return message;
}

public RobotActEnum getEnum() {
return this;
}

public static String staticMessage(RobotActEnum robotActEnum) {
if (null == robotActEnum) {
return null;
}
return robotActEnum.getMessage();
}

public static void test123() {
public static String staticValue(RobotActEnum robotActEnum) {
if (null == robotActEnum) {
return null;
}
return robotActEnum.getValue();
}
}
}

0 comments on commit bcccf7b

Please sign in to comment.