domaindrivendev / Swashbuckle.AspNetCore

Swagger tools for documenting API's built on ASP.NET Core
MIT License
5.26k stars 1.32k forks source link

[Bug]: Incorrect maximum value in open api document for RangeAttribute with long.MaxValue #3159

Open ArieGato opened 6 days ago

ArieGato commented 6 days ago

Describe the bug

The maximum value of a property of type long is generated incorrectly. The issue occurs when the property is decorated with a custom range attribute derived from RangeAttribute. The generated value is 9223372036854776000, but should be 9223372036854775807.

Expected behavior

The expected behavior is that the generated max value is the correct max value of a long data type: 9223372036854775807

Actual behavior

The generated value is 9223372036854776000

Steps to reproduce

  1. Open repro: https://github.com/ArieGato/Swashbuckle.AspNetCore
  2. Start web site test/Basic
  3. Under DataAnnotation section, notice that the example value contains the incorrect max value : the body creditCard -> Id

Exception(s) (if any)

No response

Swashbuckle.AspNetCore version

7.0.0

.NET Version

any

Anything else?

The issue was previously reported here: 1038

martincostello commented 6 days ago

At a glance without looking at the repro, there's a chance this is related to serializer behaviours and JavaScript/JSON precision of large values in browsers. I've seen similar issues raised against ASP.NET Core itself with the same wrong number that were related to that.

If you want to check yourself to rule that out that would be useful, otherwise I'll look into it at some point in the near future.

ArieGato commented 6 days ago

Hi @martincostello

I can concur that the schema is generated correctly in code:

image

The open api document however contains this:

"id": {
  "maximum": 9223372036854776000,
  "minimum": 0,
  "type": "integer",
  "format": "int64"
},
martincostello commented 6 days ago

The value's type is a decimal rather than a long in the screenshot. This suggests to me it's a rounding issue in the serialisation, and that's handled in the Microsoft.OpenApi library, rather than by Swashbuckle itself.

ArieGato commented 6 days ago

The issue is getting harder to grasp for me. I dug a little deeper and checked the output that was written to the HttpResponse. The open api json that is returned is in fact correct. All the time I was debugging in Edge.

image

In Chrome the json is shown without some extra Microsoft magic.

image

However... The Swagger UI also doesn't render the correct maximum value. So I guess it ends here.

Any suggestions on how to go forwards?

martincostello commented 6 days ago

I think as I mentioned in my first comment, you're hitting issues with Javascript's precision when it comes to dealing with very large numbers. I'm not aware of a way to deal with it.

martincostello commented 6 days ago

https://github.com/dotnet/aspnetcore/issues/55571#issuecomment-2418055907