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

jsonrpc add id from request to response #742

Merged
merged 3 commits into from
Aug 7, 2018
Merged

jsonrpc add id from request to response #742

merged 3 commits into from
Aug 7, 2018

Conversation

wangzuo
Copy link
Contributor

@wangzuo wangzuo commented Aug 3, 2018

According to https://www.jsonrpc.org/specification:

id
This member is REQUIRED.
It MUST be the same as the value of the id member in the Request Object.
If there was an error in detecting the id in the Request object (e.g. Parse error/Invalid Request), it > MUST be Null.

This pull request add req.ID to response object.

@basvanbeek
Copy link
Member

Thanks for the PR.

Can you please add a unit test for the correct behavior?

@wangzuo
Copy link
Contributor Author

wangzuo commented Aug 3, 2018

Not sure why the tests failed (timeout)

}
}

func TestCanMarshalNullID(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

why a separate test for null? wouldn't this fit the TestCanMarshalID test (adding a case for it)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

test updated

@basvanbeek
Copy link
Member

@peterbourgon can you review? i’ll be offline the next 2 weeks.

Copy link
Member

@peterbourgon peterbourgon left a comment

Choose a reason for hiding this comment

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

With that one comment addressed, I'm +1

} else {
return json.Marshal(id.stringValue)
}
}
Copy link
Member

Choose a reason for hiding this comment

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

My sense is this would be better expressed without the short-circuit logic, as

switch {
case id.intError == nil && id.floatError != nil:
    return json.Marshal(id.intValue)
case id.intError != nil && id.floatError == nil:
    return json.Marshal(id.floatValue)
default:
    return json.Marshal(id.stringValue)
}

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I dont think so, intError and floatError can both be nil

Copy link
Member

Choose a reason for hiding this comment

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

Yes, if intErr == nil and floatErr == nil, the final default case is triggered.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For integer value, intErr and floatErr are nil and the default case will return empty string.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, OK, I understand. Then the original implementation is best.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Awesome!

@peterbourgon peterbourgon merged commit 631bc1e into go-kit:master Aug 7, 2018
@peterbourgon
Copy link
Member

Thank you!

@wangzuo wangzuo deleted the rpc-response-id-fix branch August 7, 2018 14:06
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.

3 participants