micro-elements - MicroElements.Swashbuckle.NodaTime 2.0.0

Configure Asp.Net Core and swagger to use NodaTime types.

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

PM> Install-Package MicroElements.Swashbuckle.NodaTime -Version 2.0.0 -Source https://www.myget.org/F/micro-elements/api/v3/index.json

Copy to clipboard

> nuget.exe install MicroElements.Swashbuckle.NodaTime -Version 2.0.0 -Source https://www.myget.org/F/micro-elements/api/v3/index.json

Copy to clipboard

> dotnet add package MicroElements.Swashbuckle.NodaTime --version 2.0.0 --source https://www.myget.org/F/micro-elements/api/v3/index.json

Copy to clipboard
<PackageReference Include="MicroElements.Swashbuckle.NodaTime" Version="2.0.0" />
Copy to clipboard
source https://www.myget.org/F/micro-elements/api/v3/index.json

nuget MicroElements.Swashbuckle.NodaTime  ~> 2.0.0
Copy to clipboard

> choco install MicroElements.Swashbuckle.NodaTime --version 2.0.0 --source https://www.myget.org/F/micro-elements/api/v2

Copy to clipboard
Import-Module PowerShellGet
Register-PSRepository -Name "micro-elements" -SourceLocation "https://www.myget.org/F/micro-elements/api/v2"
Install-Module -Name "MicroElements.Swashbuckle.NodaTime" -RequiredVersion "2.0.0" -Repository "micro-elements" 
Copy to clipboard

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

  • Supports Swashbuckle 5, net core 3 and brand new System.Text.Json
  • Implemented in c#, no FSharp.Core lib in dependencies
  • JsonSerializerSettings ContractResolver uses for NamingStrategy, so you can use DefaultNamingStrategy, CamelCaseNamingStrategy or SnakeCaseNamingStrategy
  • Supports new DateInterval (use NodaTime.Serialization.JsonNet >= 2.1.0)

Sample

Full sample see in samples: https://github.com/micro-elements/MicroElements.Swashbuckle.NodaTime/tree/master/samples/WebApiSample

public class Startup
{
    enum JsonProvider
    {
        NewtonsoftJson,
        SystemTextJson
    }

    // JsonProvider
    private static JsonProvider useJsonProvider = JsonProvider.SystemTextJson;

    // USING NewtonsoftJson settings as PropertyNamingPolicy for System.Text.Json
    private static bool useNewtonsoftJsonAsNamingPolicy = true;

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        void ConfigureNewtonsoftJsonSerializerSettings(JsonSerializerSettings serializerSettings)
        {
            // Use DefaultContractResolver or CamelCasePropertyNamesContractResolver;
            // serializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            serializerSettings.ContractResolver = new DefaultContractResolver()
            {
                //NamingStrategy = new DefaultNamingStrategy()
                //NamingStrategy = new CamelCaseNamingStrategy()
                //NamingStrategy = new SnakeCaseNamingStrategy()
                NamingStrategy = new CamelCaseNamingStrategy()
            };

            // Configures JsonSerializer to properly serialize NodaTime types.
            serializerSettings.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
        }

        void ConfigureSystemTextJsonSerializerSettings(JsonSerializerOptions serializerOptions)
        {
            if (useNewtonsoftJsonAsNamingPolicy)
            {
                // USING NewtonsoftJson settings as PropertyNamingPolicy for System.Text.Json
                JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings();
                ConfigureNewtonsoftJsonSerializerSettings(jsonSerializerSettings);
                serializerOptions.PropertyNamingPolicy = new NewtonsoftJsonNamingPolicy(jsonSerializerSettings);
            }

            // Configures JsonSerializer to properly serialize NodaTime types.
            serializerOptions.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);

            serializerOptions.Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping;
        }

        if (useJsonProvider == JsonProvider.NewtonsoftJson)
        {
            services
                .AddMvcCore()
                .AddApiExplorer()
                .AddNewtonsoftJson(options => ConfigureNewtonsoftJsonSerializerSettings(options.SerializerSettings, shouldGenerateExamples: true));
        }

        if (useJsonProvider == JsonProvider.SystemTextJson)
        {
            services
                .AddMvcCore()
                .AddApiExplorer()
                .AddJsonOptions(options => ConfigureSystemTextJsonSerializerSettings(options.JsonSerializerOptions, shouldGenerateExamples: true))
                ;
        }

        //services.AddTransient<IConfigureOptions<JsonOptions>, ConfigureNewtonsoftJsonJsonNamingPolicy>();

        // Adds swagger
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });

            if (useJsonProvider == JsonProvider.NewtonsoftJson)
            {
                // Configures swagger to use NodaTime with serializerSettings.
                c.ConfigureForNodaTime(configureSerializerSettings: ConfigureNewtonsoftJsonSerializerSettings);
            }

            if (useJsonProvider == JsonProvider.SystemTextJson)
            {
                JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions();
                ConfigureSystemTextJsonSerializerSettings(jsonSerializerOptions);
                c.ConfigureForNodaTimeWithSystemTextJson(jsonSerializerOptions);
            }
        });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app)
    {
        app.UseSwagger();
        app.UseRouting();
        app.UseEndpoints(endpoints => { endpoints.MapControllers(); });

        // Adds swagger UI
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
        });
    }
}

public class NewtonsoftJsonNamingPolicy : JsonNamingPolicy
{
    private readonly JsonSerializerSettings _jsonSerializerSettings;

    /// <inheritdoc />
    public NewtonsoftJsonNamingPolicy(JsonSerializerSettings jsonSerializerSettings)
    {
        _jsonSerializerSettings = jsonSerializerSettings;
    }

    /// <inheritdoc />
    public override string ConvertName(string name)
    {
        var contractResolver = _jsonSerializerSettings.ContractResolver;
        return (contractResolver as DefaultContractResolver)?.GetResolvedPropertyName(name) ?? name;
    }
}

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.

2.0.0

  • Swashbuckle.AspNetCore fixed to versions [4.0.1, 5.0.0)

1.2.0

  • Swashbuckle.AspNetCore fixed to versions [2.4.0, 4.0.1)

1.1.0

  • Dependencies updated

1.0.2

  • Bugfix: Uses factory instead of shared Schema instance. See: PR#3

1.0.1

  • Updated package description

1.0.0

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

Full release notes can be found at: https://github.com/micro-elements/MicroElements.Swashbuckle.NodaTime/blob/master/CHANGELOG.md

  • .NETStandard 2.0
    • NodaTime (>= 2.4.0)
    • NodaTime.Serialization.JsonNet (>= 2.1.0)
    • Swashbuckle.AspNetCore (>= 4.0.1 && < 5.0.0)
  • .NETStandard 2.0: 2.0.0.0

Owners

Alexey Petryashev

Authors

micro-elements

Project URL

https://github.com/micro-elements/MicroElements.Swashbuckle.NodaTime

License

MIT

Tags

swagger swashbuckle NodaTime aspnetcore

Info

23 total downloads
2 downloads for version 2.0.0
Download (10.03 KB)
Found on the current feed only

Package history

Version Size Last updated Downloads Mirrored?
4.0.1 20.65 KB Thu, 21 Jan 2021 07:31:50 GMT 2
4.0.0 20.6 KB Sat, 21 Nov 2020 18:36:08 GMT 2
3.0.0 12.7 KB Sat, 29 Aug 2020 16:28:20 GMT 1
3.0.0-rc.4 12.66 KB Sat, 29 Aug 2020 16:20:16 GMT 2
3.0.0-rc.3 12.63 KB Sat, 22 Feb 2020 14:47:45 GMT 1
3.0.0-rc.2 12.02 KB Sun, 02 Feb 2020 16:09:30 GMT 1
3.0.0-rc.1 10.1 KB Thu, 14 Nov 2019 21:16:57 GMT 1
2.0.0 10.03 KB Wed, 20 Mar 2019 08:44:46 GMT 2
1.2.0 9.98 KB Wed, 20 Mar 2019 08:35:38 GMT 2
1.1.0 9.95 KB Thu, 10 Jan 2019 21:26:46 GMT 1
1.0.2 9.93 KB Thu, 27 Sep 2018 19:01:48 GMT 2
1.0.1 9.88 KB Thu, 27 Sep 2018 18:51:17 GMT 1
1.0.0 9.32 KB Fri, 17 Aug 2018 05:56:16 GMT 1
1.0.0-beta.3 9.35 KB Thu, 09 Aug 2018 21:42:56 GMT 1
1.0.0-beta.2 9.36 KB Mon, 06 Aug 2018 06:09:04 GMT 2
1.0.0-beta.1 9.13 KB Fri, 03 Aug 2018 20:37:38 GMT 1
0.1.0 4.64 KB Wed, 25 Jul 2018 20:24:40 GMT 0