Type deserialization is compliant with old version without CHANGELOG
This commit is contained in:
parent
745bb9eb3e
commit
21fcb502f2
|
@ -34,6 +34,7 @@ public class TypeMapper {
|
|||
static {
|
||||
mapper = new ObjectMapper();
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES, false);
|
||||
|
||||
mapper.registerSubtypes(Type.class);
|
||||
|
||||
|
|
|
@ -45,10 +45,14 @@ public class TypeImpl implements Type {
|
|||
public final static String CHANGELOG = "CHANGELOG";
|
||||
|
||||
public static final Map<TypeVersion, String> DEFAULT_CHANGELOG_MAP;
|
||||
private static final Map<String, String> DEFAULT_CHANGELOG_MAP_KEY_AS_STRING;
|
||||
|
||||
static {
|
||||
DEFAULT_CHANGELOG_MAP = new HashMap<>();
|
||||
DEFAULT_CHANGELOG_MAP.put(new TypeVersion(TypeVersion.MINIMAL_VERSION_STRING), "First Version");
|
||||
DEFAULT_CHANGELOG_MAP.put(new TypeVersion(TypeVersion.MINIMAL_VERSION_STRING), TypeVersion.MINIMAL_VERSION_DESCRIPTION);
|
||||
DEFAULT_CHANGELOG_MAP_KEY_AS_STRING = new HashMap<>();
|
||||
DEFAULT_CHANGELOG_MAP_KEY_AS_STRING.put(TypeVersion.MINIMAL_VERSION_STRING, TypeVersion.MINIMAL_VERSION_DESCRIPTION);
|
||||
|
||||
}
|
||||
|
||||
protected Header header;
|
||||
|
@ -56,19 +60,22 @@ public class TypeImpl implements Type {
|
|||
protected String name;
|
||||
protected String description;
|
||||
protected TypeVersion version;
|
||||
@JsonProperty(value = CHANGELOG_PROPERTY, required = false)
|
||||
@JsonInclude(Include.NON_NULL)
|
||||
protected Map<TypeVersion, String> changelog;
|
||||
|
||||
@JsonProperty(value="abstract")
|
||||
@JsonProperty(value = "abstract")
|
||||
protected boolean abstractType;
|
||||
protected Set<String> superClasses;
|
||||
|
||||
protected Set<PropertyDefinition> properties;
|
||||
|
||||
protected <E extends Element> Set<String> retrieveSuperClasses(Class<? extends E> type, Class<E> baseClass, String topSuperClass){
|
||||
protected <E extends Element> Set<String> retrieveSuperClasses(Class<? extends E> type, Class<E> baseClass,
|
||||
String topSuperClass) {
|
||||
Set<String> interfaceList = new HashSet<>();
|
||||
|
||||
if(type==baseClass){
|
||||
if(topSuperClass!=null) {
|
||||
if (type == baseClass) {
|
||||
if (topSuperClass != null) {
|
||||
interfaceList.add(topSuperClass);
|
||||
}
|
||||
return interfaceList;
|
||||
|
@ -78,7 +85,7 @@ public class TypeImpl implements Type {
|
|||
|
||||
for (Class<?> interfaceClass : interfaces) {
|
||||
|
||||
if(!baseClass.isAssignableFrom(interfaceClass)){
|
||||
if (!baseClass.isAssignableFrom(interfaceClass)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -90,12 +97,12 @@ public class TypeImpl implements Type {
|
|||
return interfaceList;
|
||||
}
|
||||
|
||||
protected Set<PropertyDefinition> retrieveListOfProperties(Class<?> type){
|
||||
protected Set<PropertyDefinition> retrieveListOfProperties(Class<?> type) {
|
||||
Set<PropertyDefinition> properties = new HashSet<>();
|
||||
for (Method m : type.getDeclaredMethods()){
|
||||
for (Method m : type.getDeclaredMethods()) {
|
||||
m.setAccessible(true);
|
||||
if(m.isAnnotationPresent(ISProperty.class)){
|
||||
if(m.isBridge()) {
|
||||
if (m.isAnnotationPresent(ISProperty.class)) {
|
||||
if (m.isBridge()) {
|
||||
continue;
|
||||
}
|
||||
ISProperty propAnnotation = m.getAnnotation(ISProperty.class);
|
||||
|
@ -108,44 +115,46 @@ public class TypeImpl implements Type {
|
|||
return properties;
|
||||
}
|
||||
|
||||
protected Class<?> getGenericClass(java.lang.reflect.Type type){
|
||||
protected Class<?> getGenericClass(java.lang.reflect.Type type) {
|
||||
TypeVariable<?> typeVariable = (TypeVariable<?>) type;
|
||||
java.lang.reflect.Type[] bounds = typeVariable.getBounds();
|
||||
java.lang.reflect.Type t = bounds[0];
|
||||
if(t instanceof ParameterizedType) {
|
||||
if (t instanceof ParameterizedType) {
|
||||
ParameterizedType parameterizedType = (ParameterizedType) t;
|
||||
return (Class<?>) parameterizedType.getRawType();
|
||||
}
|
||||
return (Class<?>) t;
|
||||
}
|
||||
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
public static Type getInstance(Class<? extends Element> clz) {
|
||||
Type typeDefinition = null;
|
||||
try {
|
||||
if(EntityElement.class.isAssignableFrom(clz)) {
|
||||
if (EntityElement.class.isAssignableFrom(clz)) {
|
||||
typeDefinition = EntityTypeImpl.getEntityTypeDefinitionInstance((Class<? extends EntityElement>) clz);
|
||||
return typeDefinition;
|
||||
} else if(RelationElement.class.isAssignableFrom(clz)){
|
||||
typeDefinition = RelationTypeImpl.getRelationTypeDefinitionInstance((Class<? extends RelationElement<?,?>>) clz);
|
||||
} else if (RelationElement.class.isAssignableFrom(clz)) {
|
||||
typeDefinition = RelationTypeImpl
|
||||
.getRelationTypeDefinitionInstance((Class<? extends RelationElement<?, ?>>) clz);
|
||||
return typeDefinition;
|
||||
} else if(PropertyElement.class.isAssignableFrom(clz)){
|
||||
} else if (PropertyElement.class.isAssignableFrom(clz)) {
|
||||
typeDefinition = new PropertyTypeImpl(clz);
|
||||
return typeDefinition;
|
||||
} else if(Type.class.isAssignableFrom(clz)) {
|
||||
} else if (Type.class.isAssignableFrom(clz)) {
|
||||
typeDefinition = new TypeImpl(clz);
|
||||
return typeDefinition;
|
||||
} else {
|
||||
throw new RuntimeException("Serialization required");
|
||||
}
|
||||
} finally {
|
||||
if(typeDefinition!=null) {
|
||||
if (typeDefinition != null) {
|
||||
logger.debug("{} : {} ", clz, typeDefinition);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected TypeImpl() {}
|
||||
protected TypeImpl() {
|
||||
}
|
||||
|
||||
protected TypeImpl(Class<? extends Element> clz) {
|
||||
this.name = TypeMapper.getType(clz);
|
||||
|
@ -154,13 +163,13 @@ public class TypeImpl implements Type {
|
|||
this.version = new TypeVersion(versionString);
|
||||
|
||||
this.changelog = TypeMapper.getStaticFieldByName(clz, CHANGELOG, DEFAULT_CHANGELOG_MAP);
|
||||
if(!changelog.containsKey(version)) {
|
||||
if (!changelog.containsKey(version)) {
|
||||
throw new RuntimeException("The Type " + name + " does not provided the appropriated changelog Map");
|
||||
}
|
||||
|
||||
this.abstractType = false;
|
||||
|
||||
if(clz.isAnnotationPresent(Abstract.class)){
|
||||
if (clz.isAnnotationPresent(Abstract.class)) {
|
||||
this.abstractType = true;
|
||||
}
|
||||
|
||||
|
@ -172,11 +181,10 @@ public class TypeImpl implements Type {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void setHeader(Header header){
|
||||
public void setHeader(Header header) {
|
||||
this.header = header;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
|
@ -192,7 +200,7 @@ public class TypeImpl implements Type {
|
|||
return version;
|
||||
}
|
||||
|
||||
@JsonGetter(value=VERSION_PROPERTY)
|
||||
@JsonGetter(value = VERSION_PROPERTY)
|
||||
protected String getVersionAsString() {
|
||||
return version.toString();
|
||||
}
|
||||
|
@ -207,19 +215,27 @@ public class TypeImpl implements Type {
|
|||
return changelog;
|
||||
}
|
||||
|
||||
@JsonGetter(value=CHANGELOG_PROPERTY)
|
||||
@JsonGetter(value = CHANGELOG_PROPERTY)
|
||||
@JsonInclude(Include.NON_NULL)
|
||||
public Map<String, String> getChangelogwithVersionAsString() {
|
||||
Map<String,String> map = new HashMap<>();
|
||||
for(TypeVersion typeVersion : changelog.keySet()) {
|
||||
if(this.changelog==null) {
|
||||
return DEFAULT_CHANGELOG_MAP_KEY_AS_STRING;
|
||||
}
|
||||
Map<String, String> map = new HashMap<>();
|
||||
for (TypeVersion typeVersion : changelog.keySet()) {
|
||||
map.put(typeVersion.toString(), changelog.get(typeVersion));
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
@JsonSetter(value = CHANGELOG_PROPERTY)
|
||||
@JsonSetter(value=CHANGELOG_PROPERTY)
|
||||
public void setChangelog(Map<String, String> changelog) {
|
||||
if(changelog==null) {
|
||||
this.changelog = DEFAULT_CHANGELOG_MAP;
|
||||
return;
|
||||
}
|
||||
this.changelog = new HashMap<>();
|
||||
for(String version : changelog.keySet()) {
|
||||
for (String version : changelog.keySet()) {
|
||||
this.changelog.put(new TypeVersion(version), changelog.get(version));
|
||||
}
|
||||
}
|
||||
|
@ -239,6 +255,4 @@ public class TypeImpl implements Type {
|
|||
return properties;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -161,6 +161,12 @@ public class SerializationTest {
|
|||
TypeVersion typeVersion = propertyType.getVersion();
|
||||
logger.debug("Version {}", typeVersion.toString());
|
||||
logger.info(ElementMapper.marshal(propertyType));
|
||||
|
||||
String json = "{\"@class\":\"PropertyType\",\"header\":null,\"name\":\"Header\",\"description\":\"\",\"superClasses\":[\"Property\"],\"properties\":[{\"@class\":\"PropertyDefinition\",\"name\":\"lastUpdateTime\",\"description\":\"\",\"mandatory\":true,\"readonly\":false,\"notnull\":true,\"max\":null,\"min\":null,\"regexp\":null,\"linkedType\":null,\"linkedClass\":null,\"type\":6},{\"@class\":\"PropertyDefinition\",\"name\":\"modifiedBy\",\"description\":\"\",\"mandatory\":true,\"readonly\":false,\"notnull\":true,\"max\":null,\"min\":null,\"regexp\":null,\"linkedType\":null,\"linkedClass\":null,\"type\":7},{\"@class\":\"PropertyDefinition\",\"name\":\"creator\",\"description\":\"\",\"mandatory\":true,\"readonly\":true,\"notnull\":true,\"max\":null,\"min\":null,\"regexp\":null,\"linkedType\":null,\"linkedClass\":null,\"type\":7},{\"@class\":\"PropertyDefinition\",\"name\":\"creationTime\",\"description\":\"\",\"mandatory\":true,\"readonly\":true,\"notnull\":true,\"max\":null,\"min\":null,\"regexp\":null,\"linkedType\":null,\"linkedClass\":null,\"type\":6},{\"@class\":\"PropertyDefinition\",\"name\":\"uuid\",\"description\":\"\",\"mandatory\":true,\"readonly\":true,\"notnull\":true,\"max\":null,\"min\":null,\"regexp\":\"^([a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}){1}$\",\"linkedType\":null,\"linkedClass\":null,\"type\":7}],\"abstract\":false,\"version\":\"1.0.0\"}";
|
||||
logger.info(json);
|
||||
@SuppressWarnings("unchecked")
|
||||
PropertyType<Header> headerType = (PropertyType<Header>) TypeMapper.deserializeTypeDefinition(json);
|
||||
logger.info(ElementMapper.marshal(headerType));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue