Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get Swashbuckle to produce Swagger-properties with date (without time)?

I have a class that is exposed through Swashbuckle that looks something like this:

public class Person
{
     [JsonProperty("dateOfBirth")]
     [JsonConverter(typeof(DateFormatConverter))]
     public System.DateTime? DateOfBirth { get; set; }
}

internal class DateFormatConverter : Newtonsoft.Json.Converters.IsoDateTimeConverter
{
    public DateFormatConverter()
    {
        DateTimeFormat = "yyyy-MM-dd";
    }
}

(The class was generated by NSwag-studio)

When I use app.UseSwagger(); to generate a Swagger contract with Swashbuckle, the result looks like this:

"Person": {
    "dateOfBirth": {
      "format": "date-time",
      "type": "string"
    }
  }
}

I would like to configure Swashbuckle to recognize my DateFormatConverter class and treat the format accordingly.

I tried options.SchemaFilter<DateFormatSchemaFilter>() in my Startup class, but the filter doesn't have the context of the property, so unless I want all DateTime objects to be date, this isn't a viable option.

like image 332
Johannes Brodwall Avatar asked May 13 '18 21:05

Johannes Brodwall


People also ask

How does swagger define date?

OpenAPI defines the following built-in string formats: date – full-date notation as defined by RFC 3339, section 5.6, for example, 2017-07-21. date-time – the date-time notation as defined by RFC 3339, section 5.6, for example, 2017-07-21T17:32:28Z. password – a hint to UIs to mask the input.

What is the use of swashbuckle AspNetCore?

Swashbuckle is an open source project for generating Swagger documents for Web APIs that are built with ASP.NET Core. There are three core components: AspNetCore. SwaggerGen - provides the functionality to generate JSON Swagger documents that describe the objects, methods, return types, etc.

How do I hide a schema in swagger net core?

How to do it? add this property in your Swagger UI Options defaultModelsExpandDepth: -1 for hide schema section and for more reference refer this swagger.io/docs/open-source-tools/swagger-ui/usage/… Can you please add your swagger ui configuration settings in your question.


1 Answers

Here is how you can change the "date-time" to "date" with an iDocumentFilter:

private class Flip2DateDocumentFilter : IDocumentFilter
{
    private List<string> DateTypes(Type AttribType)
    {
        var list = new List<string>();
        foreach (var p in AttribType.GetProperties())
            if (p.CustomAttributes?.Count() > 0)
                list.Add(p.Name);
        return list;
    }

    public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry s, IApiExplorer a)
    {
        var t = typeof(Person);
        if (swaggerDoc.definitions[t.Name] != null)
        {
            var dates = DateTypes(t);
            if (dates.Count() > 0)
                foreach (var p in swaggerDoc.definitions[t.Name].properties)
                    if (dates.Contains(p.Key) && p.Value.format == "date-time")
                        p.Value.format = "date";
        }
    }
}

The key in that code is the var t = typeof(Person); that is the class that will be crawled looking for dates with CustomAttributes.

And of course this code code is not meant for copy/pasting just a small example of what you can do with IDocumentFilter


Another option will be to use [NodaTime][1] and then use `NodaTime.LocalDate` to those properties that should be just dates, and in your swagger config use the MapType

c.MapType<NodaTime.LocalDate>(() => new Schema { type = "string", format = "date" });


I have both of those options working on this example:
http://swagger-net-test.azurewebsites.net/swagger/ui/index?filter=Date#/Date/Date_Post

And the code behind that is on github:
https://github.com/heldersepu/Swagger-Net-Test/blob/master/Swagger_Test/App_Start/SwaggerConfig.cs

like image 156
Helder Sepulveda Avatar answered Sep 21 '22 09:09

Helder Sepulveda