-
Notifications
You must be signed in to change notification settings - Fork 284
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
Don't use integers in place of booleans in RPC #1467
Don't use integers in place of booleans in RPC #1467
Conversation
@@ -157,7 +157,7 @@ procedure TDaemonOptionsForm.btTestPortClick(Sender: TObject); | |||
if res = nil then | |||
MainForm.CheckStatus(False) | |||
else | |||
if res.Objects['arguments'].Integers['port-is-open'] <> 0 then | |||
if res.Objects['arguments'].Booleans['port-is-open'] then | |||
MessageDlg(sPortTestSuccess, mtInformation, [mbOk], 0) | |||
else | |||
MessageDlg(sPortTestFailed, mtError, [mbOK], 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on the provided code patch, here is a brief code review:
-
The change in line 157 seems to be replacing the check for an integer value with a check for a boolean value.
-
Previously, it checked if
res.Objects['arguments'].Integers['port-is-open']
is not equal to zero. Now, it checks ifres.Objects['arguments'].Booleans['port-is-open']
evaluates to true. -
The change seems fine and would work as intended based on the assumption that
res.Objects['arguments'].Booleans['port-is-open']
accurately represents whether the port is open or not.
Improvement suggestions:
-
It's generally a good practice to add comments explaining the purpose of the code, especially when making changes. Consider adding a comment near this code block explaining the purpose of the condition and what it checks for.
-
Ensure that the data types returned by
res.Objects['arguments'].Booleans['port-is-open']
andres.Objects['arguments'].Integers['port-is-open']
are consistent and match the expected values ('port-is-open' field should always return a boolean). -
Consider handling any potential exceptions or error conditions that may arise during the execution of this code block and provide appropriate error messages or logging.
Overall, without additional context or seeing the surrounding code, it is challenging to provide an in-depth review. As a best practice, ensure thorough testing to verify that the code behaves as expected in various scenarios.
PR Summary
These enhancements aim to enforce better user interface management, limiting potential mishaps caused by accidental multiple clicks, as well as ensuring a smoother user experience during the testing process. |
I can confirm that transgui is broken when used with newer transmission-daemon dev-versions, regarding functions "Move torrent data" and "Delete torrent data". After I built transgui with the file changes suggested above, transgui is able to perform these functions again as expected. |
@fredo-47 Thanks for the contribution. I will take my spare time to review them as soon as possible. |
A bot wrote:
Too bad the bot does not warn against over-commenting, so much comments that important stuff gets lost or comments that state the obvious. Only if requesting the field from the request argument had some unforeseen side-effect, a comment about that would be valid otherwise the code itself is clear enough. |
25df397
into
transmission-remote-gui:master
Can anyone tell me if the used JSON parser can handle truthy and falsy values correctly? |
It's not that simple to determine because you need to explicitly state the type you're accessing when fetching values from a parsed object. However, when you access unit TestCase1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, fpcunit, testregistry, fpjson, jsonparser;
type
JsonTruthyFalsy= class(TTestCase)
published
procedure BoolAsInteger;
procedure StringboolAsInteger;
procedure StringboolAsBool;
procedure StringAsBool;
procedure IntegerAsBool;
end;
implementation
procedure JsonTruthyFalsy.BoolAsInteger;
var
obj : TJSONObject;
begin
obj := GetJSON('{"foobar":true}') as TJSONObject;
AssertEquals(obj.Integers['foobar'], 1);
end;
procedure JsonTruthyFalsy.StringboolAsInteger;
var
obj : TJSONObject;
begin
obj := GetJSON('{"foobar":"true"}') as TJSONObject;
AssertEquals(obj.Integers['foobar'], 1);
end;
procedure JsonTruthyFalsy.StringboolAsBool;
var
obj : TJSONObject;
begin
obj := GetJSON('{"foobar":"true"}') as TJSONObject;
AssertTrue(obj.Booleans['foobar']);
end;
procedure JsonTruthyFalsy.StringAsBool;
var
obj : TJSONObject;
begin
obj := GetJSON('{"foobar":"transgui"}') as TJSONObject;
AssertTrue(obj.Booleans['foobar']);
end;
procedure JsonTruthyFalsy.IntegerAsBool;
var
obj : TJSONObject;
begin
obj := GetJSON('{"foobar":1}') as TJSONObject;
AssertTrue(obj.Booleans['foobar']);
end;
initialization
RegisterTest(JsonTruthyFalsy);
end. Out of those, This means that |
Good, this means the original Integer "getter" and comparison to 0 were actually bad programming and that it's fixed even if the actual values in the JSON are 0 or 1. |
A lot of places in transgui use integers in RPC requests where the daemon expects booleans.
A recent commit in Transmission started actually enforcing this as the JSON parsing implementation has changed.
Originally reported in lighterowl#79 and cherry-picked from lighterowl@409512f.