Skip to content

The Unified and Autonomous Development Platform

Fix issue #291 - Using TypeId.NAME instead of TypeId.CLASS in RESTful api json serialization/deserialization

Robin Shen3db50157c6b1
diff --git a/server-core/src/main/java/io/onedev/server/util/jackson/ObjectMapperProvider.java b/server-core/src/main/java/io/onedev/server/util/jackson/ObjectMapperProvider.java
index b16c5e7bef..b3125f386a 100644
--- a/server-core/src/main/java/io/onedev/server/util/jackson/ObjectMapperProvider.java
+++ b/server-core/src/main/java/io/onedev/server/util/jackson/ObjectMapperProvider.java
@@ -1,8 +1,10 @@
package io.onedev.server.util.jackson;
+import java.io.Serializable;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
+import java.util.stream.Collectors;
import javax.inject.Inject;
import javax.inject.Provider;
@@ -25,13 +27,18 @@ import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.jsontype.impl.StdTypeResolverBuilder;
import com.fasterxml.jackson.databind.util.StdDateFormat;
+import io.onedev.commons.launcher.loader.ImplementationRegistry;
+
@Singleton
public class ObjectMapperProvider implements Provider<ObjectMapper> {
private final Set<ObjectMapperConfigurator> configurators;
+ private final ImplementationRegistry implementationRegistry;
+
@Inject
- public ObjectMapperProvider(Set<ObjectMapperConfigurator> configurators) {
+ public ObjectMapperProvider(ImplementationRegistry implementationRegistry, Set<ObjectMapperConfigurator> configurators) {
+ this.implementationRegistry = implementationRegistry;
this.configurators = configurators;
}
@@ -43,25 +50,34 @@ public class ObjectMapperProvider implements Provider<ObjectMapper> {
@Override
public TypeDeserializer buildTypeDeserializer(DeserializationConfig config,
JavaType baseType, Collection<NamedType> subtypes) {
- return useForType(baseType) ? super.buildTypeDeserializer(config, baseType, subtypes) : null;
+ return useForType(baseType) ? super.buildTypeDeserializer(config, baseType, getSubTypes(baseType)) : null;
}
+ private Collection<NamedType> getSubTypes(JavaType baseType) {
+ return implementationRegistry.getImplementations(baseType.getRawClass())
+ .stream()
+ .map(it->new NamedType(it))
+ .collect(Collectors.toList());
+ }
+
@Override
public TypeSerializer buildTypeSerializer(SerializationConfig config,
JavaType baseType, Collection<NamedType> subtypes) {
- return useForType(baseType) ? super.buildTypeSerializer(config, baseType, subtypes) : null;
+ return useForType(baseType) ? super.buildTypeSerializer(config, baseType, getSubTypes(baseType)) : null;
}
private boolean useForType(JavaType t) {
return !Collection.class.isAssignableFrom(t.getRawClass())
&& !Map.class.isAssignableFrom(t.getRawClass())
&& t.getRawClass() != JsonNode.class
+ && t.getRawClass() != Object.class
+ && t.getRawClass() != Serializable.class
&& !Enum.class.isAssignableFrom(t.getRawClass())
&& !t.isConcrete();
}
};
- typer = typer.init(JsonTypeInfo.Id.CLASS, null);
+ typer = typer.init(JsonTypeInfo.Id.NAME, null);
typer = typer.inclusion(JsonTypeInfo.As.PROPERTY);
mapper.setDefaultTyping(typer);
mapper.setDateFormat(new StdDateFormat().withColonInTimeZone(true));
diff --git a/server-core/src/main/java/io/onedev/server/web/page/help/ExampleValuePanel.java b/server-core/src/main/java/io/onedev/server/web/page/help/ExampleValuePanel.java
index 1a78f8a7dd..d3fe35d4ac 100644
--- a/server-core/src/main/java/io/onedev/server/web/page/help/ExampleValuePanel.java
+++ b/server-core/src/main/java/io/onedev/server/web/page/help/ExampleValuePanel.java
@@ -414,7 +414,7 @@ public class ExampleValuePanel extends Panel {
@Override
public Serializable getObject() {
- return getValue().getClass().getName();
+ return getValue().getClass().getSimpleName();
}
}, valueInfoModel, requestBodyClass));
@@ -440,7 +440,7 @@ public class ExampleValuePanel extends Panel {
@Override
public String getLabel() {
- return clazz.getName();
+ return clazz.getSimpleName();
}
@Override