Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactored nodatime extensions slightly and added test ensuring that the functionality is correct #345

Merged
merged 2 commits into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.3" />
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="5.0.3" />
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Abstractions" Version="5.0.0" />
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="5.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" />
Expand Down
3 changes: 3 additions & 0 deletions Directory.Packages.support.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
<Project>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netstandard2.1' or '$(TargetFramework)' == 'netcoreapp3.1'">
<PackageVersion Update="Microsoft.AspNetCore.Components.Web" Version="3.1.8" />
<PackageVersion Update="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.12" />
<PackageVersion Update="Microsoft.EntityFrameworkCore" Version="3.1.12" />
<PackageVersion Update="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.8" />
<PackageVersion Update="Microsoft.AspNetCore.Mvc.Testing" Version="3.1.8" />
<PackageVersion Update="System.Net.Http.Json" Version="3.2.1" />
<PackageVersion Update="Microsoft.Extensions.Configuration" Version="3.1.12" />
<PackageVersion Update="Microsoft.Extensions.Configuration.Abstractions" Version="3.1.12" />
<PackageVersion Update="Microsoft.Extensions.Hosting" Version="3.1.12" />
<PackageVersion Update="Microsoft.Extensions.Logging" Version="3.1.12" />
<PackageVersion Update="Microsoft.Extensions.DependencyInjection" Version="3.1.12" />
Expand Down
19 changes: 13 additions & 6 deletions sample/Sample.Core/DataConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using IConventionContext = Rocket.Surgery.Conventions.IConventionContext;

[assembly: Convention(typeof(DataConvention))]

namespace Sample.Core
{
class DataConvention : IServiceConvention
Expand All @@ -18,12 +19,18 @@ public void Register(IConventionContext context, IConfiguration configuration, I
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
services.AddPooledDbContextFactory<RocketDbContext>(x => x
.EnableDetailedErrors()
.EnableSensitiveDataLogging()
.EnableServiceProviderCaching()
.UseSqlite(connection)
);
services
#if NETSTANDARD
.AddDbContextPool<RocketDbContext>(
#else
.AddPooledDbContextFactory<RocketDbContext>(
#endif
x => x
.EnableDetailedErrors()
.EnableSensitiveDataLogging()
.EnableServiceProviderCaching()
.UseSqlite(connection)
);
}
}
}
2 changes: 1 addition & 1 deletion sample/Sample.Core/Domain/RocketDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Sample.Core.Domain
{
public class RocketDbContext : LpContext<RocketDbContext>
{
public RocketDbContext(DbContextOptions<RocketDbContext> options) : base(options) { }
public RocketDbContext(DbContextOptions<RocketDbContext>? options = null) : base(options ?? new DbContextOptions<RocketDbContext>()) { }
public DbSet<ReadyRocket> Rockets { get; set; } = null!;
public DbSet<LaunchRecord> LaunchRecords { get; set; } = null!;
}
Expand Down
2 changes: 1 addition & 1 deletion sample/Sample.Graphql/Sample.Graphql.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net5.0</TargetFrameworks>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
111 changes: 8 additions & 103 deletions src/AspNetCore.NewtonsoftJson/Conventions/NewtonsoftJsonConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Rocket.Surgery.Conventions;
using Rocket.Surgery.Conventions.DependencyInjection;
using Rocket.Surgery.LaunchPad.AspNetCore.NewtonsoftJson.Conventions;
using Rocket.Surgery.LaunchPad.Foundation;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -28,6 +29,12 @@ namespace Rocket.Surgery.LaunchPad.AspNetCore.NewtonsoftJson.Conventions
[PublicAPI]
public class NewtonsoftJsonConvention : IServiceConvention
{
private readonly FoundationOptions _options;

public NewtonsoftJsonConvention(FoundationOptions? options = null)
{
_options = options ?? new();
}
/// <summary>
/// Registers the specified context.
/// </summary>
Expand All @@ -45,111 +52,9 @@ public void Register(IConventionContext context, IConfiguration configuration, I
services.Configure<MvcNewtonsoftJsonOptions>(
options =>
{
options.SerializerSettings.Converters.Add(new StringEnumConverter(new CamelCaseNamingStrategy()));
options.SerializerSettings.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
ReplaceConverter(
options.SerializerSettings.Converters,
new CompositeNodaPatternConverter<Instant>(
InstantPattern.ExtendedIso,
InstantPattern.General,
new DateTimeOffsetPattern(),
new DateTimePattern()
)
);
ReplaceConverter(
options.SerializerSettings.Converters,
new CompositeNodaPatternConverter<LocalDate>(
LocalDatePattern.Iso,
LocalDatePattern.FullRoundtrip
)
);
ReplaceConverter(
options.SerializerSettings.Converters,
new CompositeNodaPatternConverter<LocalDateTime>(
LocalDateTimePattern.ExtendedIso,
LocalDateTimePattern.GeneralIso,
LocalDateTimePattern.BclRoundtrip,
LocalDateTimePattern.FullRoundtrip,
LocalDateTimePattern.FullRoundtripWithoutCalendar
)
);
ReplaceConverter(
options.SerializerSettings.Converters,
new CompositeNodaPatternConverter<LocalTime>(
LocalTimePattern.ExtendedIso,
LocalTimePattern.GeneralIso,
LocalTimePattern.LongExtendedIso
)
);
ReplaceConverter(
options.SerializerSettings.Converters,
new CompositeNodaPatternConverter<Offset>(
OffsetPattern.GeneralInvariant,
OffsetPattern.GeneralInvariantWithZ
)
);
ReplaceConverter(
options.SerializerSettings.Converters,
new CompositeNodaPatternConverter<Duration>(
DurationPattern.JsonRoundtrip,
DurationPattern.Roundtrip
)
);
ReplaceConverter(
options.SerializerSettings.Converters,
new CompositeNodaPatternConverter<Duration>(
DurationPattern.JsonRoundtrip,
DurationPattern.Roundtrip
)
);
ReplaceConverter(
options.SerializerSettings.Converters,
new CompositeNodaPatternConverter<Period>(
PeriodPattern.Roundtrip,
PeriodPattern.NormalizingIso
)
);
ReplaceConverter(
options.SerializerSettings.Converters,
new CompositeNodaPatternConverter<OffsetDateTime>(
OffsetDateTimePattern.GeneralIso,
OffsetDateTimePattern.FullRoundtrip
)
);
ReplaceConverter(
options.SerializerSettings.Converters,
new CompositeNodaPatternConverter<OffsetDate>(
OffsetDatePattern.GeneralIso,
OffsetDatePattern.FullRoundtrip
)
);
ReplaceConverter(
options.SerializerSettings.Converters,
new CompositeNodaPatternConverter<OffsetTime>(
OffsetTimePattern.Rfc3339,
OffsetTimePattern.GeneralIso,
OffsetTimePattern.ExtendedIso
)
);
ReplaceConverter(
options.SerializerSettings.Converters,
new CompositeNodaPatternConverter<ZonedDateTime>(
ZonedDateTimePattern.CreateWithInvariantCulture("uuuu'-'MM'-'dd'T'HH':'mm':'ss;FFFFFFFFFo<G> z", DateTimeZoneProviders.Tzdb),
ZonedDateTimePattern.ExtendedFormatOnlyIso,
ZonedDateTimePattern.GeneralFormatOnlyIso
)
);
options.SerializerSettings.ConfigureForLaunchPad(_options.DateTimeZoneProvider);
}
);
}

private static void ReplaceConverter<T>(ICollection<JsonConverter> converters, CompositeNodaPatternConverter<T> converter)
{
foreach (var c in converters.Where(z => z.CanConvert(typeof(T))).ToArray())
{
converters.Remove(c);
}
converters.Add(converter);
}
}
}
18 changes: 0 additions & 18 deletions src/AspNetCore.NewtonsoftJson/DateTimeOffsetPattern.cs

This file was deleted.

18 changes: 0 additions & 18 deletions src/AspNetCore.NewtonsoftJson/DateTimePattern.cs

This file was deleted.

105 changes: 8 additions & 97 deletions src/AspNetCore/Conventions/SystemJsonTextConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Rocket.Surgery.Conventions;
using Rocket.Surgery.Conventions.DependencyInjection;
using Rocket.Surgery.LaunchPad.AspNetCore.Conventions;
using Rocket.Surgery.LaunchPad.Foundation;
using System.Collections.Generic;
using System.Linq;
using JsonConverter = System.Text.Json.Serialization.JsonConverter;
Expand All @@ -30,6 +31,12 @@ namespace Rocket.Surgery.LaunchPad.AspNetCore.Conventions
[PublicAPI]
public class SystemJsonTextConvention : IServiceConvention
{
private readonly FoundationOptions _options;

public SystemJsonTextConvention(FoundationOptions? options = null)
{
_options = options ?? new();
}
/// <summary>
/// Registers the specified context.
/// </summary>
Expand All @@ -44,105 +51,9 @@ public void Register(IConventionContext context, IConfiguration configuration, I
services.Configure<JsonOptions>(
options =>
{
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase));
options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
options.JsonSerializerOptions.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
ReplaceConverter(
options.JsonSerializerOptions.Converters,
new CompositeNodaPatternConverter<Instant>(
InstantPattern.General,
InstantPattern.ExtendedIso,
new DateTimeOffsetPattern(),
new DateTimePattern()
)
);
ReplaceConverter(
options.JsonSerializerOptions.Converters,
new CompositeNodaPatternConverter<LocalDate>(
LocalDatePattern.Iso,
LocalDatePattern.FullRoundtrip
)
);
ReplaceConverter(
options.JsonSerializerOptions.Converters,
new CompositeNodaPatternConverter<LocalDateTime>(
LocalDateTimePattern.GeneralIso,
LocalDateTimePattern.ExtendedIso,
LocalDateTimePattern.BclRoundtrip,
LocalDateTimePattern.FullRoundtrip,
LocalDateTimePattern.FullRoundtripWithoutCalendar
)
);
ReplaceConverter(
options.JsonSerializerOptions.Converters,
new CompositeNodaPatternConverter<LocalTime>(
LocalTimePattern.ExtendedIso,
LocalTimePattern.GeneralIso,
LocalTimePattern.LongExtendedIso
)
);
ReplaceConverter(
options.JsonSerializerOptions.Converters,
new CompositeNodaPatternConverter<Offset>(
OffsetPattern.GeneralInvariant,
OffsetPattern.GeneralInvariantWithZ
)
);
ReplaceConverter(
options.JsonSerializerOptions.Converters,
new CompositeNodaPatternConverter<Duration>(
DurationPattern.JsonRoundtrip,
DurationPattern.Roundtrip
)
);
ReplaceConverter(
options.JsonSerializerOptions.Converters,
new CompositeNodaPatternConverter<Period>(
PeriodPattern.Roundtrip,
PeriodPattern.NormalizingIso
)
);
ReplaceConverter(
options.JsonSerializerOptions.Converters,
new CompositeNodaPatternConverter<OffsetDateTime>(
OffsetDateTimePattern.GeneralIso,
OffsetDateTimePattern.FullRoundtrip
)
);
ReplaceConverter(
options.JsonSerializerOptions.Converters,
new CompositeNodaPatternConverter<OffsetDate>(
OffsetDatePattern.GeneralIso,
OffsetDatePattern.FullRoundtrip
)
);
ReplaceConverter(
options.JsonSerializerOptions.Converters,
new CompositeNodaPatternConverter<OffsetTime>(
OffsetTimePattern.Rfc3339,
OffsetTimePattern.GeneralIso,
OffsetTimePattern.ExtendedIso
)
);
ReplaceConverter(
options.JsonSerializerOptions.Converters,
new CompositeNodaPatternConverter<ZonedDateTime>(
ZonedDateTimePattern.CreateWithInvariantCulture("uuuu'-'MM'-'dd'T'HH':'mm':'ss;FFFFFFFFFo<G> z", DateTimeZoneProviders.Tzdb),
ZonedDateTimePattern.ExtendedFormatOnlyIso,
ZonedDateTimePattern.GeneralFormatOnlyIso
)
);
options.JsonSerializerOptions.ConfigureForLaunchPad(_options.DateTimeZoneProvider);
}
);
}

private static void ReplaceConverter<T>(ICollection<JsonConverter> converters, CompositeNodaPatternConverter<T> converter)
{
foreach (var c in converters.Where(z => z.CanConvert(typeof(T))).ToArray())
{
converters.Remove(c);
}
converters.Add(converter);
}
}
}
3 changes: 0 additions & 3 deletions src/AspNetCore/Rocket.Surgery.LaunchPad.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,4 @@
<PackageReference Include="Rocket.Surgery.Build.Information" />
<PackageReference Include="Serilog.AspNetCore" />
</ItemGroup>
<ItemGroup>
<Folder Include="ModelBinding" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.1;net5.0</TargetFrameworks>
<TargetFramework>net5.0</TargetFramework>
<PackageDescription></PackageDescription>
<PackageTags>$(PackageTags)</PackageTags>
</PropertyGroup>
Expand Down
Loading