Skip to content

GODRIVER-1805 Add tests for unmarshalling BSON with undefined fields #552

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 2 commits into from
Dec 14, 2020
Merged
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
153 changes: 152 additions & 1 deletion bson/unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func TestCachingDecodersNotSharedAcrossRegistries(t *testing.T) {
}

func TestUnmarshalExtJSONWithUndefinedField(t *testing.T) {
// When unmarshalling, fields that are undefined in the destination struct are skipped.
// When unmarshalling extJSON, fields that are undefined in the destination struct are skipped.
// This process must not skip other, defined fields and must not raise errors.
type expectedResponse struct {
DefinedField string
Expand Down Expand Up @@ -278,3 +278,154 @@ func TestUnmarshalExtJSONWithUndefinedField(t *testing.T) {
})
}
}

func TestUnmarshalBSONWithUndefinedField(t *testing.T) {
// When unmarshalling BSON, fields that are undefined in the destination struct are skipped.
// This process must not skip other, defined fields and must not raise errors.
type expectedResponse struct {
DefinedField string `bson:"DefinedField"`
}

createExpectedResponse := func(t *testing.T, doc D) *expectedResponse {
t.Helper()

marshalledBSON, err := Marshal(doc)
assert.Nil(t, err, "error marshalling BSON: %v", err)

responseDoc := expectedResponse{}
err = Unmarshal(marshalledBSON, &responseDoc)
assert.Nil(t, err, "error unmarshalling BSON: %v", err)
return &responseDoc
}

testCases := []struct {
name string
testBSON D
}{
{
"no array",
D{
{"UndefinedField", D{
{"key", 1},
}},
{"DefinedField", "value"},
},
},
{
"outer array",
D{
{"UndefinedField", A{D{
{"key", 1},
}}},
{"DefinedField", "value"},
},
},
{
"embedded array",
D{
{"UndefinedField", D{
{"key", A{1}},
}},
{"DefinedField", "value"},
},
},
{
"outer array and embedded array",
D{
{"UndefinedField", A{D{
{"key", A{1}},
}}},
{"DefinedField", "value"},
},
},
{
"embedded document",
D{
{"UndefinedField", D{
{"key", D{
{"one", "two"},
}},
}},
{"DefinedField", "value"},
},
},
{
"doubly embedded document",
D{
{"UndefinedField", D{
{"key", D{
{"one", D{
{"two", "three"},
}},
}},
}},
{"DefinedField", "value"},
},
},
{
"embedded document and embedded array",
D{
{"UndefinedField", D{
{"key", D{
{"one", D{
{"two", A{3}},
}},
}},
}},
{"DefinedField", "value"},
},
},
{
"embedded document and embedded array in outer array",
D{
{"UndefinedField", A{D{
{"key", D{
{"one", A{3}},
}},
}}},
{"DefinedField", "value"},
},
},
{
"code with scope",
D{
{"UndefinedField", D{
{"logic", D{
{"$code", "foo"},
{"$scope", D{
{"bar", 1},
}},
}},
}},
{"DefinedField", "value"},
},
},
{
"embedded array of code with scope",
D{
{"UndefinedField", D{
{"logic", A{D{
{"$code", "foo"},
{"$scope", D{
{"bar", 1},
}},
}}},
}},
{"DefinedField", "value"},
},
},
{
"empty embedded document",
D{
{"UndefinedField", D{}},
{"DefinedField", "value"},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
responseDoc := createExpectedResponse(t, tc.testBSON)
assert.Equal(t, "value", responseDoc.DefinedField, "expected DefinedField to be 'value', got %q", responseDoc.DefinedField)
})
}
}