Error 400 Invalid JSON with spring data couchbase generated JSON

I get this error when trying to sync spring data couchbase generated JSON in particular for a “_class” attribute, I couldn’t find a way to remove this attribute from springdata, any workaround?

Short answer: You’ll need to remove or rename that attribute and any other ones whose names start with “_”.

Couchbase Mobile doesn’t allow user-defined top-level JSON properties whose names begin with an “_”; those names are reserved for metadata used by revision handling and the replicator. (This comes from CouchDB. Couchbase Server took out this restriction, because it also took out revision handling and replication, but Sync Gateway and Couchbase Lite support those so we couldn’t. We also aim for compatibility with CouchDB and other compatible libraries like PouchDB, so we have to treat metadata the same way.)

@jens, can i please ask you to clarify? Spring data puts “_class” property by default. Do we need to workaround it on spring data end or in couchbase function somehow?

Thank you,
Leo

You’d have to remove or rename the _class property before saving the doc to CBL. Or nest the Spring-generated object inside a property of the document.

Hm, maybe i did not ask my question clearly?
Here is the example of payload that Spring data puts in Couchbase

{
"_class": “namespace.to.entity.User”,
“check”: false,
“created_at”: “2015-04-29T16:34:23.979Z”,
“text”: “text goes here”
}

the “_class” property is put by spring data without me adding it there. It just looks up the entity class and adds this property.

So the question is - at which point should i remove it?

Leo

I think you’ll need to provide more detail on what you’re doing and where the error came from. In particular, what do you mean by “payload that Spring data puts in Couchbase” — the Spring library has no knowledge of Couchbase, so how is the JSON being added to a document?

@jens The error occurs on iOS side. The spring library that we use actually knows about CouchBase: https://github.com/spring-projects/spring-data-couchbase

The full use case i like this:
Java Server submits data using spring-data-couchbase to CouchBase bucket
CouchBase bucket get replicated (via shadowing) to CouchBase Gateway Sync Bucket
This CouchBase Gateway Sync Bucket is then distributed to mobile clients.
The error occurs on deserialization(?) on iOS side.

Does that shed more light?

Leo

Ah, OK. The root problem is that Couchbase Server doesn’t restrict any doc property names, but (for historical reasons) Couchbase Lite and Sync Gateway do.

What should probably be happening is that the gateway’s shadowing implementation should strip out “_”-prefixed properties when copying from the app bucket. I don’t do much work on the gateway these days but I seem to recall this issue coming up recently. You could check the issue tracker to see if there’s a bug on it (which might already be closed if it’s been fixed for the upcoming 1.1.)

@jens Thank you, that makes sense. As we are using Amazon image for couchbase, we probably should workaround issue before it’s released in 1.1

So if we were to workaround it ourselves, where do you think strip function should reside?

Leo

@jens As of tracker, the one i found brought me here :slight_smile:

If you’re working around this, the best place to put the fix is where Spring writes to Couchbase Server. Modify it to remove the _class property.

By ‘issue tracker’ I meant Sync Gateway’s. I don’t think anyone on the mobile team is involved with Spring.

@jens I think i worked it around on spring data end.

If someone is interested:
You’d need to override couchbaseTemplate in couch base config and pass your own instance of MappingCouchbaseConverter that has typeMapper created with custom DefaultCouchbaseTypeMapper. Unfortunately typeMapper property is protected and does not have a setter, so you’d have to extend MappingCouchbaseConverter in order to change it.

Thank you for your help @jens
Leo