[GODRIVER-1777] BSON Key conflict in unmarshaling Created: 25/Oct/20  Updated: 28/Oct/23  Resolved: 01/Dec/20

Status: Closed
Project: Go Driver
Component/s: BSON
Affects Version/s: None
Fix Version/s: 1.4.4

Type: Bug Priority: Major - P3
Reporter: Alessandro Sanino Assignee: Isabella Siu (Inactive)
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: PNG File image-2020-10-25-20-36-37-061.png     File test.go    

 Description   

Hello, I am trying to do the following

``` go
package main

import (
    "fmt"

    "github.com/gofrs/uuid"
    "gitlab.tryvium.io/booking-platform/booking-platform-backend/mongoutil"
    "go.mongodb.org/mongo-driver/mongo"
    "go.mongodb.org/mongo-driver/mongo/options"
    "golang.org/x/net/context"
)

type A struct 

{     ID   uuid.UUID `bson:"id"`     Name string    `bson:"name,required"` }

type Alias struct 

{     A  `bson:",inline"`     ID mongoutil.UUID `bson:"id,required"` }

func main() 

{     c, _ := mongo.Connect(context.Background(), options.Client().ApplyURI("mongodb://admin:admin@localhost:27017"))     coll := c.Database("tryvium").Collection("test")     id, _ := uuid.NewV4()     aa := A\{ID: id, Name: "test"}

    bb := Alias{A: aa, ID: mongoutil.UUID(aa.ID)}
    _, err := coll.InsertOne(context.Background(), bb)

    fmt.Println(err)
}
```

However I get the following error on run

`cannot transform type main.Alias to a BSON Document: struct main.Alias) duplicated key id`

Is there any way to restructure my 2 structs to achieve what I want, which is having ID field changed in `Alias` struct?



 Comments   
Comment by Githook User [ 01/Dec/20 ]

Author:

{'name': 'Isabella Siu', 'email': 'isabella.siu@mongodb.com', 'username': 'iwysiu'}

Message: GODRIVER-1777 allow inline struct fields to be overwritten by higher … (#535)
Branch: release/1.4
https://github.com/mongodb/mongo-go-driver/commit/3f950759b190548d5643b7da4af8472efa9de216

Comment by Githook User [ 01/Dec/20 ]

Author:

{'name': 'Isabella Siu', 'email': 'isabella.siu@mongodb.com', 'username': 'iwysiu'}

Message: GODRIVER-1777 allow inline struct fields to be overwritten by higher … (#535)
Branch: master
https://github.com/mongodb/mongo-go-driver/commit/7841aba303d04e74c006bcd7d6fbed4fee4b87a2

Comment by Isabella Siu (Inactive) [ 11/Nov/20 ]

Hi a.sanino@tryvium.io ,

I'll schedule it for 1.4.4, which will be released on December 1st.

Comment by Alessandro Sanino [ 11/Nov/20 ]

thanks @Isabella Siu

which is the ETA for this to come out?

Comment by Isabella Siu (Inactive) [ 11/Nov/20 ]

Hi a.sanino@tryvium.io,

As an update on this ticket, I've looked more into how encoding/json processes fields with the same names, and we will be changing bson to allow for field replacement for embedded structs.

Comment by Alessandro Sanino [ 02/Nov/20 ]

@Isabella Siu

No, that's not what I want.
I the "ID" value in the Alias struct to replace the "ID" field from A struct in resulting BSON, which is the same behaviour we have in marshalling and unmarshalling JSON

Comment by Isabella Siu (Inactive) [ 02/Nov/20 ]

Hi a.sanino@tryvium.io,

The problem here is that A.ID and Alias.ID try to go to the same key (id) when Alias is marshaled to bson. Assuming that you want the Alias.ID value to become _id, you can change Alias like this:

type Alias struct {
	A  `bson:",inline"`
	ID mongoutil.UUID `bson:"_id,required"`
}

Hopefully this fixes your issue!

Comment by Alessandro Sanino [ 25/Oct/20 ]

Added the code formatted here for a better understanding

package main
 
import (
	"fmt"
 
	"github.com/gofrs/uuid"
	"gitlab.tryvium.io/booking-platform/booking-platform-backend/mongoutil"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"golang.org/x/net/context"
)
 
type A struct {
	ID   uuid.UUID `bson:"id"`
	Name string    `bson:"name,required"`
}
 
type Alias struct {
	A  `bson:",inline"`
	ID mongoutil.UUID `bson:"id,required"`
}
 
func main() {
	c, _ := mongo.Connect(context.Background(), options.Client().ApplyURI("mongodb://admin:admin@localhost:27017"))
 
	coll := c.Database("tryvium").Collection("test")
 
	id, _ := uuid.NewV4()
 
	aa := A{ID: id, Name: "test"}
	bb := Alias{A: aa, ID: mongoutil.UUID(aa.ID)}
	_, err := coll.InsertOne(context.Background(), bb)
 
	fmt.Println(err)
}

And the error

cannot transform type main.Alias to a BSON Document: struct main.Alias) duplicated key id

The ideal result is represented by the following image (from Compass)

NOTE: mongoutil package is a UUID package which converts the binary UUID with subtype, explicit for mongoDB

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