Skip to content

FDUdannychen/MicroElements.Swashbuckle.NodaTime

 
 

Repository files navigation

MicroElements.Swashbuckle.NodaTime

Allows configure Asp.Net Core and swagger to use NodaTime types.

Latest Builds, Packages

License NuGet NuGet MyGet

Travis AppVeyor Coverage Status

Gitter

Installation

Package Reference:

dotnet add package microelements.swashbuckle.nodatime

Getting started

  • Add package reference to MicroElements.Swashbuckle.NodaTime
  • Configure asp net core to use swagger
  • Configure JsonSerializer to properly serialize NodaTime types. see AddJsonFormatters or AddJsonOptions
  • Configure AddSwaggerGen with ConfigureForNodaTime

Benefits of MicroElements.Swashbuckle.NodaTime

  • 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)

Sample

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");
        });
    }
}

How it works

  1. MicroElements.Swashbuckle.NodaTime creates Schemas for all NodaTime types
  2. MicroElements.Swashbuckle.NodaTime configures JsonSerializer for examples
  3. Maps types to ISO 8601

Screenshots

Without MicroElements.Swashbuckle.NodaTime

With MicroElements.Swashbuckle.NodaTime

With MicroElements.Swashbuckle.NodaTime (camelCase)

Build

Windows: Run build.ps1

Linux: Run build.sh

License

This project is licensed under the MIT license. See the LICENSE file for more info.

About

Configure swagger to use NodaTime

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 72.3%
  • PowerShell 14.1%
  • Shell 13.6%