Uploaded image for project: 'Go Driver'
  1. Go Driver
  2. GODRIVER-1456

Extended JSON writer should escape keys

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.2.1
    • Component/s: JSON & ExtJSON
    • Labels:
      None
    • Case:

      Description

      The extJSONValueWriter.WriteDocumenElement() method should escape keys when writing. The BI Connector team encountered an issue where a key with these characters "\usb#" could not be unmarshalled since that is not a valid unicode sequence. A simpler example is:

      func main() {
      	key := "\""
      	doc := bson.D{{Key: key, Value: 1}}
      	b, err := bson.MarshalExtJSON(&doc, false, false)
      	if err != nil {
      		fmt.Printf("marshal error: %v\n", err)
      		return
      	}
       
      	fmt.Printf("doc: %s\n", string(b))
       
      	err = bson.UnmarshalExtJSON(b, false, &doc)
      	if err != nil {
      		fmt.Printf("unmarshal error: %v\n", err)
      	}
      }
      

      This outputs:

      doc: {""":1}
      unmarshal error: invalid JSON input: missing colon after key ""
      

      https://github.com/mongodb/mongo-go-driver/blob/b5d915e43f066f88d5ea7863ec42a314a9d877ad/bson/bsonrw/extjson_writer.go#L539

      I believe this is the only line that requires a change; the key should be escaped before writing.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              isabella.siu Isabella Siu (Inactive)
              Reporter:
              matthew.chiaravalloti Matthew Chiaravalloti
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: