简体   繁体   中英

Swashbuckle: Make non-nullable properties required in array type

I'm using autoRest to generate a new client from a swagger schema. I have list of DateTime in a model

public class DateRange
{
   public IList<DateTime> Dates{ get; set; }
}

This the Json swagger schema it is generated from that property

  { ...
    "Dates": {
              "type": "array",
              "items": {
                "format": "date-time",
                "type": "string"
              }
            }
    ...
    }

This is the result I am getting after I run autoRest

public class DateRange
{

     [JsonProperty(PropertyName = "Dates")]
     public IList<System.DateTime?> Dates{ get; set; }
}

I would like to get a non nullable dateTime property something like this

public IList<System.DateTime> Dates{ get; set; }

Update your Swagger schema so that your property would look like:

dates:
    type: "array"
    items:
        format: "date-time"
        type: "string"
        x-nullable: false

Then generated the client using AutoRest in the command line:

autorest --input-file="swagger.json" --output-folder="output" --csharp

Resulting in:

/// <summary>
/// </summary>
[JsonProperty(PropertyName = "dates")]
public IList<System.DateTime> Dates { get; set; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM