backend: Require explicit account state values
This commit is contained in:
1 parent
52fb8af684
commit
0215af0749
7 files changed
+143
-19
No files matched your search
@@ -2,6 +2,7 @@ using Microsoft.OpenApi;
|
||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
using Helpdesk.Api.Controllers;
|
||||
using Helpdesk.Application.Issues;
|
||||
using Helpdesk.Application.Users;
|
||||
|
||||
namespace Helpdesk.Api.Infrastructure;
|
||||
|
||||
@@ -34,6 +35,7 @@ public static class SwaggerSetup
|
||||
[new OpenApiSecuritySchemeReference(SchemeId, document)] = [],
|
||||
});
|
||||
options.OperationFilter<IssueRequestBody>();
|
||||
options.SchemaFilter<UserStateRequestSchema>();
|
||||
|
||||
var xml = Path.Combine(AppContext.BaseDirectory, "Helpdesk.Api.xml");
|
||||
if (File.Exists(xml))
|
||||
@@ -43,6 +45,30 @@ public static class SwaggerSetup
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class UserStateRequestSchema : ISchemaFilter
|
||||
{
|
||||
public void Apply(IOpenApiSchema schema, SchemaFilterContext context)
|
||||
{
|
||||
string[] fields = context.Type switch
|
||||
{
|
||||
var type when type == typeof(UpdateProfileRequest) => ["emailNotificationsEnabled"],
|
||||
var type when type == typeof(SetActiveRequest) => ["isActive"],
|
||||
var type when type == typeof(SaveAdminUserRequest) => ["isActive", "emailConfirmed", "emailNotificationsEnabled"],
|
||||
_ => [],
|
||||
};
|
||||
if (fields.Length == 0 || schema is not OpenApiSchema { Properties: { } properties } request)
|
||||
return;
|
||||
|
||||
request.Required ??= new HashSet<string>();
|
||||
foreach (var field in fields)
|
||||
{
|
||||
request.Required.Add(field);
|
||||
if (properties[field] is OpenApiSchema property)
|
||||
property.Type = JsonSchemaType.Boolean;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class IssueRequestBody : IOperationFilter
|
||||
{
|
||||
public void Apply(OpenApiOperation operation, OperationFilterContext context)
|
||||
|
||||
Reference in new issue
Block a user