Skip to content

Commit 6ef364f

Browse files
committed
Throw an error if IndexModel Keys is nil
GODRIVER-456 Change-Id: I89843f6fcbfd23bffa46cef6c7abe3949ccc1c4c
1 parent 2909a4f commit 6ef364f

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

mongo/index_view.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ func (iv IndexView) CreateMany(ctx context.Context, opts []option.CreateIndexesO
5656
indexes := bson.NewArray()
5757

5858
for _, model := range models {
59+
if model.Keys == nil {
60+
return nil, fmt.Errorf("index model keys cannot be nil")
61+
}
62+
5963
name, err := getOrGenerateIndexName(model)
6064
if err != nil {
6165
return nil, err

mongo/index_view_internal_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,25 @@ func TestIndexView_CreateOneWithCollationOption(t *testing.T) {
245245
require.NoError(t, err)
246246
}
247247

248+
func TestIndexView_CreateOneWithNilKeys(t *testing.T) {
249+
t.Parallel()
250+
251+
if testing.Short() {
252+
t.Skip()
253+
}
254+
255+
_, coll := getIndexableCollection(t)
256+
indexView := coll.Indexes()
257+
258+
_, err := indexView.CreateOne(
259+
context.Background(),
260+
IndexModel{
261+
Keys: nil,
262+
},
263+
)
264+
require.Error(t, err)
265+
}
266+
248267
func TestIndexView_CreateMany(t *testing.T) {
249268
t.Parallel()
250269

0 commit comments

Comments
 (0)