Copy of https://stackoverflow.com/questions/54656603/swagger-nswagstudio-c-required-newtonsoft-json-required-disallownull-how , which got no attention, please help.
Given: api that i have limited influence to in terms of changing, built on net core 2.2. Standart netCore swagger used. Some classes of DTO have fields in it marked with [System.ComponentModel.DataAnnotations.Required] But for some reasons (which are also discussable) some methods return objects of this classes with nulls in this fields. Annotation resulted in
"required": [
"given", - this field for example
"family",
"email",
"postCode"
],
"type": "object",
...
in swagger spec which then results in
[Newtonsoft.Json.JsonProperty("given", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
in c# generated code (i'm using nswag studio and c# client with pretty standart settings). And then when i'm trying to get list of such objects from api using generated c# client, if some of such properties have null it obviosly throws newtonsoft deserialization exception. so how we can handle that? I thought of both client side and server side solutions:
1)on server we can configure not to expose info about required to swagger spec.
2) on client we can possibly configure behavior of translating that Required block to Required = Newtonsoft.Json.Required.Default
3) forget about all that and insist so api will not return object with null vaues which properties market Required.
Is this swagger 2.0 or openapi 3.0?
@RSuter as from swagger spec: "swagger": "2.0",
To clarify the generation of this:


base.IsNullable:

The biggest problem is that Swagger 2.0 does not have a way to express nullability - so we need to use required for nullability which is technically wrong - in JSON Schema this just specifies whether the the value must be set or not...
Another option is to transform the swagger spec before generating the clients, sample here:
https://github.com/Picturepark/Picturepark.SDK.Playground/blob/master/src/AutoRestTransformer/Program.cs
As alternative, you can manually override behavior for JsonSerializerSettings in partial class method (Client). Just add this to partial class
```c#
partial void UpdateJsonSerializerSettings(JsonSerializerSettings settings)
{
settings.ContractResolver = new SafeContractResolver();
}
class SafeContractResolver : DefaultContractResolver
{
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
var jsonProp = base.CreateProperty(member, memberSerialization);
jsonProp.Required = Required.Default;
return jsonProp;
}
}
```
@RicoSuter Given that this is a limitation of Swagger 2.0, is there an official position on where the fault lies if a 2.0 API describes a property as required but then sends null for that property? Is it unequivocally a bug in the API, or is it a matter of there being no good general solution for NSwag and therefore up to the client to implement a workaround?
To shed some light on my own question, this SO Q&A suggests that an API sending a null value for a string property is wrong, _not_ because the property is required, but because null isn't a string. It sounds like Swagger 2.0 having no means to express nullability strictly means that nothing is nullable. Any way of representing nullability is just a hack, whether it's an API sending nulls, or NSwag's treatment of required. Is that correct?
Most helpful comment
As alternative, you can manually override behavior for
JsonSerializerSettingsin partial class method (Client). Just add this to partial class```c#
partial void UpdateJsonSerializerSettings(JsonSerializerSettings settings)
{
settings.ContractResolver = new SafeContractResolver();
}
class SafeContractResolver : DefaultContractResolver
{
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
var jsonProp = base.CreateProperty(member, memberSerialization);
jsonProp.Required = Required.Default;
return jsonProp;
}
}
```