a fork of iceshrimp.net but a tweaked frontend to my personal liking. waow
fediverse social-media social iceshrimp fedi
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

[backend/openapi] Fix OverrideRequestBodyExampleOperationFilter

+23 -12
+2 -3
Iceshrimp.Backend/Controllers/Web/AdminController.cs
··· 440 440 [HttpPut("policy/{name}")] 441 441 [ProducesResults(HttpStatusCode.OK)] 442 442 [ProducesErrors(HttpStatusCode.BadRequest, HttpStatusCode.NotFound)] 443 - public async Task UpdateWordRejectPolicy( 444 - string name, [SwaggerBodyExample("{\n \"enabled\": true\n}")] JsonDocument body 445 - ) 443 + [OverrideRequestBodyExample("{\n \"enabled\": true\n}")] 444 + public async Task UpdateWordRejectPolicy(string name, JsonDocument body) 446 445 { 447 446 var type = await policySvc.GetConfigurationTypeAsync(name) ?? 448 447 throw GracefulException.NotFound("Policy not found");
+21 -9
Iceshrimp.Backend/Core/Extensions/SwaggerGenOptionsExtensions.cs
··· 1 1 using System.Diagnostics.CodeAnalysis; 2 2 using System.Reflection; 3 + using System.Text.Json.Nodes; 3 4 using Iceshrimp.Backend.Controllers.Federation.Attributes; 4 5 using Iceshrimp.Backend.Controllers.Mastodon.Attributes; 5 6 using Iceshrimp.Backend.Controllers.Mastodon.Schemas; ··· 18 19 public static void AddFilters(this SwaggerGenOptions options) 19 20 { 20 21 options.SchemaFilter<RequireNonNullablePropertiesSchemaFilter>(); 21 - options.SchemaFilter<SwaggerBodyExampleSchemaFilter>(); 22 22 options.SupportNonNullableReferenceTypes(); // Sets Nullable flags appropriately. 23 23 options.UseAllOfToExtendReferenceSchemas(); // Allows $ref enums to be nullable 24 24 options.UseAllOfForInheritance(); // Allows $ref objects to be nullable ··· 26 26 options.OperationFilter<HybridRequestOperationFilter>(); 27 27 options.OperationFilter<PossibleErrorsOperationFilter>(); 28 28 options.OperationFilter<PossibleResultsOperationFilter>(); 29 + options.OperationFilter<OverrideRequestBodyExampleOperationFilter>(); 29 30 options.DocumentFilter<AuthorizeCheckOperationDocumentFilter>(); 30 31 options.DocInclusionPredicate(DocInclusionPredicate); 31 32 } ··· 72 73 73 74 [SuppressMessage("ReSharper", "ClassNeverInstantiated.Local", 74 75 Justification = "SwaggerGenOptions.SchemaFilter<T> instantiates this class at runtime")] 75 - private class SwaggerBodyExampleSchemaFilter : ISchemaFilter 76 + private class OverrideRequestBodyExampleOperationFilter : IOperationFilter 76 77 { 77 - public void Apply(IOpenApiSchema schema, SchemaFilterContext context) 78 + public void Apply(OpenApiOperation operation, OperationFilterContext context) 78 79 { 79 - var att = context.ParameterInfo?.GetCustomAttribute<SwaggerBodyExampleAttribute>(); 80 - if (att == null) return; 81 - schema.Examples?.Clear(); 82 - schema.Examples?.Add(att.Value); 80 + if (context.MethodInfo.GetCustomAttribute<OverrideRequestBodyExampleAttribute>() is not { } attr) 81 + return; 82 + operation.RequestBody = new OpenApiRequestBody 83 + { 84 + Content = new Dictionary<string, OpenApiMediaType> 85 + { 86 + ["application/json"] = new() 87 + { 88 + Examples = new Dictionary<string, IOpenApiExample> 89 + { 90 + ["example"] = new OpenApiExample { Value = JsonNode.Parse(attr.Value)! } 91 + } 92 + } 93 + } 94 + }; 83 95 } 84 96 } 85 97 ··· 386 398 } 387 399 } 388 400 389 - public class SwaggerBodyExampleAttribute(string value) : Attribute 401 + public class OverrideRequestBodyExampleAttribute(string value) : Attribute 390 402 { 391 403 public string Value => value; 392 404 } 393 - } 405 + }