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

Web Api properties of type long (Int64) are incorrectly serialized by some rounding #41463

Closed
1 task done
AndersWangRask opened this issue May 1, 2022 · 2 comments
Closed
1 task done
Labels
area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels

Comments

@AndersWangRask
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When a Web Api function returns a class with a property of type long (Int64) the value of the property is rounded. E.g. 9223372036854773406 becomes 9223372036854774000. The last four digits are changed from 3406 to 4000.

I have tested this with .Net 6 and .Net 7.

I have tested with both NewtonSoft and System.Text.Json and issue is with both. However, when I "manually" serialize with either Json framework the values are correct. This change in value seems to be something that happens in the Web Api between the function returns the value to the Web Api serializes.

Expected Behavior

The values are output correctly. I.e. value 9223372036854773406 is output as 9223372036854773406.

Steps To Reproduce

  1. With Visual Studio Create a new WebApi project with controllers using .Net 6 or .Net 7.

  2. Inside the WeatherForecastController.cs add the following code:

     public class TestModel
     {
         public long negLongId { get; set; }
         public long posLongId { get; set; }
         public int intId { get; set; }
     }
    
     [HttpGet("testids")]
     public TestModel GetTestIds()
     {
         return
             new TestModel
             {
                 negLongId = -9223372036854773407,
                 posLongId = 9223372036854773406,
                 intId = -2147483648
             };
     }
    
  3. Run the project and execute the testids. See that the returned values are incorrect as described.

image

Additionally there is a Public GitHub Repo here that shows this issue:

https://github.com/AndersWangRask/DotNetWebApiLongSerializationBug

Exceptions (if any)

No response

.NET Version

Microsoft.AspNetCore.App 7.0.0-preview.3.22178.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]

Anything else?

No response

@MariovanZeist
Copy link
Contributor

Hi @AndersWangRask

AspNet core is serializing the values correctly, the problem you see has to do with a javascript limitation, and swagger uses javascript to display the values see for example

You can see the correct values when you use the API itself instead of using Swagger to view the output

Call the API directly by calling http://localhost:{portnumber}/WeatherForecast/testids
to verify the output

@javiercn javiercn added the area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels label May 2, 2022
@AndersWangRask
Copy link
Author

Hi @MariovanZeist

Thank you very much for your message and for helping out. You are correct and I will close this issue.

Funny thing is that I DID try and test "directly", but because I have a JSON viewer extension in my Firefox browser that ALSO has this issue I didn't realise the reality of it.

Thanks a lot.

@ghost ghost locked as resolved and limited conversation to collaborators Jun 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels
Projects
None yet
Development

No branches or pull requests

3 participants