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.

c# validation nullable value

See original GitHub issue

Hi, We have a swagger spec with a value that is conditional/optional i.e. can be null with some validation constraints this is presented like so:

        "totalThroughputMibps": {
          "type": "number",
          "x-nullable": true,
          "description": "Total throughput of pool in Mibps",
          "example": 164.221,
          "readOnly": true,
          "multipleOf": 0.001
        }

This causes issues with c# generated SDK where the validation code does not allow null values.

            public double? TotalThroughputMibps { get; private set; }
.
.
.
.
.
            if (TotalThroughputMibps % 0.001 != 0)
            {
                throw new ValidationException(ValidationRules.MultipleOf, "TotalThroughputMibps", 0.001);
            }

Is it possible to present this in the swagger so that the validation in the generated code handles null values correctly?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:16 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
pakrymcommented, Nov 12, 2020

As to the original issue in this thread, I believe it’s fixed as part of https://github.com/Azure/autorest.csharp/issues/885.

Try adding --use:@microsoft.azure/autorest.csharp@2.3.90 to your command line.

0reactions
audunncommented, Feb 18, 2021

@daviwil apologies for the late reply, yes indeed this does seem to fix the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use Data Annotations to validate a nullable int
Show activity on this post. In an MVC 5 project, I have a model with a nullable int. For reasons that might not...
Read more >
Required Attribute on nullable ValueTypes - MSDN - Microsoft
and when I pass this JSON value to the ApiController I get the following error: JSON Value: var jsData = { ID: null,...
Read more >
C# Futures: Simplified Parameter Null Validation - InfoQ
It is redundant with the declaration that the parameter is non-nullable. Another issue is that value! would mean either “please check this for ......
Read more >
C#: Different ways to Check for Null - Thomas Claudius Huber
The compiler explained to me that I am trying to do a null check against a non-nullable value type. By attempting this, I...
Read more >
Built-in Validators — FluentValidation documentation
Ensures that the specified property is not null, an empty string or whitespace (or the default value for value types, e.g., 0 for...
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