-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(java): Compatible mode on de/serialize api failed to deserialize (#…
…1996) ## What does this PR do? Read and write class data on COMPATIBLE mode for de/serializeJavaObject api. When COMPATIBLE mode is on and need to serialize and deserialize different POJO, users are required to register classes those are going to be serialized or deserialized. ## Related issues <!-- Is there any related issue? Please attach here. - #xxxx0 - #xxxx1 - #xxxx2 --> ## Does this PR introduce any user-facing change? <!-- If any user-facing interface changes, please [open an issue](https://github.com/apache/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. --> --------- Co-authored-by: Shawn Yang <[email protected]>
- Loading branch information
1 parent
1c250e1
commit d7ddd90
Showing
8 changed files
with
331 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
...est/java/org/apache/fury/serializer/compatible/DifferentPOJOCompatibleSerializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* 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.serializer.compatible; | ||
|
||
import org.apache.fury.Fury; | ||
import org.apache.fury.config.CompatibleMode; | ||
import org.apache.fury.config.Language; | ||
import org.apache.fury.serializer.compatible.classes.ClassCompleteField; | ||
import org.apache.fury.serializer.compatible.classes.ClassMissingField; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
/** | ||
* Test COMPATIBILITY mode that supports - same field type and name can be deserialized to other | ||
* class with different name - scrambled field order to make sure it could handle different field | ||
* order - missing or extra field from source class to target class - generic class | ||
*/ | ||
public class DifferentPOJOCompatibleSerializerTest extends Assert { | ||
|
||
Fury getFury(Class<?>... classes) { | ||
Fury instance = | ||
Fury.builder() | ||
.withLanguage(Language.JAVA) | ||
.withRefTracking(true) | ||
.withCompatibleMode(CompatibleMode.COMPATIBLE) | ||
.withMetaShare(true) | ||
.withScopedMetaShare(true) | ||
.requireClassRegistration(false) | ||
.withAsyncCompilation(true) | ||
.serializeEnumByName(true) | ||
.build(); | ||
if (classes != null) { | ||
for (Class<?> clazz : classes) { | ||
instance.register(clazz); | ||
} | ||
} | ||
; | ||
return instance; | ||
} | ||
|
||
@Test | ||
void testTargetHasLessFieldComparedToSourceClass() throws InterruptedException { | ||
|
||
ClassCompleteField<String> subclass = new ClassCompleteField<>("subclass", "subclass2"); | ||
ClassCompleteField<ClassCompleteField<String>> classCompleteField = | ||
new ClassCompleteField<>(subclass, subclass); | ||
byte[] serialized = getFury(ClassCompleteField.class).serializeJavaObject(classCompleteField); | ||
ClassMissingField<ClassMissingField<String>> classMissingField = | ||
getFury(ClassMissingField.class).deserializeJavaObject(serialized, ClassMissingField.class); | ||
|
||
assertEq(classCompleteField, classMissingField); | ||
} | ||
|
||
@Test | ||
void testTargetHasMoreFieldComparedToSourceClass() throws InterruptedException { | ||
|
||
ClassMissingField<String> subclass = new ClassMissingField<>("subclass"); | ||
ClassMissingField classMissingField = new ClassMissingField(subclass); | ||
byte[] serialized = getFury(ClassMissingField.class).serializeJavaObject(classMissingField); | ||
|
||
ClassCompleteField classCompleteField = | ||
getFury(ClassCompleteField.class) | ||
.deserializeJavaObject(serialized, ClassCompleteField.class); | ||
|
||
assertEq(classCompleteField, classMissingField); | ||
} | ||
|
||
void assertEq(ClassCompleteField classCompleteField, ClassMissingField classMissingField) { | ||
assertEqSubClass( | ||
(ClassCompleteField) classCompleteField.getPrivateFieldSubClass(), | ||
(ClassMissingField) classMissingField.getPrivateFieldSubClass()); | ||
assertEquals(classCompleteField.getPrivateMap(), classMissingField.getPrivateMap()); | ||
assertEquals(classCompleteField.getPrivateList(), classMissingField.getPrivateList()); | ||
assertEquals(classCompleteField.getPrivateString(), classMissingField.getPrivateString()); | ||
assertEquals(classCompleteField.getPrivateInt(), classMissingField.getPrivateInt()); | ||
} | ||
|
||
void assertEqSubClass( | ||
ClassCompleteField classCompleteField, ClassMissingField classMissingField) { | ||
assertEquals( | ||
classCompleteField.getPrivateFieldSubClass(), classMissingField.getPrivateFieldSubClass()); | ||
assertEquals(classCompleteField.getPrivateMap(), classMissingField.getPrivateMap()); | ||
assertEquals(classCompleteField.getPrivateList(), classMissingField.getPrivateList()); | ||
assertEquals(classCompleteField.getPrivateString(), classMissingField.getPrivateString()); | ||
assertEquals(classCompleteField.getPrivateInt(), classMissingField.getPrivateInt()); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...-core/src/test/java/org/apache/fury/serializer/compatible/classes/ClassCompleteField.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* 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.serializer.compatible.classes; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
|
||
@EqualsAndHashCode | ||
@Getter | ||
public class ClassCompleteField<T> { | ||
private boolean privateBoolean = true; | ||
private int privateInt = 10; | ||
private String privateString = "notNull"; | ||
private Map<String, String> privateMap; | ||
private List<String> privateList; | ||
private T privateFieldSubClass; | ||
|
||
private boolean privateBoolean2 = true; | ||
private int privateInt2 = 10; | ||
private String privateString2 = "notNull"; | ||
private Map<String, String> privateMap2; | ||
private List<String> privateList2; | ||
private T privateFieldSubClass2; | ||
|
||
public ClassCompleteField(T privateFieldSubClass, T privateFieldSubClass2) { | ||
privateMap = new HashMap<>(); | ||
privateMap.put("a", "b"); | ||
privateList = new ArrayList<>(); | ||
privateList.add("a"); | ||
|
||
privateMap2 = new HashMap<>(); | ||
privateMap2.put("a", "b"); | ||
privateList2 = new ArrayList<>(); | ||
privateList2.add("a"); | ||
|
||
this.privateFieldSubClass = privateFieldSubClass; | ||
this.privateFieldSubClass2 = privateFieldSubClass2; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...y-core/src/test/java/org/apache/fury/serializer/compatible/classes/ClassMissingField.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* 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.serializer.compatible.classes; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
|
||
@EqualsAndHashCode | ||
@Getter | ||
public class ClassMissingField<T> { | ||
private T privateFieldSubClass; | ||
private List<String> privateList; | ||
private Map<String, String> privateMap; | ||
private String privateString = "missing"; | ||
private int privateInt = 999; | ||
private boolean privateBoolean = false; | ||
|
||
public ClassMissingField(T privateFieldSubClass) { | ||
privateMap = new HashMap<>(); | ||
privateMap.put("z", "x"); | ||
privateList = new ArrayList<>(); | ||
privateList.add("c"); | ||
this.privateFieldSubClass = privateFieldSubClass; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...re/src/test/java/org/apache/fury/serializer/compatible/classes/SubclassCompleteField.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* 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.serializer.compatible.classes; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@EqualsAndHashCode | ||
public class SubclassCompleteField { | ||
private boolean privateBoolean = true; | ||
private int privateInt = 10; | ||
private String privateString = "notNull"; | ||
private Map<String, String> privateMap; | ||
private List<String> privateList; | ||
|
||
private boolean privateBoolean2 = true; | ||
private int privateInt2 = 10; | ||
private String privateString2 = "notNull"; | ||
private Map<String, String> privateMap2; | ||
private List<String> privateList2; | ||
|
||
public SubclassCompleteField() { | ||
privateMap = new HashMap<>(); | ||
privateMap.put("a", "b"); | ||
privateList = new ArrayList<>(); | ||
privateList.add("a"); | ||
|
||
privateMap2 = new HashMap<>(); | ||
privateMap2.put("a", "b"); | ||
privateList2 = new ArrayList<>(); | ||
privateList2.add("a"); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...ore/src/test/java/org/apache/fury/serializer/compatible/classes/SubclassMissingField.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* 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.serializer.compatible.classes; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@EqualsAndHashCode | ||
public class SubclassMissingField { | ||
private boolean privateBoolean = true; | ||
private int privateInt = 10; | ||
private String privateString = "notNull"; | ||
private Map<String, String> privateMap; | ||
private List<String> privateList; | ||
|
||
public SubclassMissingField() { | ||
privateMap = new HashMap<>(); | ||
privateMap.put("a", "b"); | ||
privateList = new ArrayList<>(); | ||
privateList.add("a"); | ||
} | ||
} |