-
Notifications
You must be signed in to change notification settings - Fork 8
V10.3.0/support for minimal api #147
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
87d22f3
✅ add tests for json and xml formatter options registration
gimlichael 0764213
🐛 use TryConfigure instead of Configure in service collection extensions
gimlichael 801a1bb
🐛 use tryconfigure instead of configure in service collection
gimlichael 8c00056
✨ add minimaljsonoptions and service collection extensions
gimlichael 8044a45
✅ add tests for minimaljsonoptions and service collection extensions
gimlichael 426be6a
🐛 refactor minimaljsonoptions to use jsonserializeroptions directly
gimlichael 8b580dc
✅ add test to ensure minimaljsonoptions does not duplicate converters
gimlichael 8135744
📝 update documentation for MinimalJsonOptions constructor parameters
gimlichael File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/Cuemon.Extensions.AspNetCore.Text.Json/MinimalJsonOptions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| using System.Text.Json; | ||
| using Cuemon.Collections.Generic; | ||
| using Cuemon.Extensions.AspNetCore.Text.Json.Converters; | ||
| using Cuemon.Extensions.Text.Json.Formatters; | ||
| using Microsoft.AspNetCore.Http.Json; | ||
| using Microsoft.Extensions.Options; | ||
|
|
||
| namespace Cuemon.Extensions.AspNetCore.Text.Json | ||
| { | ||
| /// <summary> | ||
| /// A <see cref="ConfigureOptions{TOptions}"/> implementation which will pass <see cref="JsonFormatterOptions"/> to <see cref="JsonOptions"/>. | ||
| /// </summary> | ||
| public class MinimalJsonOptions : ConfigureOptions<JsonOptions> | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="MinimalJsonOptions" /> class. | ||
| /// </summary> | ||
| /// <param name="formatterOptions"> | ||
| /// The formatter options. | ||
| /// </param> | ||
| public MinimalJsonOptions(IOptions<JsonFormatterOptions> formatterOptions) : base(mo => | ||
| { | ||
| var options = formatterOptions.Value; | ||
|
|
||
| var settings = new JsonSerializerOptions(options.Settings); | ||
| settings.Converters.AddHttpExceptionDescriptorConverter(o => o.SensitivityDetails = options.SensitivityDetails); | ||
|
|
||
| Decorator.Enclose(mo.SerializerOptions.Converters).AddRange(settings.Converters); | ||
| mo.SerializerOptions.AllowOutOfOrderMetadataProperties = settings.AllowOutOfOrderMetadataProperties; | ||
| mo.SerializerOptions.AllowTrailingCommas = settings.AllowTrailingCommas; | ||
| mo.SerializerOptions.DefaultBufferSize = settings.DefaultBufferSize; | ||
| mo.SerializerOptions.Encoder = settings.Encoder; | ||
| mo.SerializerOptions.DictionaryKeyPolicy = settings.DictionaryKeyPolicy; | ||
| mo.SerializerOptions.DefaultIgnoreCondition = settings.DefaultIgnoreCondition; | ||
| mo.SerializerOptions.NumberHandling = settings.NumberHandling; | ||
| mo.SerializerOptions.PreferredObjectCreationHandling = settings.PreferredObjectCreationHandling; | ||
| mo.SerializerOptions.UnknownTypeHandling = settings.UnknownTypeHandling; | ||
| mo.SerializerOptions.UnmappedMemberHandling = settings.UnmappedMemberHandling; | ||
| mo.SerializerOptions.IgnoreReadOnlyProperties = settings.IgnoreReadOnlyProperties; | ||
| mo.SerializerOptions.IgnoreReadOnlyFields = settings.IgnoreReadOnlyFields; | ||
| mo.SerializerOptions.IncludeFields = settings.IncludeFields; | ||
| mo.SerializerOptions.MaxDepth = settings.MaxDepth; | ||
| mo.SerializerOptions.PropertyNamingPolicy = settings.PropertyNamingPolicy; | ||
| mo.SerializerOptions.PropertyNameCaseInsensitive = settings.PropertyNameCaseInsensitive; | ||
| mo.SerializerOptions.ReadCommentHandling = settings.ReadCommentHandling; | ||
| mo.SerializerOptions.WriteIndented = settings.WriteIndented; | ||
| mo.SerializerOptions.IndentCharacter = settings.IndentCharacter; | ||
| mo.SerializerOptions.IndentSize = settings.IndentSize; | ||
| mo.SerializerOptions.ReferenceHandler = settings.ReferenceHandler; | ||
| mo.SerializerOptions.NewLine = settings.NewLine; | ||
| mo.SerializerOptions.RespectNullableAnnotations = settings.RespectNullableAnnotations; | ||
| mo.SerializerOptions.RespectRequiredConstructorParameters = settings.RespectRequiredConstructorParameters; | ||
| #if NET10_0_OR_GREATER | ||
| mo.SerializerOptions.AllowDuplicateProperties = settings.AllowDuplicateProperties; | ||
| #endif | ||
| if (settings.TypeInfoResolver is not null) | ||
| { | ||
| mo.SerializerOptions.TypeInfoResolver = settings.TypeInfoResolver; | ||
| } | ||
| }) | ||
| { | ||
| } | ||
| } | ||
| } | ||
36 changes: 36 additions & 0 deletions
36
src/Cuemon.Extensions.AspNetCore.Text.Json/ServiceCollectionExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| using System; | ||
| using Cuemon.Extensions.AspNetCore.Text.Json.Formatters; | ||
| using Cuemon.Extensions.Text.Json.Formatters; | ||
| using Microsoft.AspNetCore.Http.Json; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.DependencyInjection.Extensions; | ||
| using Microsoft.Extensions.Options; | ||
|
|
||
| namespace Cuemon.Extensions.AspNetCore.Text.Json | ||
| { | ||
| /// <summary> | ||
| /// Extension methods for the <see cref="IServiceCollection"/> interface. | ||
| /// </summary> | ||
| public static class ServiceCollectionExtensions | ||
| { | ||
| /// <summary> | ||
| /// Adds a <see cref="JsonFormatterOptions"/> service to the specified <see cref="IServiceCollection"/>. | ||
| /// </summary> | ||
| /// <param name="services">The <see cref="IServiceCollection"/> to add services to.</param> | ||
| /// <param name="setup">The <see cref="JsonFormatterOptions"/> which may be configured.</param> | ||
| /// <returns>An <see cref="IServiceCollection"/> that can be used to further configure other services.</returns> | ||
| /// <remarks> | ||
| /// This method registers a <see cref="MinimalJsonOptions"/> configuration as a singleton <see cref="IConfigureOptions{TOptions}"/> for <see cref="JsonOptions"/> | ||
| /// and delegates to <see cref="Cuemon.Extensions.AspNetCore.Text.Json.Formatters.ServiceCollectionExtensions.AddJsonExceptionResponseFormatter"/> to configure the JSON exception response formatter. | ||
| /// </remarks> | ||
| /// <exception cref="ArgumentNullException"> | ||
| /// <paramref name="services"/> cannot be null. | ||
| /// </exception> | ||
| public static IServiceCollection AddMinimalJsonOptions(this IServiceCollection services, Action<JsonFormatterOptions> setup = null) | ||
| { | ||
| Validator.ThrowIfNull(services); | ||
| services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<JsonOptions>, MinimalJsonOptions>()); | ||
| return services.AddJsonExceptionResponseFormatter(setup); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...uemon.Extensions.AspNetCore.Tests/Text.Json/Formatters/ServiceCollectionExtensionsTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| using System.Linq; | ||
| using Cuemon.Extensions.Text.Json.Formatters; | ||
| using Codebelt.Extensions.Xunit; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Options; | ||
| using Xunit; | ||
|
|
||
| namespace Cuemon.Extensions.AspNetCore.Text.Json.Formatters | ||
| { | ||
| public class ServiceCollectionExtensionsTest : Test | ||
| { | ||
| public ServiceCollectionExtensionsTest(ITestOutputHelper output) : base(output) | ||
| { | ||
| } | ||
|
|
||
| [Fact] | ||
| public void AddJsonFormatterOptions_ShouldOnlyRegisterOnce_WhenCalledMultipleTimes() | ||
| { | ||
| var sut = new ServiceCollection(); | ||
|
|
||
| sut.AddJsonFormatterOptions(); | ||
| sut.AddJsonFormatterOptions(); | ||
| sut.AddJsonFormatterOptions(); | ||
|
|
||
| var configureOptionsCount = sut.Count(sd => | ||
| sd.ServiceType == typeof(IConfigureOptions<JsonFormatterOptions>)); | ||
|
|
||
| TestOutput.WriteLine($"IConfigureOptions<JsonFormatterOptions> registrations: {configureOptionsCount}"); | ||
|
|
||
| Assert.Equal(1, configureOptionsCount); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.