Fix for returning a scalar EJSON type from a method #731

Tests for returning a scalar EJSON type from a method
This commit is contained in:
Naomi Seyfer
2013-02-28 11:01:09 -08:00
parent 58f243869c
commit 88f2969fa2
4 changed files with 22 additions and 6 deletions

View File

@@ -104,7 +104,10 @@ EJSON._isCustomType = function (obj) {
var adjustTypesToJSONValue =
EJSON._adjustTypesToJSONValue = function (obj) {
if (obj === null)
return;
return null;
var maybeChanged = toJSONValueHelper(obj);
if (maybeChanged !== undefined)
return maybeChanged;
_.each(obj, function (value, key) {
if (typeof value !== 'object' && value !== undefined)
return; // continue
@@ -117,6 +120,7 @@ EJSON._adjustTypesToJSONValue = function (obj) {
// at this level. recurse.
adjustTypesToJSONValue(value);
});
return obj;
};
// Either return the JSON-compatible version of the argument, or undefined (if
@@ -142,11 +146,16 @@ EJSON.toJSONValue = function (item) {
return item;
};
//for both arrays and objects
//for both arrays and objects. Tries its best to just
// use the object you hand it, but may return something
// different if the object you hand it itself needs changing.
var adjustTypesFromJSONValue =
EJSON._adjustTypesFromJSONValue = function (obj) {
if (obj === null)
return;
return null;
var maybeChanged = fromJSONValueHelper(obj);
if (maybeChanged !== obj)
return maybeChanged;
_.each(obj, function (value, key) {
if (typeof value === 'object') {
var changed = fromJSONValueHelper(value);
@@ -159,6 +168,7 @@ EJSON._adjustTypesFromJSONValue = function (obj) {
adjustTypesFromJSONValue(value);
}
});
return obj;
};
// Either return the argument changed to have the non-json

View File

@@ -79,7 +79,7 @@ Meteor._parseDDP = function (stringMessage) {
_.each(['fields', 'params', 'result'], function (field) {
if (_.has(msg, field))
EJSON._adjustTypesFromJSONValue(msg[field]);
msg[field] = EJSON._adjustTypesFromJSONValue(msg[field]);
});
return msg;
@@ -105,7 +105,7 @@ Meteor._stringifyDDP = function (msg) {
// adjust types to basic
_.each(['fields', 'params', 'result'], function (field) {
if (_.has(copy, field))
EJSON._adjustTypesToJSONValue(copy[field]);
copy[field] = EJSON._adjustTypesToJSONValue(copy[field]);
});
if (msg.id && typeof msg.id !== 'string') {
throw new Error("Message id is not a string");

View File

@@ -5,6 +5,9 @@ Meteor.methods({
echo: function (/* arguments */) {
return _.toArray(arguments);
},
echoOne: function (/*arguments*/) {
return arguments[0];
},
exception: function (where, intended) {
var shouldThrow =
(Meteor.isServer && where === "server") ||

View File

@@ -57,12 +57,15 @@ Tinytest.add("livedata - methods with colliding names", function (test) {
var echoTest = function (item) {
return function (test, expect) {
if (Meteor.isServer)
if (Meteor.isServer) {
test.equal(Meteor.call("echo", item), [item]);
test.equal(Meteor.call("echoOne", item), item);
}
if (Meteor.isClient)
test.equal(Meteor.call("echo", item), undefined);
test.equal(Meteor.call("echo", item, expect(undefined, [item])), undefined);
test.equal(Meteor.call("echoOne", item, expect(undefined, item)), undefined);
};
};