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

Repository with Enum argument: json can't serialize type [DATAMONGO-1421] #2332

Closed
spring-projects-issues opened this issue Apr 15, 2016 · 4 comments
Assignees
Labels
in: mapping Mapping and conversion infrastructure in: repository Repositories abstraction type: bug A general bug
Milestone

Comments

@spring-projects-issues
Copy link

Agoston Horvath opened DATAMONGO-1421 and commented

With version 1.8.2.RELEASE, our enum maps correctly to a string in the DB and vica versa.
If I simply change the spring-data-mongo version to 1.9.1.RELEASE, it breaks with the following stacktrace:

java.lang.RuntimeException: json can't serialize type : class com.bol.service.olg.server.beans.EmbeddedSocialListBean$SocialListType
	at com.mongodb.util.ClassMapBasedObjectSerializer.serialize(ClassMapBasedObjectSerializer.java:79)
	at com.mongodb.util.JSONSerializers$MapSerializer.serialize(JSONSerializers.java:323)
	at com.mongodb.util.ClassMapBasedObjectSerializer.serialize(ClassMapBasedObjectSerializer.java:82)
	at com.mongodb.util.JSON.serialize(JSON.java:53)
	at com.mongodb.util.JSON.serialize(JSON.java:39)
	at com.mongodb.BasicDBObject.toString(BasicDBObject.java:196)
	at org.springframework.data.mongodb.repository.query.PartTreeMongoQuery.createQuery(PartTreeMongoQuery.java:113)
	at org.springframework.data.mongodb.repository.query.AbstractMongoQuery.execute(AbstractMongoQuery.java:81)
	at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:482)
	at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:460)

Was the enum mapping feature removed from version 1.9.1 ?


Affects: 1.9.1 (Hopper SR1), 2.0 M1 (Kay), 1.10 GA (Ingalls)

Referenced from: pull request #448

Backported to: 1.10.2 (Ingalls SR2), 1.9.9 (Hopper SR9)

@spring-projects-issues
Copy link
Author

Christoph Strobl commented

Can you please provide a bit more context to this issue by adding the involved domain type and the repository finder method. Thanks!

@spring-projects-issues
Copy link
Author

Ryan Tracy commented

I am getting this same error. It was working then I reorganized my code a little and not it is throwing this same exception. Usage:

searchQuery.addCriteria(Criteria.where(ITransaction.REQUEST_TYPE).is(RequestType.CHANGEDEVICEID));

ITransaction.REQUEST_TYPE is a final String and
RequestType.CHANGEDEVICEID is an enum.

I am not sure why it quit working as I am using this same pattern all over.

There is a slight difference in the stack trace. Replace 113 with:
at java.lang.String.valueOf(String.java:2994)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at org.springframework.data.mongodb.core.query.Query.addCriteria(Query.java:99)
...

@spring-projects-issues
Copy link
Author

Ryan Tracy commented

So I found the issue. The error message does not match the actual problem. You get this error when you have two addCriteria statements on the same field.

To reproduce do something like this:

if (true) {
  searchQuery.addCriteria(Criteria.where(ITransaction.REQUEST_TYPE).is(RequestType.REQUEST_TYPE1));
}
if (true) {
  //If this call to addCriteria is made after the first for the same field, you will get the above exception.
  searchQuery.addCriteria(Criteria.where(ITransaction.REQUEST_TYPE).is(RequestType.REQUEST_TYPE2)); //Exception will occur here, but tells you "json can't serialize type"
}

The exception that is thrown is accurate, but misleading

@spring-projects-issues
Copy link
Author

Christoph Strobl commented

thanks for the feedback - good catch - there's an issue in creating the error message when values cannot be safely serialized to JSON. We'll get this fixed

@spring-projects-issues spring-projects-issues added type: bug A general bug in: repository Repositories abstraction in: mapping Mapping and conversion infrastructure labels Dec 30, 2020
@spring-projects-issues spring-projects-issues added this to the 2.0 M2 (Kay) milestone Dec 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: mapping Mapping and conversion infrastructure in: repository Repositories abstraction type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants