From 0c9dcc2056b332e73584d2f7cddf3b412d0699c7 Mon Sep 17 00:00:00 2001 From: Shawn Yang Date: Sun, 30 Jun 2024 23:02:52 +0800 Subject: [PATCH] fix(java): fix fastjson object serialization (#1717) ## What does this PR do? Closes #1716 ## Related issues ## Does this PR introduce any user-facing change? - [ ] Does this PR introduce any public API change? - [ ] Does this PR introduce any binary protocol compatibility change? ## Benchmark --- .../apache/fury/resolver/ClassResolver.java | 3 + java/fury-testsuite/pom.xml | 6 ++ .../org/apache/fury/test/FastJsonTest.java | 83 +++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 java/fury-testsuite/src/test/java/org/apache/fury/test/FastJsonTest.java diff --git a/java/fury-core/src/main/java/org/apache/fury/resolver/ClassResolver.java b/java/fury-core/src/main/java/org/apache/fury/resolver/ClassResolver.java index 58ea84aadb..e8147a498f 100644 --- a/java/fury-core/src/main/java/org/apache/fury/resolver/ClassResolver.java +++ b/java/fury-core/src/main/java/org/apache/fury/resolver/ClassResolver.java @@ -839,6 +839,9 @@ public Class getSerializerClass(Class cls, boolean code } else if (ByteBuffer.class.isAssignableFrom(cls)) { return BufferSerializers.ByteBufferSerializer.class; } + if (shimDispatcher.contains(cls)) { + return shimDispatcher.getSerializer(cls).getClass(); + } if (fury.getConfig().checkJdkClassSerializable()) { if (cls.getName().startsWith("java") && !(Serializable.class.isAssignableFrom(cls))) { throw new UnsupportedOperationException( diff --git a/java/fury-testsuite/pom.xml b/java/fury-testsuite/pom.xml index 0601003977..62fea8d62d 100644 --- a/java/fury-testsuite/pom.xml +++ b/java/fury-testsuite/pom.xml @@ -105,6 +105,12 @@ 3.2.1 test + + com.alibaba + fastjson + 1.2.83 + test + diff --git a/java/fury-testsuite/src/test/java/org/apache/fury/test/FastJsonTest.java b/java/fury-testsuite/src/test/java/org/apache/fury/test/FastJsonTest.java new file mode 100644 index 0000000000..ae5397645e --- /dev/null +++ b/java/fury-testsuite/src/test/java/org/apache/fury/test/FastJsonTest.java @@ -0,0 +1,83 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.fury.test; + +import com.alibaba.fastjson.JSONObject; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Sets; +import java.util.List; +import org.apache.fury.Fury; +import org.apache.fury.collection.Collections; +import org.apache.fury.config.CompatibleMode; +import org.apache.fury.config.Language; +import org.testng.Assert; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +public class FastJsonTest { + public static class DemoResponse { + private JSONObject json; + private List objects; + + public DemoResponse(JSONObject json) { + this.json = json; + objects = Collections.ofArrayList(json); + } + } + + @DataProvider + public static Object[][] config() { + return Sets.cartesianProduct( + ImmutableSet.of(true, false), // referenceTracking + ImmutableSet.of(true, false), // compatible mode + ImmutableSet.of(true, false), // scoped meta share mode + ImmutableSet.of(true, false) // fury enable codegen + ) + .stream() + .map(List::toArray) + .toArray(Object[][]::new); + } + + @Test(dataProvider = "config") + public void testSerializeJson( + boolean trackingRef, boolean compatible, boolean scoped, boolean codegen) { + // For issue: https://github.com/apache/fury/issues/1604 + JSONObject jsonObject = new JSONObject(); + jsonObject.put("k1", "v1"); + jsonObject.put("k2", "v2"); + DemoResponse resp = new DemoResponse(jsonObject); + Fury fury = + Fury.builder() + .withLanguage(Language.JAVA) + .requireClassRegistration(false) + .withRefTracking(trackingRef) + .withCompatibleMode( + compatible ? CompatibleMode.COMPATIBLE : CompatibleMode.SCHEMA_CONSISTENT) + .withScopedMetaShare(scoped) + .withCodegen(codegen) + .registerGuavaTypes(false) + .withCompatibleMode(CompatibleMode.COMPATIBLE) + .build(); + byte[] serialized = fury.serialize(resp); + DemoResponse o = (DemoResponse) fury.deserialize(serialized); + Assert.assertEquals(o.json, jsonObject); + Assert.assertEquals(o.objects, Collections.ofArrayList(jsonObject)); + } +}