Easily configure Swashbuckle to generate correct documentation for NodaTime types.
NodaTime is an alternative date and time API for .NET which is often used to replace built in types for handling date and time. It can be easily configured to work nicely with Web Api using NodaTime.Serialization.JsonNet package.
Swashbuckle is a library to seamlesly add swagger generation and UI to Web Api projects.
The problem is that by default swagger generated by Swashbuckle doesn't show NodaTime types nicely as can be seen on the following picture:
Swashbuckle.NodaTime configures Swashbuckle to show NodaTime types as they will be really deserialized:
Install from NuGet: https://www.nuget.org/packages/Swashbuckle.NodaTime.
Run the following command in the Package Manager Console:
Install-Package Swashbuckle.NodaTime
Call ConfigureForNodaTime
method on swagger configuration when setting up swagger using EnableSwagger method.
httpConfiguration
.EnableSwagger(c =>
{
...
c.ConfigureForNodaTime(jsonSerializerSettings);
});
jsonSerializerSettings
should be the Json.NET settings object which is used by Web API to serialize and deserialize JSON. Typical usage looks like this:
var httpConfiguration = new HttpConfiguration();
var jsonSerializerSettings = httpConfiguration.Formatters.JsonFormatter.SerializerSettings;
// ConfigureForNodaTime is from NodaTime.Serialization.JsonNet package
// it needs to be called before configuring swagger
jsonSerializerSettings.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
ConfigureSwagger(httpConfiguration, jsonSerializerSettings);
Check folder example
for working Web Api project with everything configured.