micro-elements - MicroElements.Swashbuckle.NodaTime 4.0.1
Configure Asp.Net Core and swagger to use NodaTime types.
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
- NamingStrategy support. You can use DefaultNamingStrategy, CamelCaseNamingStrategy or SnakeCaseNamingStrategy
- Supports DateInterval
- Can be generated with user provided examples or without examples
PM> Install-Package MicroElements.Swashbuckle.NodaTime -Version 4.0.1 -Source https://www.myget.org/F/micro-elements/api/v3/index.json
> nuget.exe install MicroElements.Swashbuckle.NodaTime -Version 4.0.1 -Source https://www.myget.org/F/micro-elements/api/v3/index.json
> dotnet add package MicroElements.Swashbuckle.NodaTime --version 4.0.1 --source https://www.myget.org/F/micro-elements/api/v3/index.json
source https://www.myget.org/F/micro-elements/api/v3/index.json
nuget MicroElements.Swashbuckle.NodaTime ~> 4.0.1
Copy to clipboard
> choco install MicroElements.Swashbuckle.NodaTime --version 4.0.1 --source https://www.myget.org/F/micro-elements/api/v2
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 "4.0.1" -Repository "micro-elements"
Copy to clipboard
MicroElements.Swashbuckle.NodaTime
Allows configure Asp.Net Core and swagger to use NodaTime types.
Latest Builds, Packages
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
orAddJsonOptions
- Configure
AddSwaggerGen
withConfigureForNodaTime
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
- MicroElements.Swashbuckle.NodaTime creates Schemas for all NodaTime types
- MicroElements.Swashbuckle.NodaTime configures JsonSerializer for examples
- 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.
$# 4.0.0
- NodaTime updated to 3.0.0
- NodaTime.Serialization.JsonNet updated to 3.0.0
- NodaTime.Serialization.SystemTextJson updated to 1.0.0
- Swashbuckle.AspNetCore updated to version 5.5.1
- Added SchemaExamples to NodaTimeSchemaSettings and configuration methods ConfigureForNodaTime and ConfigureForNodaTimeWithSystemTextJson to support custom example values
- Nullable annotations added
3.0.0
- Supports Swashbuckle 5, net core 3 and System.Text.Json
- Swashbuckle.AspNetCore updated to version 5.0.0
- NodaTime and NodaTime.Serialization.JsonNet updated to latest versions
- ConfigureForNodaTime became more customizable
- Compatibility with System.Text.Json:
- New dependency: NodaTime.Serialization.SystemTextJson
- Added new ConfigureForNodaTimeWithSystemTextJson
- PR #13 by jeremyhayes: remove unspecified format aliases (full-date, partial-time)
- PR #8 by Romanx: Add flag for generating examples in output
- PR #11 by dgarciarubio: Add support for OffsetDate and OffsetTime types
3.0.0-rc.4
- PR #8 by Romanx: Add flag for generating examples in output
- PR #11 by dgarciarubio: Add support for OffsetDate and OffsetTime types
3.0.0-rc.3
- Supports Swashbuckle 5, net core 3 and System.Text.Json
- Swashbuckle.AspNetCore updated to version 5.0.0
3.0.0-rc.2
- Supports net core 3 and brand new System.Text.Json
- NodaTime and NodaTime.Serialization.JsonNet updated to latest versions
- Swashbuckle.AspNetCore updated to version 5.0.0-rc5
- ConfigureForNodaTime became more customizable
- Compatibility with System.Text.Json:
- New dependency: NodaTime.Serialization.SystemTextJson
- Added new ConfigureForNodaTimeWithSystemTextJson
- Sample moved to net core 3
- Sample supports NewtonsoftJson, System.Text.Json and System.Text.Json with NamingPolicy from NewtonsoftJson
Full release notes can be found at: https://github.com/micro-elements/MicroElements.Swashbuckle.NodaTime.git/blob/master/CHANGELOG.md
-
.NETStandard 2.0
- NodaTime (>= 3.0.0)
- NodaTime.Serialization.JsonNet (>= 3.0.0)
- NodaTime.Serialization.SystemTextJson (>= 1.0.0)
- Swashbuckle.AspNetCore (>= 5.5.1)
- .NETStandard 2.0: 2.0.0.0
OwnersAlexey Petryashev |
Authorsmicro-elements |
Project URLhttps://github.com/micro-elements/MicroElements.Swashbuckle.NodaTime |
LicenseMIT |
Tagsswagger swashbuckle NodaTime aspnetcore |
Info17 total downloads |
1 downloads for version 4.0.1 |
Download (20.65 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 | 1 | ||||
4.0.0 | 20.6 KB | Sat, 21 Nov 2020 18:36:08 GMT | 1 | ||||
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 | 1 | ||||
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 | 1 | ||||
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 | 1 | ||||
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 | 1 | ||||
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 |