strongloop / strong-remoting

Communicate between objects in servers, mobile apps, and other servers.
www.strongloop.com
Other
105 stars 93 forks source link

coerce json type to change will cause bug in iOS project! #201

Closed tonnylitao closed 8 years ago

tonnylitao commented 9 years ago

json data in http post body will lost it's type. And will cause a crash bug in iOS project if got unexpected type.

such as

{
    "name": "1"
}

it will become

{
    "name": 1
}

why force change type to int? Is it a bug?

function coerce(str) {
    if (typeof str !== 'string') return str;
    if ('null' === str) return null;
    if ('true' === str) return true;
    if ('false' === str) return false;
    if (isfloat.test(str)) return parseFloat(str, 10);
    if (isint.test(str) && str.charAt(0) !== '0') return parseInt(str, 10);   //why need this?
    return str;
}
bajtos commented 9 years ago

@TonnyTao thank you for reporting this issue. Can you please provide us with a simple application to reproduce this problem, see Reporting issues?

why force change type to int? Is it a bug?

See #168 which introduced this change, and the issues #143 and #167 it was fixing.

I think this may be a bug in the iOS SDK if it does not correctly support numeric ids. @hideya could you please check?

hideya commented 9 years ago

@TonnyTao would you please check the type of the property with which you are having the trouble?

I guess it is set to any. My understanding is that the coercion code runs only for untyped arg. If it were any, would you check what Obj-C type do you use for the property? It needs to be id, and the code needs to be prepared for handling any type including NSString and NSNumber.

I checked iOS SDK's behavior against any, and found it is a bit tricky. Say, a model with a property of any type is defined on the server side, and on the iOS app side, I declare the property type is NSString. Then if I set a string like @"123" to the property in the iOS app, save the model to and retrieve it from the server, then the property holds an object of type NSNumber (@123) regardless of the Obj-C type declaration. I guess this might be something happening in your app. If it is the case, I'd say it is not a bug but an issue cause by the type mismatch (please consider to specify string explicitly on the server side). Does this make sense?

richardpringle commented 8 years ago

@TonnyTao, I see that this issue is pretty old so I will close it. If you are still having issues, please re-open and mention me directly and I will see what I can do!