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

Range component model validation decimal #5132

Open
philipborg opened this issue Apr 1, 2025 · 0 comments
Open

Range component model validation decimal #5132

philipborg opened this issue Apr 1, 2025 · 0 comments

Comments

@philipborg
Copy link

Describe the bug

For the following open API specification:

"freightAmount": {
    "type": "number",
    "format": "decimal",
    "maximum": 1000000.0,
    "minimum": 0.0
},

the following C# code is generated

[Newtonsoft.Json.JsonProperty("freightAmount", Required = Newtonsoft.Json.Required.Always)]
[System.ComponentModel.DataAnnotations.Range(0.0M, 1000000.0M)]
public decimal FreightAmount { get; }

Which while it looks correct there is no decimal constructor for System.ComponentModel.DataAnnotations.Range making this impossible to compile.

Version of NSwag toolchain, computer and .NET runtime used

Present in NSwag 14.3.0.

Not present in NSwag 14.2.0.

Target framework: 9

.NET SDK: 9.0.202

Generated using NSwag.AspNetCore & NSwag.MSBuild.

To Reproduce

Enable "generateDataAnnotations": true,

Add a model with decimal data type and the range attribute to a request, like

[Range(0, 1_000_000)]
public decimal InvoiceFee { get; }

Generate OpenAPI specification for this request.
Generate C# client from the OpenAPI specification.

Expected behavior

Ideally it would know when to use integer if that was originally used. But that wouldn't support decimal digits. Thus the suggested solution is that it should result in the following C# code instead.

[Newtonsoft.Json.JsonProperty("freightAmount", Required = Newtonsoft.Json.Required.Always)]
[System.ComponentModel.DataAnnotations.Range(typeof(decimal), "0.0", "1000000.0")]
public decimal FreightAmount { get; }

It will correctly handle all decimal values. Using the double constructor is not feasible as that would introduce floating point rounding errors.

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

No branches or pull requests

1 participant