swagger-api / swagger-api/swagger-ui

Nswag studio annotates Nullable C# properties with Newtonsoft.Json.Required.DisallowNull

Open
#8,345 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
29k
Forks
9.3k
Avg merge
2d 23h
Merged PRs (30d)
25

Description

Hi,

I'm sure, there are similar issues reported before, but those are either still open, or no satisfactory solution hasn't been provided, yet.

I have tried making the property nullable in different ways - Please see the example below:

namespace ResponseModel
{
public class Child
{
[AllowNull]
public Sex? Male { get; set; }
[AllowNull]
public Sex? Female { get; set; }
}
public class Sex
{
public Height[] Height { get; set; }
public object Weight { get; set; }
public object Ofc { get; set; }
public object Bmi { get; set; }
}
//class
{
//properties
}
//class
{
//properties
}
}

Nevertheless, when I use NswagStudio to generate C# code at my client application corresponding to the API response (deserialised using the above RespnseModel), it generates the "Child" class properties as follows:

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.18.2.0 (NJsonSchema v10.8.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class Child
{
[Newtonsoft.Json.JsonProperty("id", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public Sex Male { get; set; }
[Newtonsoft.Json.JsonProperty("id", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public Sex Female { get; set; }
}

My client-side logic expects null values, and the above class throws a "null value not allowed" exception when I try to deserialize the API response at the client app using Nswag generated method.

I've tried checking the NswagStudio settings to "Generate Nullable Properties/default values", but it makes no difference. Having said that, similar (nullable )properties of some other classes are generated with "Required.Default". It seems to me rather random.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No repository file or test is identified. Reproduce the behavior in NSwag Studio with the supplied Child and Sex models, compare generated Newtonsoft.Json Required values with cases that produce Required.Default, and consider the work complete when nullable properties accept null during client deserialization.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.