question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unhandled OverflowException on decimal property with Range attribute

See original GitHub issue

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; }
}

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
oldercommented, Dec 10, 2021

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

0reactions
RicoSutercommented, Jun 13, 2018
Read more comments on GitHub >

github_iconTop Results From Across the Web

C#: DataAnnotation for Range does not work with leading ' ...
I have a set of nullable decimal properties that are decorated with a Range validation attribute with values -40.00 to +40.00 but when...
Read more >
FIX: "System.OverflowException: Value was either too large ...
This issue occurs when a column of a group expression contains a value that is not in the range of the "Int32" data...
Read more >
high-precision calculation in C# with Decimal
C# Decimal tutorial shows how to perform high-precision calculation in C# with Decimal. Decimal type represents a decimal floating-point number.
Read more >
Decimal exception handling
This exception may be caused when nonzero digits are lost because the destination field in a decimal operation is too short to contain...
Read more >
IEEE 754
The standard defines: arithmetic formats: sets of binary and decimal floating-point data, which consist of finite numbers (including signed zeros and subnormal ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found