Skip to content

Commit 44aa3e6

Browse files
authored
Fix problem parsing string to ObjectID
If client sent data, which has format like: {"id": "5b4bcb0f85e47a1fabb82f3e"} then gets error I added check for string.
1 parent b7c48f5 commit 44aa3e6

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

bson/objectid/objectid.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,25 @@ func (id *ObjectID) UnmarshalJSON(b []byte) error {
8888
copy(id[:], b)
8989
default:
9090
// Extended JSON
91-
m := make(map[string]string)
92-
err := json.Unmarshal(b, &m)
91+
var res interface{}
92+
err := json.Unmarshal(b, &res)
9393
if err != nil {
9494
return err
9595
}
96-
str, ok := m["$oid"]
96+
str, ok := res.(string)
9797
if !ok {
98-
return errors.New("not an extended JSON ObjectID")
98+
m, ok := res.(map[string]interface{})
99+
if !ok {
100+
return errors.New("not an extended JSON ObjectID")
101+
}
102+
oid, ok := m["$oid"]
103+
if !ok {
104+
return errors.New("not an extended JSON ObjectID")
105+
}
106+
str, ok = oid.(string)
107+
if !ok {
108+
return errors.New("not an extended JSON ObjectID")
109+
}
99110
}
100111

101112
if len(str) != 24 {

0 commit comments

Comments
 (0)