Skip to content
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

allow ids as string types #17

Merged
merged 2 commits into from
May 4, 2015
Merged

allow ids as string types #17

merged 2 commits into from
May 4, 2015

Conversation

bencxr
Copy link

@bencxr bencxr commented Feb 18, 2015

according to the json-rpc specification at http://www.jsonrpc.org/specification:

id
An identifier established by the Client that MUST contain a String, Number, or NULL value if included. If it is not included it is assumed to be a notification. The value SHOULD normally not be Null [1] and Numbers SHOULD NOT contain fractional parts [2]

this fix makes it possible to use a string as an id

* @param {Object} request
* @return {Boolean}
*/
hasId : function (request){
return request && typeof request['id'] !== 'undefined' && /^\-?\d+$/.test(request['id']);
return request && typeof request['id'] !== 'undefined' &&
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's missing checking against null (the typeof !== 'undefined' includes null, and all other types), so I think in the end it would be

return request && typeof request['id'] !== 'undefined' && (typeof(request['id']) === 'number' && /^\-?\d+$/.test(request['id']) || typeof(request['id']) === 'string' || request['id'] === null);

Please notice that your typeof in string check is wrong. It's checking for the boolean if typeof(request['id'] === 'string') which will be always false, and it will always return truthy

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch on the string. fixed it.

@pocesar
Copy link
Owner

pocesar commented Feb 18, 2015

Although the current implementation does permit strings, numbers and nulls, this change might be a breaking change, since people usually don't read the specifications before setting their projects / calls up. the in and out of JSON payload from different languages might return numbers for IDs that look like string but aren't, like octal and scientific notation strings, like -1.3e in form of a string, that may be or not be treated as a number.

@bencxr
Copy link
Author

bencxr commented Feb 18, 2015

as to your point of a breaking change, if they give input as a string, it will still return true (plus that's not valid spec).

if we believe it is a large enough breaking concern, then we should rev the version accordingly.

@bencxr
Copy link
Author

bencxr commented Mar 11, 2015

ping on this - are we going to fix it?

@pocesar pocesar merged commit 6b07599 into pocesar:master May 4, 2015
@pocesar
Copy link
Owner

pocesar commented May 4, 2015

merged and bumped to 1.0, there was another PR that was doing the same thing in a different part of the code, so merged both and updated the dependencies

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants