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

Extended JSON writer should escape keys

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

      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.

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

              Created:
              Updated:
              Resolved: