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.
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.
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 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.
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
c.MapType<NodaTime.LocalDate>(() => new Schema { type = "string", format = "date" });
And the code behind that is on github:
https://github.com/heldersepu/Swagger-Net-Test/blob/master/Swagger_Test/App_Start/SwaggerConfig.cs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With