mirror of https://github.com/alibaba/fastjson2.git
fix skip value setting when field is final and not empty collection initialized, for issue #2944
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os }} (11, macos-latest) (push) Has been cancelled
Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os }} (11, ubuntu-24.04) (push) Has been cancelled
Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os }} (11, windows-latest) (push) Has been cancelled
Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os }} (17, macos-latest) (push) Has been cancelled
Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os }} (17, ubuntu-24.04) (push) Has been cancelled
Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os }} (17, windows-latest) (push) Has been cancelled
Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os }} (21, macos-latest) (push) Has been cancelled
Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os }} (21, ubuntu-24.04) (push) Has been cancelled
Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os }} (21, windows-latest) (push) Has been cancelled
Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os }} (8, macos-latest) (push) Has been cancelled
Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os }} (8, ubuntu-24.04) (push) Has been cancelled
Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os }} (8, windows-latest) (push) Has been cancelled
Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os }} (11, macos-latest) (push) Has been cancelled
Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os }} (11, ubuntu-24.04) (push) Has been cancelled
Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os }} (11, windows-latest) (push) Has been cancelled
Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os }} (17, macos-latest) (push) Has been cancelled
Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os }} (17, ubuntu-24.04) (push) Has been cancelled
Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os }} (17, windows-latest) (push) Has been cancelled
Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os }} (21, macos-latest) (push) Has been cancelled
Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os }} (21, ubuntu-24.04) (push) Has been cancelled
Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os }} (21, windows-latest) (push) Has been cancelled
Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os }} (8, macos-latest) (push) Has been cancelled
Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os }} (8, ubuntu-24.04) (push) Has been cancelled
Details
Java CI / Test on JDK ${{ matrix.java }} OS ${{ matrix.os }} (8, windows-latest) (push) Has been cancelled
Details
This commit is contained in:
parent
e25d5238ea
commit
61dc7e16e1
|
@ -7,6 +7,7 @@ import com.alibaba.fastjson2.util.Fnv;
|
|||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
|
@ -697,6 +698,17 @@ public class ObjectReaderAdapter<T>
|
|||
continue;
|
||||
}
|
||||
|
||||
if (fieldReader.field != null && Modifier.isFinal(fieldReader.field.getModifiers())) {
|
||||
try {
|
||||
Object value = fieldReader.method.invoke(object);
|
||||
if (value instanceof Collection && !((Collection) value).isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// just ignore
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
if (fieldValue.getClass() == fieldReader.fieldType) {
|
||||
fieldReader.accept(object, fieldValue);
|
||||
|
|
|
@ -6,6 +6,7 @@ import com.alibaba.fastjson2.util.TypeUtils;
|
|||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.*;
|
||||
import java.util.function.BiFunction;
|
||||
|
@ -563,6 +564,17 @@ public class ObjectReaderNoneDefaultConstructor<T>
|
|||
continue;
|
||||
}
|
||||
|
||||
if (fieldReader.field != null && Modifier.isFinal(fieldReader.field.getModifiers())) {
|
||||
try {
|
||||
Object value = fieldReader.method.invoke(object);
|
||||
if (value instanceof Collection && !((Collection) value).isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// just ignore
|
||||
}
|
||||
}
|
||||
|
||||
Class<?> valueClass = fieldValue.getClass();
|
||||
Class fieldClass = fieldReader.fieldClass;
|
||||
Type fieldType = fieldReader.fieldType;
|
||||
|
|
|
@ -0,0 +1,65 @@
|
|||
package com.alibaba.fastjson2.issues_2900;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import lombok.Getter;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class Issue2944 {
|
||||
@Test
|
||||
public void test() {
|
||||
List<Employee> employees = new ArrayList<>();
|
||||
employees.add(new Employee(0, "John"));
|
||||
employees.add(new Employee(1, "Jane"));
|
||||
employees.add(new Employee(2, "Bob"));
|
||||
Department department = new Department("dev", employees);
|
||||
|
||||
String payload = JSON.toJSONString(department);
|
||||
JSONObject jsonObject = JSON.parseObject(payload);
|
||||
Department parsed = jsonObject.toJavaObject(Department.class);
|
||||
assertEquals(payload, JSON.toJSONString(parsed));
|
||||
}
|
||||
|
||||
@Getter
|
||||
static class Department {
|
||||
private final String name;
|
||||
private final List<Employee> employees;
|
||||
|
||||
public Department(String name, List<Employee> employees) {
|
||||
this.name = name;
|
||||
this.employees = employees;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Department{" +
|
||||
"name='" + name + '\'' +
|
||||
", employees=" + employees +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
@Getter
|
||||
static class Employee {
|
||||
private final Integer id;
|
||||
private final String name;
|
||||
|
||||
public Employee(int id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Employee{" +
|
||||
"id=" + id +
|
||||
", name='" + name + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue