From 8379d2af81a209734d8a5c9efb0e2cfef3c7e1b9 Mon Sep 17 00:00:00 2001 From: Ted Wollman <25165500+TheTedder@users.noreply.github.com> Date: Thu, 18 Jul 2024 12:43:28 -0400 Subject: [PATCH] Remove RequiredNotNullableSchemaFilter. --- .../Models/Requests/LeaderboardRequests.cs | 4 +- LeaderboardBackend/Program.cs | 2 - .../RequiredNotNullableSchemaFilter.cs | 50 ------------------- 3 files changed, 2 insertions(+), 54 deletions(-) delete mode 100644 LeaderboardBackend/Swagger/RequiredNotNullableSchemaFilter.cs diff --git a/LeaderboardBackend/Models/Requests/LeaderboardRequests.cs b/LeaderboardBackend/Models/Requests/LeaderboardRequests.cs index 574de82c..51ed9f14 100644 --- a/LeaderboardBackend/Models/Requests/LeaderboardRequests.cs +++ b/LeaderboardBackend/Models/Requests/LeaderboardRequests.cs @@ -9,12 +9,12 @@ public record CreateLeaderboardRequest /// The display name of the `Leaderboard` to create. /// /// Foo Bar - public string Name { get; set; } = null!; + public required string Name { get; set; } = null!; /// /// The URL-scoped unique identifier of the `Leaderboard`.
/// Must be [2, 80] in length and consist only of alphanumeric characters and hyphens. ///
/// foo-bar - public string Slug { get; set; } = null!; + public required string Slug { get; set; } = null!; } diff --git a/LeaderboardBackend/Program.cs b/LeaderboardBackend/Program.cs index 19f0249c..3d38e70e 100644 --- a/LeaderboardBackend/Program.cs +++ b/LeaderboardBackend/Program.cs @@ -11,7 +11,6 @@ using LeaderboardBackend.Authorization; using LeaderboardBackend.Models.Entities; using LeaderboardBackend.Services; -using LeaderboardBackend.Swagger; using MailKit.Net.Smtp; using MicroElements.Swashbuckle.NodaTime; using Microsoft.AspNetCore.Authentication.JwtBearer; @@ -195,7 +194,6 @@ ); c.SupportNonNullableReferenceTypes(); - c.SchemaFilter(); c.MapType(() => new OpenApiSchema { Type = "string", Pattern = "^[a-zA-Z0-9-_]{22}$" }); c.ConfigureForNodaTimeWithSystemTextJson(jsonSerializerOptions); }); diff --git a/LeaderboardBackend/Swagger/RequiredNotNullableSchemaFilter.cs b/LeaderboardBackend/Swagger/RequiredNotNullableSchemaFilter.cs deleted file mode 100644 index f580bace..00000000 --- a/LeaderboardBackend/Swagger/RequiredNotNullableSchemaFilter.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System.Reflection; -using System.Text.Json.Serialization; -using Microsoft.OpenApi.Models; -using Swashbuckle.AspNetCore.SwaggerGen; - -namespace LeaderboardBackend.Swagger; - -// https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/2036#issuecomment-894015122 -internal class RequiredNotNullableSchemaFilter : ISchemaFilter -{ - public void Apply(OpenApiSchema schema, SchemaFilterContext context) - { - if (schema.Properties is null) - { - return; - } - - foreach ((string propertyName, OpenApiSchema property) in schema.Properties) - { - if (property.Reference != null) - { - - MemberInfo? field = context.Type - .GetMembers(BindingFlags.Public | BindingFlags.Instance) - .FirstOrDefault(x => string.Equals(x.Name, propertyName, StringComparison.InvariantCultureIgnoreCase)); - - if (field == null) - { - continue; - } - - Type fieldType = field switch - { - FieldInfo fieldInfo => fieldInfo.FieldType, - PropertyInfo propertyInfo => propertyInfo.PropertyType, - _ => throw new NotSupportedException(), - }; - - property.Nullable = fieldType.IsValueType - ? Nullable.GetUnderlyingType(fieldType) != null // is not a Nullable<> type - : !field.IsNonNullableReferenceType(); - } - - if (!property.Nullable) - { - schema.Required.Add(propertyName); - } - } - } -}