lightcouch / LightCouch

CouchDB Java API
www.lightcouch.org
Apache License 2.0
67 stars 70 forks source link

Querying _all_docs gives stack trace. #50

Closed buckett closed 6 years ago

buckett commented 8 years ago

I was attempting to page through _all_docs and on the first page I'm getting a stack trace:

Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected STRING but was BEGIN_OBJECT
    at com.google.gson.Gson.fromJson(Gson.java:822)
    at com.google.gson.Gson.fromJson(Gson.java:875)
    at com.google.gson.Gson.fromJson(Gson.java:848)
    at org.lightcouch.CouchDbUtil.JsonToObject(CouchDbUtil.java:74)
    at org.lightcouch.View.queryView(View.java:196)
    at org.lightcouch.View.queryNextPage(View.java:310)
    at org.lightcouch.View.queryPage(View.java:266)
    at uk.ac.ox.it.CouchUpdater.cleanup(CouchUpdater.java:106)
    at uk.ac.ox.it.App.importSkills(App.java:50)
    at uk.ac.ox.it.App.main(App.java:34)
Caused by: java.lang.IllegalStateException: Expected STRING but was BEGIN_OBJECT
    at com.google.gson.internal.bind.JsonTreeReader.nextString(JsonTreeReader.java:154)
    at com.google.gson.internal.bind.TypeAdapters$13.read(TypeAdapters.java:358)
    at com.google.gson.internal.bind.TypeAdapters$13.read(TypeAdapters.java:346)
    at com.google.gson.Gson.fromJson(Gson.java:810)
    ... 9 more

My code is effectively this:

        View allDocs = couchClient.view("_all_docs");
        String pageParam = null;
        Page<Document> page;
        do {
            page = allDocs.queryPage(10, pageParam, Document.class);
            // Do stuff
            pageParam = page.getNextParam();
        } while (page.isHasNext());
buckett commented 8 years ago

It looks like org.lightcouch.View#queryNextPage assumes that the value in a view will always be a String which isn't the case for _all_docs where you have something like:

{
id: "052cc4aa11304d58ac23b722325f84cd",
key: "052cc4aa11304d58ac23b722325f84cd",
value: {
rev: "1-241b85056f1f333f8c8e90dc79796351"
},
doc: {
_id: "052cc4aa11304d58ac23b722325f84cd",
_rev: "1-241b85056f1f333f8c8e90dc79796351",
Type: "Foo",
title: "some-val",
position: 0
}
}
lightcouch commented 6 years ago

Fixed in release 0.2.0