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

fix(java): fix TypeRef getSubType #1608

Merged
merged 2 commits into from
May 6, 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
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -498,15 +498,6 @@ private void populateTypeMappings(
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 <?>
Expand All @@ -516,7 +507,9 @@ private void populateTypeMappings(
((GenericArrayType) supertypeWithArgsFromSubtype).getGenericComponentType();
populateTypeMappings(mappings, fromComponentType, componentType);
} else {
throw new AssertionError("Unknown type: " + toType);
if (!(supertypeWithArgsFromSubtype instanceof Class)) {
throw new AssertionError("Unknown type: " + toType);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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>>() {});
}
}
Loading