Allows configure Asp.Net Core and swagger to use NodaTime types.
dotnet add package microelements.swashbuckle.nodatime
- Add package reference to MicroElements.Swashbuckle.NodaTime
- Configure asp net core to use swagger
- Configure JsonSerializer to properly serialize NodaTime types. see
AddJsonFormatters
orAddJsonOptions
- Configure
AddSwaggerGen
withConfigureForNodaTime
- Implemented in c#, no FSharp.Core lib in dependencies
- JsonSerializerSettings ContractResolver uses for NamingStrategy, so you can use DefaultNamingStrategy, CamelCaseNamingStrategy or SnakeCaseNamingStrategy
- Added new DateInterval (use NodaTime.Serialization.JsonNet >= 2.1.0)
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
void InitJsonSettings(JsonSerializerSettings serializerSettings)
{
// Use DefaultContractResolver or CamelCasePropertyNamesContractResolver;
serializerSettings.ContractResolver = new DefaultContractResolver();
// Configure JsonSerializer to properly serialize NodaTime types.
serializerSettings.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
}
// CASE1: AddMvcCore with AddJsonFormatters
services
.AddMvcCore()
.AddApiExplorer()
.AddJsonFormatters(InitJsonSettings)
;
// CASE2: AddMvc with AddJsonOptions
//services
// .AddMvc()
// .AddJsonOptions(options => InitJsonSettings(options.SerializerSettings))
// ;
// Adds swagger
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
// Configures swagger to use NodaTime.
// c.ConfigureForNodaTime();
// Configures swagger to use NodaTime. Use the same InitJsonSettings action that in AddJsonFormatters
c.ConfigureForNodaTime(InitJsonSettings);
// Configures swagger to use NodaTime with serializerSettings.
// c.ConfigureForNodaTime(serializerSettings);
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseMvc().UseSwagger();
// Adds swagger UI
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});
}
}
- MicroElements.Swashbuckle.NodaTime creates Schemas for all NodaTime types
- MicroElements.Swashbuckle.NodaTime configures JsonSerializer for examples
- Maps types to ISO 8601
Windows: Run build.ps1
Linux: Run build.sh
This project is licensed under the MIT license. See the LICENSE file for more info.