Skip to content

Unmarshalling errors due to DesignDocumentViewsMapReduce.Map schema assumptions #507

Open
@jchunkins

Description

@jchunkins

Describe the bug

Calls to CloudantV1.GetDesignDocument and CloudantV1.GetDesignDocumentWithContext will fail because the model struct DesignDocumentViewsMapReduce does not take into account other possibilities that can be generated through valid cloudant index creation. To be clear, the struct structure that is causing the problem is DesignDocument.Views.map[<view name>]DesignDocumentViewsMapReduce.Map.

To illustrate the issue, here's a design document that was created as a byproduct of using the Query Indexes to create an index:

{
 "id": "_design/428a2f2506db4e77001bfe08fa0b79ea9eaf0279",
 "key": "_design/428a2f2506db4e77001bfe08fa0b79ea9eaf0279",
 "value": {
  "rev": "1-a3d9d6e8096d729c0b258dd96ab59fa8"
 },
 "doc": {
  "_id": "_design/428a2f2506db4e77001bfe08fa0b79ea9eaf0279",
  "_rev": "1-a3d9d6e8096d729c0b258dd96ab59fa8",
  "language": "query",
  "views": {
   "foo-json-index": {
    "map": {
     "fields": {
      "foo": "asc"
     },
     "partial_filter_selector": {}
    },
    "reduce": "_count",
    "options": {
     "def": {
      "fields": [
       "foo"
      ]
     }
    }
   }
  }
 }
}

Note that the doc.language is a query (as opposed to some other query server implementation like javascript) and the doc.views.foo-json-index.map value contains an object rather than a string as the DesignDocumentViewsMapReduce model expects. This results in the following unmarshalling error:

error unmarshalling cloudantv1.DesignDocument: error unmarshalling property 'views' as map[string]cloudantv1.DesignDocumentViewsMapReduce: error unmarshalling property 'map': json: cannot unmarshal object into Go value of type string

The code fails when the core.UnmarshalModel function is called:

core.UnmarshalModel(rawResponse, "", &result, UnmarshalDesignDocument)

To Reproduce

  1. In the Cloudant web browser, click the Design Documents + button and then chose Query Indexes.
  2. Enter the following JSON:
{
  "index": {
    "fields": [
      "foo"
    ]
  },
  "name": "foo-json-index",
  "type": "json"
}
  1. Click Create Index
  2. Look through the design documents and find the design document that was created as a result of creating the index
  3. Use the CloudantV1.GetDesignDocument and CloudantV1.GetDesignDocumentWithContext against the resulting design document.

Expected behavior

I expected that the unmarshalling would succeed by taking into account the type of query server used. Presumably this would require custom unmarshalling logic to handle this scenario.

As a workaround, if there was set of functions to allow access to the raw data such as the following examples:

  • A way to get the content without unmarshalling
    • GetDesignDocumentAsStream
    • GetDesignDocumentAsStreamWithContext
  • A way to create design documents that don't necessarily conform to the DesignDocument schema. Maybe accept a map or just a raw string body?
    • PutDesignDocumentWithMap
    • PutDesignDocumentWithMapWithContext

Screenshots

Must gather (please complete the following information):

  • SDK Version [e.g. 1.2.1]: v0.7.7
  • Go Version [e.g. Go 1.21]: 1.22.1
  • Name of service that you're trying to use (if applicable)
  • Name of operation that you're trying to invoke (if applicable)

Additional context

I was not able to see another way to handle this aside from rolling my own functions to handle design documents of the type described.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions