[GODRIVER-1793] For nil options, InsertMany and InsertOne have different behaviors Created: 15/Nov/20  Updated: 28/Oct/23  Resolved: 30/Nov/20

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

Type: Bug Priority: Major - P3
Reporter: 智 姜 Assignee: Kevin Albertson
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

mongoDB go driver v1.4.0


Backwards Compatibility: Minor Change

 Description   

To coll.InsertMany(ctx,document, nil), InsertMany will use default options.InsertMany()

To coll.InsertOne(ctx, document, nil), InsertOne will panic because opts is [nil], and opt.BypassDocumentValidation will panic for access nil opt.

func (coll *Collection) InsertOne(ctx context.Context, document interface{},
   opts ...*options.InsertOneOptions) (*InsertOneResult, error) {
 
   imOpts := make([]*options.InsertManyOptions, len(opts))
   for i, opt := range opts {
      imo := options.InsertMany()
      if opt.BypassDocumentValidation != nil && *opt.BypassDocumentValidation {
         imo = imo.SetBypassDocumentValidation(*opt.BypassDocumentValidation)
      }
      imOpts[i] = imo
   }



 Comments   
Comment by Githook User [ 30/Nov/20 ]

Author:

{'name': 'Kevin Albertson', 'email': 'kevin.albertson@mongodb.com', 'username': 'kevinAlbs'}

Message: GODRIVER-1793 Use merge helper and test nil opts for InsertOne (#538)
Branch: release/1.4
https://github.com/mongodb/mongo-go-driver/commit/2fbb028f3944078d9da46b681d4fcee9f9d5d2ca

Comment by Githook User [ 30/Nov/20 ]

Author:

{'name': 'jiangzhi', 'email': 'jiangzhi@qiniu.com', 'username': 'jiangz222'}

Message: GODRIVER-1793 Avoid crash when opts is [nil] in InsertOne
Branch: release/1.4
https://github.com/mongodb/mongo-go-driver/commit/3069e04546bb4ec4a2e206aa7fa995ec2f2ec64f

Comment by Githook User [ 30/Nov/20 ]

Author:

{'name': 'Kevin Albertson', 'email': 'kevin.albertson@mongodb.com', 'username': 'kevinAlbs'}

Message: GODRIVER-1793 Use merge helper and test nil opts for InsertOne (#538)
Branch: master
https://github.com/mongodb/mongo-go-driver/commit/882c8809c0b206a58e1759505e430c5241231429

Comment by Kevin Albertson [ 20/Nov/20 ]

PR for tests: https://github.com/mongodb/mongo-go-driver/pull/538

Comment by Githook User [ 20/Nov/20 ]

Author:

{'name': 'jiangzhi', 'email': 'jiangzhi@qiniu.com', 'username': 'jiangz222'}

Message: GODRIVER-1793 Avoid crash when opts is [nil] in InsertOne
Branch: master
https://github.com/mongodb/mongo-go-driver/commit/9e2aca8afd8821e6b068cc2f25192bc640d90a0d

Comment by Kevin Albertson [ 18/Nov/20 ]

Thank you for the bug report and contribution jiangz0222@gmail.com!

Comment by Divjot Arora (Inactive) [ 16/Nov/20 ]

PR: https://github.com/mongodb/mongo-go-driver/pull/536

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