You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2019/11/20 07:32:09 UTC
[james-project] 26/41: JAMES-2813 forbid duplicate typeid for DTO
This is an automated email from the ASF dual-hosted git repository.
btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git
commit d8a83b0d0fd4da05cc1fed67a844a503c60020c3
Author: Matthieu Baechler <ma...@apache.org>
AuthorDate: Wed Oct 23 18:05:25 2019 +0200
JAMES-2813 forbid duplicate typeid for DTO
---
.../main/java/org/apache/james/json/JsonGenericSerializer.java | 10 ++++++++++
json/src/test/java/org/apache/JsonGenericSerializerTest.java | 8 ++++++++
2 files changed, 18 insertions(+)
diff --git a/json/src/main/java/org/apache/james/json/JsonGenericSerializer.java b/json/src/main/java/org/apache/james/json/JsonGenericSerializer.java
index bc2c156..0cf11dc 100644
--- a/json/src/main/java/org/apache/james/json/JsonGenericSerializer.java
+++ b/json/src/main/java/org/apache/james/json/JsonGenericSerializer.java
@@ -23,6 +23,7 @@ import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Set;
+import java.util.stream.Collectors;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
@@ -37,6 +38,7 @@ import com.fasterxml.jackson.datatype.guava.GuavaModule;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.github.steveash.guavate.Guavate;
+import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
@@ -90,10 +92,18 @@ public class JsonGenericSerializer<T, U extends DTO> {
private final DTOConverter<T, U> dtoConverter;
private JsonGenericSerializer(Set<? extends DTOModule<? extends T, ? extends U>> modules, Set<? extends DTOModule<?, ?>> nestedTypesModules, DTOConverter<T, U> converter) {
+ Preconditions.checkArgument(!hasDuplicateTypeIds(modules, nestedTypesModules));
this.dtoConverter = converter;
this.objectMapper = buildObjectMapper(Sets.union(modules, nestedTypesModules));
}
+ private boolean hasDuplicateTypeIds(Set<? extends DTOModule<?, ?>> modules, Set<? extends DTOModule<?, ?>> nestedTypesModules) {
+ return Sets.intersection(
+ modules.stream().map(DTOModule::getDomainObjectType).collect(Collectors.toSet()),
+ nestedTypesModules.stream().map(DTOModule::getDomainObjectType).collect(Collectors.toSet()))
+ .size() > 0;
+ }
+
private ObjectMapper buildObjectMapper(Set<? extends DTOModule<?, ?>> modules) {
ObjectMapper objectMapper = new ObjectMapper()
.registerModule(new Jdk8Module())
diff --git a/json/src/test/java/org/apache/JsonGenericSerializerTest.java b/json/src/test/java/org/apache/JsonGenericSerializerTest.java
index d9bcde1..235d19d 100644
--- a/json/src/test/java/org/apache/JsonGenericSerializerTest.java
+++ b/json/src/test/java/org/apache/JsonGenericSerializerTest.java
@@ -158,4 +158,12 @@ class JsonGenericSerializerTest {
.isInstanceOf(JsonGenericSerializer.UnknownTypeException.class);
}
+ @Test
+ void shouldThrowWhenRegisteringDuplicateTypeIds() {
+ assertThatThrownBy(() -> JsonGenericSerializer
+ .forModules(TestModules.FIRST_NESTED)
+ .withNestedTypeModules(TestModules.FIRST_NESTED))
+ .isInstanceOf(IllegalArgumentException.class);
+ }
+
}
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org