Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JsonIOExcpetion when trying to deserialize property value of generic type with generic type parameters #157

Closed
Dieler opened this issue May 22, 2015 · 4 comments

Comments

@Dieler
Copy link

Dieler commented May 22, 2015

When a snapshot state has a property of a generic type which in turn has generic type arguments, its deserialization raises a JsonIOException. E.g., trying to deserialize a value of type Map<String,EnumSet<SomeEnum>> results in

com.google.gson.JsonIOException: Invalid EnumSet type: class java.util.EnumSet
    at com.google.gson.internal.ConstructorConstructor$5.construct(ConstructorConstructor.java:149)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:78)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
    at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:187)
    at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:145)
    at com.google.gson.Gson.fromJson(Gson.java:803)
    at com.google.gson.Gson.fromJson(Gson.java:868)
    at org.javers.core.json.JsonConverter.fromJson(JsonConverter.java:86)
    at org.javers.core.json.JsonConverter$1.deserialize(JsonConverter.java:61)
    at org.javers.core.json.typeadapter.commit.CdoSnapshotStateDeserializer.decodePropertyValue(CdoSnapshotStateDeserializer.java:48)
    at org.javers.core.json.typeadapter.commit.CdoSnapshotStateDeserializer.deserialize(CdoSnapshotStateDeserializer.java:39)
    at org.javers.core.json.JsonConverter.snapshotStateFromJson(JsonConverter.java:108)

Gson as such is perfectly capable of handling such types. The actual problem seems to be the constructor of ParameterizedDehydratedType where the type parameters get lost:

    ParametrizedDehydratedType(JaversType javersType, final TypeMapper typeMapper) {
        this.rawType = javersType.getBaseJavaClass();

        this.actualTypeArguments =
                Lists.transform(javersType.getActualTypeArguments(), new Function<Type, Type>() {
                    public Type apply(Type input) {
                        return typeMapper.getJaversType(input).getRawDehydratedType();
                    }
                }).toArray(new Type[]{});
    }

Maybe the input type should be handled here in the same manner as in TypeMapper.getDehydratedType(),i.e., something like so:

    ParametrizedDehydratedType(JaversType javersType, final TypeMapper typeMapper) {
        this.rawType = javersType.getBaseJavaClass();

        this.actualTypeArguments =
                Lists.transform(javersType.getActualTypeArguments(), new Function<Type, Type>() {
                    public Type apply(Type input) {
                        JaversType javersType = typeMapper.getJaversType(input);
                        if (!javersType.isGenericType()){
                            return javersType.getRawDehydratedType();
                        }
                        return new ParameterizedDehydratedType(javersType, typeMapper);
                    }
                }).toArray(new Type[]{});
    }
@bartoszwalacik
Copy link
Member

well, Javers supports nested Generics to some extent (like Set<ThreadLocal<String>>)
but in fact we do not have test for Maps. Could you think about implementing a faling test in your forked javers repository?

for ex. here https://github.com/javers/javers/blob/b523a05b64b86631ab9fd01da4616f6518590087/javers-core/src/test/groovy/org/javers/core/metamodel/type/MapTypeTest.groovy

@Dieler
Copy link
Author

Dieler commented May 25, 2015

I've submitted a pull request for my suggested fix. See if it makes any sense to you.

@bartoszwalacik
Copy link
Member

ok, I'll take a look

@bartoszwalacik
Copy link
Member

@Dieler thanks for your contribution!
released in JaVers 1.2.6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants