Skip to content

Commit

Permalink
Fix for Mixed binary roundtrip issue (#4278) (#4366)
Browse files Browse the repository at this point in the history
* Fix for Mixed binary roundtrip issue (#4278)
  • Loading branch information
FFranck authored Feb 24, 2022
1 parent f23fbec commit 1ea2c5b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ x.x.x Release notes (yyyy-MM-dd)

### Fixed
* <How to hit and notice issue? what was the impact?> ([#????](https://github.com/realm/realm-js/issues/????), since v?.?.?)
* None.
* Fixed issue that could cause mangling of binary data on a roundtrip to/from the database ([#4278](https://github.com/realm/realm-js/issues/4278), since v10.1.4).

### Compatibility
* MongoDB Realm Cloud.
Expand Down
4 changes: 2 additions & 2 deletions src/js_object_accessor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,8 +487,8 @@ struct Unbox<JSEngine, Mixed> {
static Mixed call(NativeAccessor<JSEngine>* ctx, typename JSEngine::Value const& value, realm::CreatePolicy,
ObjKey)
{
return js::Value<JSEngine>::to_mixed(ctx->m_ctx, ctx->m_realm, value,
ctx->m_string_buffer); // no need to validate type for a mixed value
return js::Value<JSEngine>::to_mixed(ctx->m_ctx, ctx->m_realm, value, ctx->m_string_buffer,
ctx->m_owned_binary_data); // no need to validate type for a mixed value
}
};

Expand Down
7 changes: 4 additions & 3 deletions src/js_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ struct Value {
static OwnedBinaryData to_binary(ContextType, const ValueType&);
static bson::Bson to_bson(ContextType, ValueType);
static Mixed to_mixed(ContextType ctx, std::shared_ptr<Realm> realm, const ValueType& value,
std::string& string_buffer);
std::string& string_buffer, OwnedBinaryData& binary_buffer);

#define VALIDATED(return_t, type) \
static return_t validated_to_##type(ContextType ctx, const ValueType& value, const char* name = nullptr) \
Expand Down Expand Up @@ -875,7 +875,7 @@ inline bson::Bson Value<T>::to_bson(typename T::Context ctx, ValueType value)

template <typename T>
typename realm::Mixed Value<T>::to_mixed(ContextType ctx, std::shared_ptr<Realm> realm, const ValueType& value,
std::string& string_buffer)
std::string& string_buffer, OwnedBinaryData& binary_buffer)
{
if (is_null(ctx, value) || is_undefined(ctx, value)) {
return Mixed(realm::null());
Expand All @@ -901,7 +901,8 @@ typename realm::Mixed Value<T>::to_mixed(ContextType ctx, std::shared_ptr<Realm>
return Mixed(string_buffer);
}
else if (is_binary(ctx, value)) {
return Mixed(to_binary(ctx, value).get());
binary_buffer = std::move(to_binary(ctx, value));
return Mixed(binary_buffer.get());
}
else if (is_decimal128(ctx, value)) {
return Mixed(to_decimal128(ctx, value));
Expand Down
1 change: 1 addition & 0 deletions src/node/node_value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ template <>
inline OwnedBinaryData node::Value::to_binary(Napi::Env env, const Napi::Value& value)
{

// TODO: this pointer is no good :( It is never de-allocated
NodeBinary* node_binary = nullptr;


Expand Down
31 changes: 31 additions & 0 deletions tests/js/mixed-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,35 @@ module.exports = {

realm.close();
},

// test Mixed datatype with binary data contents
testMixedData() {
const buffer1 = new Uint8Array([1, 2, 4, 8]).buffer;
const buffer2 = new Uint8Array([255, 128, 64, 32, 16, 8]).buffer;

const MixedSchema = {
name: "MixedWithData",
properties: { value: "mixed" },
};

const realm = new Realm({ schema: [MixedSchema] });

realm.write(() => {
realm.create("MixedWithData", { value: buffer1 });
});

let mixedObjects = realm.objects("MixedWithData");
let returnedData = [...new Uint8Array(mixedObjects[0].value)];
TestCase.assertArraysEqual(returnedData, [1, 2, 4, 8]);

realm.write(() => {
mixedObjects[0].value = buffer2;
});

mixedObjects = realm.objects("MixedWithData");
returnedData = [...new Uint8Array(mixedObjects[0].value)];
TestCase.assertArraysEqual(returnedData, [255, 128, 64, 32, 16, 8]);

realm.close();
},
};

0 comments on commit 1ea2c5b

Please sign in to comment.