9

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.

shadowspawn
  • 3,039
  • 22
  • 26
Andrey Stepanov
  • 311
  • 2
  • 11

0 Answers0