Skip to content

Commit

Permalink
fix prefer to most specified setter method, for issue #2229
Browse files Browse the repository at this point in the history
  • Loading branch information
yanxutao89 committed Jun 7, 2024
1 parent 7118a55 commit 47088a6
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 13 deletions.
41 changes: 30 additions & 11 deletions core/src/main/java/com/alibaba/fastjson2/reader/FieldReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@
import com.alibaba.fastjson2.util.TypeUtils;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.lang.reflect.*;
import java.time.*;
import java.util.Calendar;
import java.util.Date;
Expand Down Expand Up @@ -276,12 +272,28 @@ public int compareTo(FieldReader o) {
return -1;
}

if (thisParamType.isEnum() && (otherParamType == Integer.class || otherParamType == int.class)) {
return 1;
}

if (otherParamType.isEnum() && (thisParamType == Integer.class || thisParamType == int.class)) {
return -1;
if (needCompareToActualFieldClass(thisParamType)|| needCompareToActualFieldClass(otherParamType)) {
Class actualFieldClass = null;
try {
actualFieldClass = thisDeclaringClass.getDeclaredField(this.fieldName).getType();
if (actualFieldClass == null) {
actualFieldClass = otherDeclaringClass.getDeclaredField(this.fieldName).getType();
}
} catch (NoSuchFieldException ignored) {
// ignored
}
if (actualFieldClass != null) {
for (Class s = thisParamType; s != null && s != Object.class; s = s.getSuperclass()) {
if (s == actualFieldClass) {
return -1;
}
}
for (Class s = otherParamType; s != null && s != Object.class; s = s.getSuperclass()) {
if (s == actualFieldClass) {
return 1;
}
}
}
}
//JSONField annotation priority over non JSONField annotation
JSONField thisAnnotation = BeanUtils.findAnnotation(this.method, JSONField.class);
Expand Down Expand Up @@ -603,4 +615,11 @@ private String getActualFieldName(FieldReader fieldReader) {
String name = fieldReader.method.getName();
return fieldReader.isReadOnly() ? BeanUtils.getterName(name, PropertyNamingStrategy.CamelCase.name()) : BeanUtils.setterName(name, PropertyNamingStrategy.CamelCase.name());
}

private boolean needCompareToActualFieldClass(Class clazz) {
if (clazz.isEnum() || clazz.isInterface()) {
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3311,8 +3311,14 @@ private void putIfAbsent(Map<String, List<FieldReader>> fieldReaders, String fie
return;
}
if (!fieldReader.isReadOnly()) {
FieldReader finalReader = fieldReader;
FieldReader sameReader = origin.stream().filter(o -> o.sameTo(finalReader)).findAny().orElse(null);
FieldReader sameReader = null;
for (int i = 0; i < origin.size(); i++) {
FieldReader tempReader = origin.get(i);
if (tempReader.sameTo(fieldReader)) {
sameReader = tempReader;
break;
}
}
if (sameReader != null) {
if (sameReader.compareTo(fieldReader) > 0 || !sameReader.belongTo(objectClass)) {
origin.set(origin.indexOf(sameReader), fieldReader);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.alibaba.fastjson2.issues_2200;

import com.alibaba.fastjson2.JSON;
import org.junit.jupiter.api.Test;

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

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

public class Issue2229 {
@Test
void test() {
String json = "{\"type\": \"A\", \"types\": [\"B\"]}";
DTO dto = JSON.parseObject(json, DTO.class);
assertEquals(Type.A, dto.getType());
assertEquals(Type.B, dto.getTypes().get(0));
}

static class DTO {
private Type type;
private List<Type> types;

public Type getType() {
return type;
}

public void setType(Type type) {
this.type = type;
}

public void setType(int type) {
this.type = null;
}

public List<Type> getTypes() {
return types;
}

public void setTypes(List<Type> types) {
this.types = types;
}

public void setTypes(int types) {
this.types = new ArrayList<>();
}
}

enum Type {
A,B;
}
}

0 comments on commit 47088a6

Please # to comment.