[GODRIVER-1233] Truncate Bug Created: 13/Aug/19  Updated: 28/Oct/23  Resolved: 04/Sep/19

Status: Closed
Project: Go Driver
Component/s: BSON
Affects Version/s: 1.1.0
Fix Version/s: 1.1.1

Type: Bug Priority: Major - P3
Reporter: Craig Wilson Assignee: Alice Thum
Resolution: Fixed Votes: 0
Labels: neweng
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

I've got the code below that should ignore truncation errors, but returns an error indicating it can't truncate:

        type collStats struct {
		CollectionName string `bson:"collStats"`
		Scale          *int64 `bson:"scale" valid:"optional,gt(0)~scale must be a number > 0"`
	}
 
	var cmd collStats
	decodeContext := bsoncodec.DecodeContext{
		Registry: bson.DefaultRegistry,
		Truncate: true,
	}
	err := bson.UnmarshalWithContext(decodeContext, doc, &cmd)
	if err != nil {
		return nil, mongoerrors.Wrap(err, mongoerrors.CodeFailedToParse, "unable to parse the collStats command")
	}

produces the following error:

{"collStats": "foo", "scale": 29.821 } -> IntDecodeValue can only truncate float64 to an integer type when truncation is enabled



 Comments   
Comment by Githook User [ 05/Sep/19 ]

Author:

{'email': 'alice.thum@10gen.com', 'name': 'Alice Thum'}

Message: Add truncation check to DecodeValue.

GODRIVER-1233

Change-Id: Ie1b85a73ba7e0eb3b387d596d654eb4da2082cf1
Branch: release/1.1
https://github.com/mongodb/mongo-go-driver/commit/4028be8728b2b5ab8b02c9c5660eff657280f0a3

Comment by Alice Thum [ 04/Sep/19 ]

commit link: https://github.com/mongodb/mongo-go-driver/commit/deb59ac036dbdb1242eb2458f69f2ced31a328bd

Comment by Githook User [ 04/Sep/19 ]

Author:

{'email': 'alice.thum@10gen.com', 'name': 'Alice Thum'}

Message: Add truncation check to DecodeValue.

GODRIVER-1233

Change-Id: Ie1b85a73ba7e0eb3b387d596d654eb4da2082cf1
Branch: master
https://github.com/mongodb/mongo-go-driver/commit/deb59ac036dbdb1242eb2458f69f2ced31a328bd

Comment by Alice Thum [ 29/Aug/19 ]

gerrithub link: https://review.gerrithub.io/c/mongodb/mongo-go-driver/+/466782

Comment by Kristofer Brandow (Inactive) [ 13/Aug/19 ]

The issue stems from how the bsoncodec.StructCodec handles the truncation option. When we create the context to pass to a lower decoder, we don't check to see if the bsoncodec.DecodeContext had Truncate set to true. Changing this line:

dctx := DecodeContext{Registry: r.Registry, Truncate: fd.truncate}

to this:

dctx := DecodeContext{Registry: r.Registry, Truncate: fd.truncate||r.Truncate}

fixes the problem.
 

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