|
| 1 | +using DotCruz.Notifications.Api.Filters; |
| 2 | +using Microsoft.AspNetCore.Mvc.ApplicationModels; |
| 3 | +using Microsoft.AspNetCore.Mvc.ModelBinding; |
| 4 | +using System.Globalization; |
| 5 | +using System.Text.Json; |
| 6 | +using System.Text.Json.Serialization; |
| 7 | +using System.Text.RegularExpressions; |
| 8 | + |
| 9 | +namespace DotCruz.Notifications.Api.Configurations; |
| 10 | + |
| 11 | +public static class ApiConventionsConfiguration |
| 12 | +{ |
| 13 | + public static IMvcBuilder AddApiConventions(this IServiceCollection services) |
| 14 | + { |
| 15 | + services.ConfigureHttpJsonOptions(options => |
| 16 | + { |
| 17 | + options.SerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower; |
| 18 | + options.SerializerOptions.DictionaryKeyPolicy = JsonNamingPolicy.SnakeCaseLower; |
| 19 | + options.SerializerOptions.Converters.Add(new JsonStringEnumConverter()); |
| 20 | + }); |
| 21 | + |
| 22 | + return services |
| 23 | + .AddControllers(options => |
| 24 | + { |
| 25 | + options.Filters.Add<ExceptionFilter>(); |
| 26 | + |
| 27 | + options.Conventions.Add( |
| 28 | + new RouteTokenTransformerConvention(new KebabCaseParameterTransformer())); |
| 29 | + |
| 30 | + ReplaceQueryValueProvider(options.ValueProviderFactories); |
| 31 | + }) |
| 32 | + .AddJsonOptions(options => |
| 33 | + { |
| 34 | + options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower; |
| 35 | + options.JsonSerializerOptions.DictionaryKeyPolicy = JsonNamingPolicy.SnakeCaseLower; |
| 36 | + options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()); |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + private static void ReplaceQueryValueProvider(IList<IValueProviderFactory> factories) |
| 41 | + { |
| 42 | + var defaultFactory = factories.OfType<QueryStringValueProviderFactory>().FirstOrDefault(); |
| 43 | + |
| 44 | + if (defaultFactory is null) |
| 45 | + { |
| 46 | + factories.Add(new SnakeCaseQueryValueProviderFactory()); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + var index = factories.IndexOf(defaultFactory); |
| 51 | + factories[index] = new SnakeCaseQueryValueProviderFactory(); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +public sealed partial class KebabCaseParameterTransformer : IOutboundParameterTransformer |
| 56 | +{ |
| 57 | + public string? TransformOutbound(object? value) |
| 58 | + { |
| 59 | + var input = value?.ToString(); |
| 60 | + |
| 61 | + return string.IsNullOrEmpty(input) |
| 62 | + ? input |
| 63 | + : WordBoundaryRegex().Replace(input, "$1-$2").ToLowerInvariant(); |
| 64 | + } |
| 65 | + |
| 66 | + [GeneratedRegex("([a-z0-9])([A-Z])")] |
| 67 | + private static partial Regex WordBoundaryRegex(); |
| 68 | +} |
| 69 | + |
| 70 | +public sealed class SnakeCaseQueryValueProviderFactory : IValueProviderFactory |
| 71 | +{ |
| 72 | + public Task CreateValueProviderAsync(ValueProviderFactoryContext context) |
| 73 | + { |
| 74 | + ArgumentNullException.ThrowIfNull(context); |
| 75 | + |
| 76 | + var query = context.ActionContext.HttpContext.Request.Query; |
| 77 | + context.ValueProviders.Add( |
| 78 | + new SnakeCaseQueryValueProvider(BindingSource.Query, query, CultureInfo.InvariantCulture)); |
| 79 | + |
| 80 | + return Task.CompletedTask; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +public sealed class SnakeCaseQueryValueProvider( |
| 85 | + BindingSource bindingSource, |
| 86 | + IQueryCollection values, |
| 87 | + CultureInfo culture) : QueryStringValueProvider(bindingSource, values, culture) |
| 88 | +{ |
| 89 | + public override bool ContainsPrefix(string prefix) => base.ContainsPrefix(ToSnakeCase(prefix)); |
| 90 | + |
| 91 | + public override ValueProviderResult GetValue(string key) => base.GetValue(ToSnakeCase(key)); |
| 92 | + |
| 93 | + private static string ToSnakeCase(string key) |
| 94 | + { |
| 95 | + if (string.IsNullOrEmpty(key)) |
| 96 | + { |
| 97 | + return key; |
| 98 | + } |
| 99 | + |
| 100 | + return string.Join('.', key.Split('.').Select(JsonNamingPolicy.SnakeCaseLower.ConvertName)); |
| 101 | + } |
| 102 | +} |
0 commit comments