Sync Gateway Query GET is not compatible with PUT

The object returned by GET {db}/_design/{ddoc} injects a whole bunch of extra validation syntax into the ‘map’ value for a view which, when returned to the server in a PUT request, invalidates the view.

For example:
PUT
{‘views’: {’_test’: {‘map’: ‘function(doc, meta) { if (doc.uid == “test1”) { emit(null, null); } }’}}}

creates:

GET
{‘views’: {’_test’: {‘map’: ‘function(doc,meta) {\n\t\t var sync = doc._sync;\n\t\t if (sync === undefined || meta.id.substring(0,6) == “_sync:”)\n\t\t return;\n\t\t if ((sync.flags & 1) || sync.deleted)\n\t\t return;\n\t\t var channels = [];\n\t\t var channelMap = sync.channels;\n\t\t\t\t\t\t\tif (channelMap) {\n\t\t\t\t\t\t\t\tfor (var name in channelMap) {\n\t\t\t\t\t\t\t\t\tremoved = channelMap[name];\n\t\t\t\t\t\t\t\t\tif (!removed)\n\t\t\t\t\t\t\t\t\t\tchannels.push(name);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t delete doc._sync;\n\t\t meta.rev = sync.rev;\n\t\t meta.channels = channels;\n\n\t\t var _emit = emit;\n\t\t (function(){\n\t\t\t var emit = function(key,value) {\n\t\t\t \t_emit(key,[channels, value]);\n\t\t\t };\n\t\t\t\t\t\t\t\t(function(doc, meta) { if (doc.uid == “test1”) { emit(null, null); } }) (doc, meta);\n\t\t\t\t\t\t\t}());\n\t\t\t\t\t\t\tdoc._sync = sync;\n\t\t\t\t\t\t}’}}}

And, if this new object is returned in a PUT request, the view is not added to the design document.

This present a problem when trying to update a design doc with a new view without removing all the previous views associated with it. Either all the (user defined) view definitions need to be stored as their own documents or the application needs to parse the object returned by GET and remove all the junk code before re-submitting the updated design document.

I have also posted this issue on StackOverflow:

Hi, I responded on Stack Overflow