IBM / cloudant-java-sdk

Cloudant SDK for Java

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Some design documents cannot be serialized to DesignDocument class

jnhagelberg opened this issue · comments

We have noticed that many of our design documents cannot be serialized com.ibm.cloud.cloudant.v1.model.DesignDocument. As a result we have been forced to directly use the okhttp3 apis for many operations related to design documents. Here is an example of a design document that cannot be serialized/deserialized:

{
  "_id": "_design/adminSearch_v36",
  "language": "javascript",
  "views": {},
  "indexes": {
    "adminSearch": {
      "analyzer": {
        "name": "perfield",
        "default": "standard",
        "fields": {
          "metadata.name": "keyword"
        }
      },
      "index": "function (doc) { /* anything */ }"
}

The deserialization fails because it attempts to deserialize fields to Map<String, Analyzer>. In this case, the value is a primitive string and cannot be represented by the Analyzer class.

In cases where it is not possible to update the design document to the alternative format one workaround for the known issue is to use the bulk API to get the bytes of the design doc and use with an alternative deserializer of choice, thus bypassing the model, e.g. something like this with GSON

PostBulkGetOptions options = new PostBulkGetOptions.Builder()
        .db("mydb")
        .docs(Collections.singletonList(new BulkGetQueryDocument.Builder().id("_design/myddoc").build()))
        .latest(true)
        .build();
try (InputStream bulkGetStream = client.postBulkGetAsStream(options).execute().getResult(); InputStreamReader bulkGetReader = new InputStreamReader(bulkGetStream, "UTF-8")) {
    JsonObject result = new Gson().fromJson(bulkGetReader, JsonObject.class);
    JsonObject designDocument = result.getAsJsonArray("results").get(0).getAsJsonObject()
                                        .getAsJsonArray("docs").get(0).getAsJsonObject()
                                        .getAsJsonObject("ok");
    System.out.println("Id: " + designDocument.get("_id") + " Rev: " + designDocument.get("_rev"));
} catch (ServiceResponseException e) {
    System.out.println(String.format("%s %s %s", e.getStatusCode(), e.getMessage(), e.getDebuggingInfo()));
}