Skip to content

Commit

Permalink
method-name startsWith 'set' have higher priority than method-name w…
Browse files Browse the repository at this point in the history
…ith non-startsWith 'set',for issue alibaba#2230
  • Loading branch information
rowstop committed Feb 5, 2024
1 parent 3ec7884 commit b8f1415
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 33 deletions.
45 changes: 23 additions & 22 deletions core/src/main/java/com/alibaba/fastjson2/reader/FieldReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,16 +253,17 @@ public int compareTo(FieldReader o) {
if (this.method != null && o.method != null) {
Class<?> thisDeclaringClass = this.method.getDeclaringClass();
Class<?> otherDeclaringClass = o.method.getDeclaringClass();

for (Class s = thisDeclaringClass.getSuperclass(); s != null && s != Object.class; s = s.getSuperclass()) {
if (s == otherDeclaringClass) {
return -1;
//declaring class compare
if (thisDeclaringClass != otherDeclaringClass) {
for (Class s = thisDeclaringClass.getSuperclass(); s != null && s != Object.class; s = s.getSuperclass()) {
if (s == otherDeclaringClass) {
return -1;
}
}
}

for (Class s = otherDeclaringClass.getSuperclass(); s != null && s != Object.class; s = s.getSuperclass()) {
if (s == thisDeclaringClass) {
return 1;
for (Class s = otherDeclaringClass.getSuperclass(); s != null && s != Object.class; s = s.getSuperclass()) {
if (s == thisDeclaringClass) {
return 1;
}
}
}

Expand All @@ -286,30 +287,30 @@ public int compareTo(FieldReader o) {
if (otherParamType.isEnum() && (thisParamType == Integer.class || thisParamType == int.class)) {
return -1;
}

//JSONField annotation priority over non JSONField annotation
JSONField thisAnnotation = BeanUtils.findAnnotation(this.method, JSONField.class);
JSONField otherAnnotation = BeanUtils.findAnnotation(o.method, JSONField.class);

if (thisAnnotation != null && otherAnnotation == null) {
return -1;
}

if (thisAnnotation == null && otherAnnotation != null) {
return 1;
boolean thisAnnotatedWithJsonFiled = thisAnnotation != null;
if (thisAnnotatedWithJsonFiled == (otherAnnotation == null)) {
return thisAnnotatedWithJsonFiled ? -1 : 1;
}
}
}

String thisMethodName = this.method.getName();
String otherMethodName = o.method.getName();
if (!thisMethodName.equals(otherMethodName)) {
//setter priority over non setter
boolean thisMethodNameSetStart = thisMethodName.startsWith("set");
if (thisMethodNameSetStart != otherMethodName.startsWith("set")) {
return thisMethodNameSetStart ? -1 : 1;
}
//different field name priority over same field name
String thisName = BeanUtils.setterName(thisMethodName, null);
String otherName = BeanUtils.setterName(otherMethodName, null);
if (this.fieldName.equals(thisName) && !o.fieldName.equals(otherName)) {
return 1;
}
if (o.fieldName.equals(otherName) && !this.fieldName.equals(thisName)) {
return -1;
boolean thisFieldNameEquals = this.fieldName.equals(thisName);
if (thisFieldNameEquals != o.fieldName.equals(otherName)) {
return thisFieldNameEquals ? 1 : -1;
}
}
}
Expand Down
25 changes: 14 additions & 11 deletions core/src/test/java/com/alibaba/fastjson2/issues_2200/Issue2230.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import lombok.Data;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.Serializable;
Expand All @@ -15,37 +16,39 @@
*/
public class Issue2230 {
@Test
void test(){
void test() {
JSONObject dfDtoJson = new JSONObject();
JSONObject value = new JSONObject()
.fluentPut("compTypeName", "test11")
.fluentPut("name", "test22");
dfDtoJson.put(
"componentList",
new JSONArray().fluentAdd(
new JSONObject().fluentPut(
"props",
new JSONObject()
.fluentPut("compTypeName", "test11")
.fluentPut("name", "test22")
)
new JSONObject()
.fluentPut("props", value)
)
);
DynFormDto newDfDto = dfDtoJson.toJavaObject(DynFormDto.class);
System.out.println(newDfDto.getComponentList().get(0).getProps());
JSONObject props = newDfDto.getComponentList().get(0).getProps();
Assertions.assertEquals(value, props);
}
@Data
static class DynFormDto {
private List<DynFormComponentDto> componentList;
private String notes;
}
@Data
static class DynFormComponentDto implements Serializable {
static class DynFormComponentDto
implements Serializable {
private JSONObject props; // 组件完整配置
// 这个方法会导致props字段反序列化结果出现{"h":{***}}结构
public DynFormComponentDto props(IFormComponent comp) {
this.props = (JSONObject) JSONObject.from(comp, JSONWriter.Feature.FieldBased);
this.props = JSONObject.from(comp, JSONWriter.Feature.FieldBased);
return this;
}
}
interface IFormComponent {

interface IFormComponent {
String getCompTypeName();
String getName();
}
Expand Down

0 comments on commit b8f1415

Please sign in to comment.