This is an automated email from the ASF dual-hosted git repository.
chaokunyang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-fury.git
The following commit(s) were added to refs/heads/main by this push:
new 72fdfd59 fix(java): fix TypeRef getSubType (#1608)
72fdfd59 is described below
commit 72fdfd5946b1e76a9708d25ea4d70db1433ca798
Author: Shawn Yang <[email protected]>
AuthorDate: Tue May 7 00:29:36 2024 +0800
fix(java): fix TypeRef getSubType (#1608)
## What does this PR do?
fix TypeRef getSubType
## Related issues
Closes #1604
## Does this PR introduce any user-facing change?
<!--
If any user-facing interface changes, please [open an
issue](https://github.com/apache/incubator-fury/issues/new/choose)
describing the need to do so and update the document if necessary.
-->
- [ ] Does this PR introduce any public API change?
- [ ] Does this PR introduce any binary protocol compatibility change?
## Benchmark
<!--
When the PR has an impact on performance (if you don't know whether the
PR will have an impact on performance, you can submit the PR first, and
if it will have impact on performance, the code reviewer will explain
it), be sure to attach a benchmark data here.
-->
---
.../org/apache/fury/benchmark/state/JsonTest.java | 57 ++++++++++++++++++++++
.../main/java/org/apache/fury/reflect/TypeRef.java | 13 ++---
.../java/org/apache/fury/reflect/TypeRefTest.java | 42 ++++++++++++++++
3 files changed, 102 insertions(+), 10 deletions(-)
diff --git
a/java/benchmark/src/test/java/org/apache/fury/benchmark/state/JsonTest.java
b/java/benchmark/src/test/java/org/apache/fury/benchmark/state/JsonTest.java
new file mode 100644
index 00000000..f96ea6a1
--- /dev/null
+++ b/java/benchmark/src/test/java/org/apache/fury/benchmark/state/JsonTest.java
@@ -0,0 +1,57 @@
+/*
+ * 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.benchmark.state;
+
+import com.alibaba.fastjson2.JSONObject;
+import org.apache.fury.Fury;
+import org.apache.fury.config.CompatibleMode;
+import org.apache.fury.config.Language;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+public class JsonTest {
+ public static class DemoResponse {
+ private JSONObject json;
+
+ public DemoResponse(JSONObject json) {
+ this.json = json;
+ }
+ }
+
+ @Test
+ public void testSerializeJson() {
+ // For issue: https://github.com/apache/incubator-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(true)
+ .registerGuavaTypes(false)
+ .withCompatibleMode(CompatibleMode.COMPATIBLE)
+ .build();
+ byte[] serialized = fury.serialize(resp);
+ DemoResponse o = (DemoResponse) fury.deserialize(serialized);
+ Assert.assertEquals(o.json, jsonObject);
+ }
+}
diff --git a/java/fury-core/src/main/java/org/apache/fury/reflect/TypeRef.java
b/java/fury-core/src/main/java/org/apache/fury/reflect/TypeRef.java
index 9a41fa13..b984bb32 100644
--- a/java/fury-core/src/main/java/org/apache/fury/reflect/TypeRef.java
+++ b/java/fury-core/src/main/java/org/apache/fury/reflect/TypeRef.java
@@ -498,15 +498,6 @@ public class TypeRef<T> {
for (int i = 0; i < fromArgs.length; i++) {
populateTypeMappings(mappings, fromArgs[i], toArgs[i]);
}
- } else if (supertypeWithArgsFromSubtype instanceof Class) {
- if (toType instanceof WildcardType) {
- return; // Okay to say Foo is <?>
- }
- // Can't map from a raw class to anything other than itself or a
wildcard.
- // You can't say "assuming String is Integer".
- // And we don't support "assuming String is T"; user has to say
"assuming T is String".
- throw new IllegalArgumentException(
- "No type mapping from " + supertypeWithArgsFromSubtype + " to " +
toType);
} else if (supertypeWithArgsFromSubtype instanceof GenericArrayType) {
if (toType instanceof WildcardType) {
return; // Okay to say A[] is <?>
@@ -516,7 +507,9 @@ public class TypeRef<T> {
((GenericArrayType)
supertypeWithArgsFromSubtype).getGenericComponentType();
populateTypeMappings(mappings, fromComponentType, componentType);
} else {
- throw new AssertionError("Unknown type: " + toType);
+ if (!(supertypeWithArgsFromSubtype instanceof Class)) {
+ throw new AssertionError("Unknown type: " + toType);
+ }
}
}
diff --git
a/java/fury-core/src/test/java/org/apache/fury/reflect/TypeRefTest.java
b/java/fury-core/src/test/java/org/apache/fury/reflect/TypeRefTest.java
new file mode 100644
index 00000000..a0cc6cf5
--- /dev/null
+++ b/java/fury-core/src/test/java/org/apache/fury/reflect/TypeRefTest.java
@@ -0,0 +1,42 @@
+/*
+ * 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.reflect;
+
+import static org.testng.Assert.*;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+import org.apache.fury.type.TypeUtils;
+import org.testng.annotations.Test;
+
+public class TypeRefTest {
+ static class MapObject extends LinkedHashMap<String, Object> {}
+
+ @Test
+ public void testGetSubtype() {
+ // For issue: https://github.com/apache/incubator-fury/issues/1604
+ TypeRef<? extends Map<String, Object>> typeRef =
+ TypeUtils.mapOf(MapObject.class, String.class, Object.class);
+ assertEquals(typeRef, TypeRef.of(MapObject.class));
+ assertEquals(
+ TypeUtils.mapOf(Map.class, String.class, Object.class),
+ new TypeRef<Map<String, Object>>() {});
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]