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

Unhandled OverflowException on decimal property with Range attribute #1365

Open
older opened this issue Jun 6, 2018 · 5 comments
Open

Unhandled OverflowException on decimal property with Range attribute #1365

older opened this issue Jun 6, 2018 · 5 comments

Comments

@older
Copy link
Contributor

older commented Jun 6, 2018

System.ComponentModel.DataAnnotations.RangeAttribute doesn't have constructor for decimal type. One of alternatives is to use constructor which accepts double values for boundaries. JSON schema generator tries to create instance of type using value provided as maximum boundary and fails if double value is greater than decimal.MaxValue with the following exception:

System.OverflowException: Value was either too large or too small for a Decimal.
  in System.Decimal..ctor(Double value)

As far as I can tell it is common practice to use Range attribute with something like double.MaxValue if you only want to have lower limit on your value. Even (double)decimal.MaxValue would fail as the value produced after cast is actually greater than decimal.MaxValue.

The issue is easy to reproduce by creating Web API method with the following response class and trying to generate swagger spec in NSwagStudio:

using System.ComponentModel.DataAnnotations;
public class ResponseDto
{
    [Range(0.0, double.MaxValue)]
    public decimal AnyPositiveDecimal { get; set; }
}
@RicoSuter
Copy link
Owner

I think we can handle double.MaxValue as no upper limit, but because min/max is modeled as decimal we cannot support big values in ranges...

We need to fix this here: https://github.com/RSuter/NJsonSchema/blob/master/src/NJsonSchema/Generation/JsonSchemaGenerator.cs#L881

@older
Copy link
Contributor Author

older commented Jun 6, 2018

Would that be possible to treat OverflowException on any of the boundary values as no limit?

@RicoSuter
Copy link
Owner

Looking at the code this should be possible, we just don't set schema.Minimum or schema.Maximum when value is exactly == double.MaxValue, etc.

@RicoSuter
Copy link
Owner

Related: RicoSuter/NJsonSchema#676

@older
Copy link
Contributor Author

older commented Dec 10, 2021

I cannot reproduce this with NSwag 13.15.1 anymore. Should I close the issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants