Skip to content

GODRIVER-2133 Error on semantic single documents as aggregate pipelines #721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,17 @@ func transformAggregatePipeline(registry *bsoncodec.Registry, pipeline interface
aidx, arr := bsoncore.AppendArrayStart(nil)
var hasOutputStage bool
valLen := val.Len()

// Explicitly forbid non-empty pipelines that are semantically single documents
// and are implemented as slices.
switch t := pipeline.(type) {
case bson.D, bson.Raw, bsoncore.Document:
if valLen > 0 {
return nil, false,
fmt.Errorf("%T is not an allowed pipeline type as it represents a single document. Use bson.A or mongo.Pipeline instead", t)
}
}

for idx := 0; idx < valLen; idx++ {
doc, err := transformBsoncoreDocument(registry, val.Index(idx).Interface(), true, fmt.Sprintf("pipeline stage :%v", idx))
if err != nil {
Expand Down
46 changes: 46 additions & 0 deletions mongo/mongo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ func TestMongoHelpers(t *testing.T) {
arr, _ = bsoncore.AppendDocumentEnd(arr, dindex)
arr, _ = bsoncore.AppendArrayEnd(arr, index)

index, doc := bsoncore.AppendDocumentStart(nil)
doc = bsoncore.AppendInt32Element(doc, "x", 1)
doc, _ = bsoncore.AppendDocumentEnd(doc, index)

testCases := []struct {
name string
pipeline interface{}
Expand Down Expand Up @@ -342,6 +346,48 @@ func TestMongoHelpers(t *testing.T) {
true,
nil,
},
{
"semantic single document/bson.D",
bson.D{{"x", 1}},
nil,
false,
errors.New("primitive.D is not an allowed pipeline type as it represents a single document. Use bson.A or mongo.Pipeline instead"),
},
{
"semantic single document/bson.Raw",
bson.Raw(doc),
nil,
false,
errors.New("bson.Raw is not an allowed pipeline type as it represents a single document. Use bson.A or mongo.Pipeline instead"),
},
{
"semantic single document/bsoncore.Document",
bsoncore.Document(doc),
nil,
false,
errors.New("bsoncore.Document is not an allowed pipeline type as it represents a single document. Use bson.A or mongo.Pipeline instead"),
},
{
"semantic single document/empty bson.D",
bson.D{},
bson.A{},
false,
nil,
},
{
"semantic single document/empty bson.Raw",
bson.Raw{},
bson.A{},
false,
nil,
},
{
"semantic single document/empty bsoncore.Document",
bsoncore.Document{},
bson.A{},
false,
nil,
},
}

for _, tc := range testCases {
Expand Down