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

method-name startsWith 'set' have higher priority than method-name with non-startsWith 'set',for issue #2230 #2237

Merged
merged 2 commits into from
Feb 5, 2024
Merged
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
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.alibaba.fastjson2.issues_2200;

import com.alibaba.fastjson2.JSONArray;
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;
import java.util.List;

/**
* @author 张治保
* @since 2024/2/5
*/
public class Issue2230 {
@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", value)
)
);
DynFormDto newDfDto = dfDtoJson.toJavaObject(DynFormDto.class);
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 {
private JSONObject props; // 组件完整配置
// 这个方法会导致props字段反序列化结果出现{"h":{***}}结构
public DynFormComponentDto props(IFormComponent comp) {
this.props = JSONObject.from(comp, JSONWriter.Feature.FieldBased);
return this;
}
}

interface IFormComponent {
String getCompTypeName();
String getName();
}
}
Loading