[GODRIVER-2407] BSON Unmarshal to struct uses incorrect type alias for interface{} Created: 04/May/22  Updated: 28/Oct/23  Resolved: 27/May/22

Status: Closed
Project: Go Driver
Component/s: BSON
Affects Version/s: 1.9.1
Fix Version/s: 1.10.0, 1.10.0-beta1

Type: Bug Priority: Unknown
Reporter: Thomas Chandelle Assignee: Preston Vasquez
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Summary

I'm trying to unmarshal some document received from mongo into a struct. This struct has a member that is a map[string]interface but with an alias (type metadata map[string]interface{} {}in the example below). This map can contains any data, including other map[string]interface{}{}.

When unmarshalling the document, it's correctly parsing this member as type metadata, but it's also converting map that are included in the map as this the type metadata. I think it should be map[string]interface instead.

Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

1.9.1

How to Reproduce

https://go.dev/play/p/baefmEyupxI 

Unmarshal as 

{{main.data{Metadata:main.metadata{"foo":main.metadata

{"bar":"baz"}

}}}}

But expected as 

{{main.data{Metadata:main.metadata{"foo":map[string]interface {}

{"bar":"baz"}

}}}}
 

Additional Background

Please provide any additional background information that may be helpful in diagnosing the bug.



 Comments   
Comment by Preston Vasquez [ 27/May/22 ]

thomas.chandelle@gmail.com , thank you for bringing this to the teams attention.  We have introduced a work-around intended to default empty types into either primitive.M or primitive.D.  It’s worth noting that there is no encodable value for a “BSON Map”; all maps are encoded as embedded documents.  In your case, since the “embedded map” is an empty interface, we can only tell the decoder to default it to primitive.M or primitive.D.  This logic will be available next week after the version 1.10.0 release.  Here is an example of the usage:

// You can edit this code!
// Click here and start typing.
package main
 
import (
	"log"
 
	"go.mongodb.org/mongo-driver/bson"
)
 
type someMap map[string]interface{}
 
func main() {
    in := make(someMap)
    in["foo"] = map[string]interface{}{"bar": "baz"}
 
    bytes, err := Marshal(in)
    if err != nil {
        t.Fatal(err)
    }
 
    var bsonOut someMap
    dec, err := NewDecoder(bsonrw.NewBSONDocumentReader(bytes))
    if err != nil {
        t.Fatal(err)
    }
    dec.DefaultDocumentM()
    if err := dec.Decode(&bsonOut); err != nil {
        t.Fatal(err)
    }
 
    log.Printf("%#v", bsonOut)
    // bson.someMap{"foo":primitive.M{"bar":"baz"}}
}

Comment by Githook User [ 26/May/22 ]

Author:

{'name': 'Preston Vasquez', 'email': 'prestonvasquez@icloud.com', 'username': 'prestonvasquez'}

Message: GODRIVER-2407 Add DocumentDecodeType to DecodeContext (#951)

Co-authored-by: Benjamin Rewis <32186188+benjirewis@users.noreply.github.com>

Co-authored-by: Benjamin Rewis <32186188+benjirewis@users.noreply.github.com>
Branch: master
https://github.com/mongodb/mongo-go-driver/commit/8f19bdbde525ac6afbfae22f334ff15256f176b3

Comment by Preston Vasquez [ 17/May/22 ]

thomas.chandelle@gmail.com Thank you for bringing this to our attention! The team has begun looking into the issue.

Generated at Thu Feb 08 08:38:31 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.