From 972833642a3ceeacdb973574d20fdeb9973fcc66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E9=93=81?= Date: Fri, 9 Aug 2024 01:31:41 +0800 Subject: [PATCH] fix jackson annotation JsonUnwrapped compatible, for issue #2846 --- .../com/alibaba/fastjson2/util/BeanUtils.java | 1 + .../fastjson2/issues_2800/Issue2846.java | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 core/src/test/java/com/alibaba/fastjson2/issues_2800/Issue2846.java diff --git a/core/src/main/java/com/alibaba/fastjson2/util/BeanUtils.java b/core/src/main/java/com/alibaba/fastjson2/util/BeanUtils.java index 71570be51a..af7eb4fbdd 100644 --- a/core/src/main/java/com/alibaba/fastjson2/util/BeanUtils.java +++ b/core/src/main/java/com/alibaba/fastjson2/util/BeanUtils.java @@ -1125,6 +1125,7 @@ private static boolean isJSONField(AnnotatedElement element) { case "com.fasterxml.jackson.annotation.JsonValue": case "com.fasterxml.jackson.annotation.JsonRawValue": case "com.fasterxml.jackson.annotation.JsonProperty": + case "com.fasterxml.jackson.annotation.JsonUnwrapped": if (JSONFactory.isUseJacksonAnnotation()) { return true; } diff --git a/core/src/test/java/com/alibaba/fastjson2/issues_2800/Issue2846.java b/core/src/test/java/com/alibaba/fastjson2/issues_2800/Issue2846.java new file mode 100644 index 0000000000..ea58560bb5 --- /dev/null +++ b/core/src/test/java/com/alibaba/fastjson2/issues_2800/Issue2846.java @@ -0,0 +1,31 @@ +package com.alibaba.fastjson2.issues_2800; + +import com.alibaba.fastjson2.JSON; +import com.fasterxml.jackson.annotation.JsonUnwrapped; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class Issue2846 { + @Test + public void testFastJson() throws Exception { + CombineView combineView = new CombineView(); + String jacksonResult = new ObjectMapper().writeValueAsString(combineView); + String fastjsonResult = JSON.toJSONString(new CombineView()); + assertEquals(jacksonResult, fastjsonResult); + } + + static class CombineView { + @JsonUnwrapped + public View1 author() { + return new View1(); + } + } + + static class View1 { + public String getAuthorName() { + return "name"; + } + } +}