Skip to content

Commit 040962a

Browse files
committed
disabled type hints when using builder or static factory method deserialization
1 parent 2d9d784 commit 040962a

File tree

3 files changed

+97
-4
lines changed

3 files changed

+97
-4
lines changed

src/main/java/com/arangodb/velocypack/VPack.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ private void addValue(
929929
serializeArray(name, value, builder, elemType);
930930
} else if (type instanceof Class && ((Class) type).isEnum()) {
931931
builder.add(name, ((Enum) value).name());
932-
} else if (type != value.getClass()) {
932+
} else if (shouldAddTypeHint(type, value, fieldInfo)) {
933933
addValue(name, value.getClass(), value, builder, fieldInfo,
934934
Collections.<String, Object>singletonMap(typeKey, value.getClass().getName()));
935935
} else {
@@ -938,14 +938,33 @@ private void addValue(
938938
}
939939
}
940940

941-
private void serializeArray(final String name, final Object value, final VPackBuilder builder, final Type type)
941+
private boolean shouldAddTypeHint(
942+
final Type type,
943+
final Object value,
944+
final FieldInfo fieldInfo
945+
) {
946+
final AnnotatedElement referencingElement = fieldInfo != null ? fieldInfo.getReferencingElement() : null;
947+
final BuilderInfo builderInfo = builderUtils.getBuilderInfo(type, referencingElement);
948+
if (builderInfo != null) {
949+
return false;
950+
}
951+
952+
final VPackCreatorMethodInfo factoryMethodInfo = vPackCreatorMethodUtils.getCreatorMethodInfo(type);
953+
if (factoryMethodInfo != null) {
954+
return false;
955+
}
956+
957+
return type != value.getClass();
958+
}
959+
960+
private void serializeArray(final String name, final Object value, final VPackBuilder builder, final Type type)
942961
throws ReflectiveOperationException, VPackException {
943962
builder.add(name, ValueType.ARRAY);
944963
for (int i = 0; i < Array.getLength(value); i++) {
945964
final Object element = Array.get(value, i);
946965
if (element != null) {
947-
final Type t = type != null ? type : element.getClass();
948-
addValue(null, t, element, builder, null, Collections.<String, Object> emptyMap());
966+
final Type t = type != null ? type : element.getClass();
967+
addValue(null, t, element, builder, null, Collections.<String, Object>emptyMap());
949968
} else {
950969
builder.add(ValueType.NULL);
951970
}

src/test/java/com/arangodb/velocypack/immutable/ImmutablesTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,33 @@ public void allArgsConstructor() {
182182
}
183183
}
184184

185+
@Test
186+
public void covariantCollection() {
187+
VPack vpack = new VPack.Builder().build();
188+
for (int i = 0; i < 3; i++) {
189+
PersonWithFriends original = PersonWithFriends.builder()
190+
.name("name")
191+
.age(987)
192+
.friends(Collections.singletonList(
193+
Person.builderFunction()
194+
.withName("name")
195+
.withAge(99)
196+
.withSecondNames(Arrays.asList("aaa", "bbb", "ccc"))
197+
.withAddresses(Arrays.asList(
198+
Collections.singletonMap("home", "Avocado Street 14"),
199+
Collections.singletonMap("work", "Java Street 14")
200+
))
201+
.buildIt()
202+
))
203+
.build();
204+
205+
System.out.println(original);
206+
VPackSlice serialized = vpack.serialize(original);
207+
System.out.println(serialized);
208+
PersonWithFriends deserialized = vpack.deserialize(serialized, PersonWithFriends.class);
209+
System.out.println(deserialized);
210+
assertThat(deserialized, is(original));
211+
}
212+
}
213+
185214
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* DISCLAIMER
3+
*
4+
* Copyright 2016 ArangoDB GmbH, Cologne, Germany
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
*/
20+
21+
package com.arangodb.velocypack.immutable;
22+
23+
import com.arangodb.velocypack.annotations.VPackPOJOBuilder;
24+
import org.immutables.value.Value;
25+
26+
import java.util.List;
27+
28+
/**
29+
* @author Michele Rastelli
30+
*/
31+
@Value.Immutable
32+
public abstract class PersonWithFriends {
33+
34+
@VPackPOJOBuilder
35+
public static ImmutablePersonWithFriends.Builder builder() {
36+
return ImmutablePersonWithFriends.builder();
37+
}
38+
39+
abstract String getName();
40+
41+
abstract int getAge();
42+
43+
abstract List<Person> getFriends();
44+
45+
}

0 commit comments

Comments
 (0)