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

Fix number precision for Newtonsoft.Json #2843

Merged
merged 3 commits into from
Jan 7, 2022
Merged

Fix number precision for Newtonsoft.Json #2843

merged 3 commits into from
Jan 7, 2022

Conversation

sungam3r
Copy link
Member

@sungam3r sungam3r commented Jan 6, 2022

First commit demonstrates issue with Newtonsoft.

Inspired by changes in tests from #2841.

@sungam3r sungam3r requested a review from Shane32 January 6, 2022 21:05
@sungam3r sungam3r self-assigned this Jan 6, 2022
@sungam3r sungam3r added the bugfix Pull request that fixes a bug label Jan 6, 2022
@sungam3r sungam3r added this to the 4.8 milestone Jan 6, 2022
@github-actions github-actions bot added the test Pull request that adds new or changes existing tests label Jan 6, 2022
@Shane32
Copy link
Member

Shane32 commented Jan 6, 2022

I do not understand. Can you explain what this PR is fixing?

@sungam3r
Copy link
Member Author

sungam3r commented Jan 6, 2022

Newtonsoft.Json always adds .0 when serializes float, double or decimal values. First commit shows it. I changed tests to always execute both serializers. Before changes tests used only STJ serializer.

@Shane32
Copy link
Member

Shane32 commented Jan 7, 2022

Newtonsoft.Json always adds .0 when serializes float, double or decimal values. First commit shows it. I changed tests to always execute both serializers. Before changes tests used only STJ serializer.

And why is that an issue? It’s not wrong. There looks to be a lot of code that now has to execute for every single floating point number serialized. Seems like a complete waste of CPU power. There’s no reason why the serializers cannot perform differently so long as they meet spec.

We could add the converter to execute only during tests if we need to unify the testing. But it would be a shame not to test the default environment.

I just don’t see that anyone would request this change. Except us, and only because of our tests that cover two serialization engines. (User code uses one serializer or the other.) So, it does not seem an appropriate change to make.

Now personally this doesn’t affect me because I don’t use Newtonsoft.Json. But if I did I wouldn’t want this extra code added for no reason (as seen from the users perspective).

This code hurt anything except performance. So, we could add this code. But why injure performance for the sake of a few tests?

@sungam3r
Copy link
Member Author

sungam3r commented Jan 7, 2022

And why is that an issue?
I just don’t see that anyone would request this change

JamesNK/Newtonsoft.Json#1726 reasoning and reactions here

@Shane32
Copy link
Member

Shane32 commented Jan 7, 2022

I read through that. It seems people have issues with large integer decimal values. Let’s at least restrict this converter to the decimal data type and not float or double. Since the default deserialization mode for numbers with a period is double, none of these issues would affect them.

throw new NotSupportedException();
}

public override bool CanConvert(Type objType) => objType == typeof(decimal) || objType == typeof(float) || objType == typeof(double);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
public override bool CanConvert(Type objType) => objType == typeof(decimal) || objType == typeof(float) || objType == typeof(double);
public override bool CanConvert(Type objType) => objType == typeof(decimal);

@github-actions github-actions bot added the documentation An issue or pull request regarding documentation improvements label Jan 7, 2022
@sungam3r
Copy link
Member Author

sungam3r commented Jan 7, 2022

Ready for review.

@codecov-commenter
Copy link

Codecov Report

Merging #2843 (6e8dc57) into master (951753a) will increase coverage by 0.11%.
The diff coverage is 83.33%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2843      +/-   ##
==========================================
+ Coverage   83.67%   83.78%   +0.11%     
==========================================
  Files         363      364       +1     
  Lines       14924    14978      +54     
  Branches     2361     2369       +8     
==========================================
+ Hits        12487    12549      +62     
+ Misses       1808     1798      -10     
- Partials      629      631       +2     
Impacted Files Coverage Δ
...rc/GraphQL.NewtonsoftJson/FixPrecisionConverter.cs 83.33% <83.33%> (ø)
src/GraphQL.NewtonsoftJson/DocumentWriter.cs 62.12% <0.00%> (-1.52%) ⬇️
src/GraphQL/Types/Schema.cs 79.72% <0.00%> (-0.91%) ⬇️
...hQL.NewtonsoftJson/ExecutionResultJsonConverter.cs 88.28% <0.00%> (+15.62%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 951753a...6e8dc57. Read the comment docs.

Copy link
Member

@Shane32 Shane32 left a comment

Choose a reason for hiding this comment

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

Nice

@sungam3r sungam3r merged commit 157074f into master Jan 7, 2022
@sungam3r sungam3r deleted the fix-precision branch January 7, 2022 16:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix Pull request that fixes a bug documentation An issue or pull request regarding documentation improvements test Pull request that adds new or changes existing tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants