-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Nswag, c# Required = Newtonsoft.Json.Required.DisallowNull, how to handle #1991
Comments
Is this swagger 2.0 or openapi 3.0? |
@RSuter as from swagger spec: "swagger": "2.0", |
Another option is to transform the swagger spec before generating the clients, sample here: |
As alternative, you can manually override behavior for 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 |
To shed some light on my own question, this SO Q&A suggests that an API sending a |
This answer helped me to create a more nuanced approach to intelligently adjust the
With the introduction of C# 8 we also need to account for nullable reference types. This handy I hope this helps the next person who comes here looking for a good workaround. |
For someone who isn't using the JsonConvert himself. You can set the default DataContractSerializer to the Safe one:
|
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.
on client we can possibly configure behavior of translating that Required block to Required = Newtonsoft.Json.Required.Default
forget about all that and insist so api will not return object with null vaues which properties market Required.
The text was updated successfully, but these errors were encountered: