[backend/core] Fix policy configuration endpoint not updating the configuration correctly depending on JSON format

This commit is contained in:
Laura Hausmann 2024-10-09 06:20:09 +02:00
parent f98d9ce711
commit 23d2664376
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 8 additions and 6 deletions

View file

@ -14,6 +14,7 @@ using Iceshrimp.Backend.Core.Helpers;
using Iceshrimp.Backend.Core.Middleware; using Iceshrimp.Backend.Core.Middleware;
using Iceshrimp.Backend.Core.Services; using Iceshrimp.Backend.Core.Services;
using Iceshrimp.Backend.Core.Tasks; using Iceshrimp.Backend.Core.Tasks;
using Iceshrimp.Shared.Configuration;
using Iceshrimp.Shared.Schemas.Web; using Iceshrimp.Shared.Schemas.Web;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@ -207,14 +208,13 @@ public class AdminController(
string name, [SwaggerBodyExample("{\n \"enabled\": true\n}")] JsonDocument body string name, [SwaggerBodyExample("{\n \"enabled\": true\n}")] JsonDocument body
) )
{ {
// @formatter:off
var type = await policySvc.GetConfigurationType(name) ?? throw GracefulException.NotFound("Policy not found"); var type = await policySvc.GetConfigurationType(name) ?? throw GracefulException.NotFound("Policy not found");
var data = body.Deserialize(type) as IPolicyConfiguration ?? throw GracefulException.BadRequest("Invalid policy config"); var data = body.Deserialize(type, JsonSerialization.Options) as IPolicyConfiguration;
if (data.GetType() != type) throw GracefulException.BadRequest("Invalid policy config"); if (data?.GetType() != type) throw GracefulException.BadRequest("Invalid policy config");
// @formatter:on var serialized = JsonSerializer.Serialize(data, type, JsonSerialization.Options);
await db.PolicyConfiguration await db.PolicyConfiguration
.Upsert(new PolicyConfiguration { Name = name, Data = JsonSerializer.Serialize(data) }) .Upsert(new PolicyConfiguration { Name = name, Data = serialized })
.On(p => new { p.Name }) .On(p => new { p.Name })
.RunAsync(); .RunAsync();

View file

@ -4,6 +4,7 @@ using System.Text.Json;
using Iceshrimp.AssemblyUtils; using Iceshrimp.AssemblyUtils;
using Iceshrimp.Backend.Core.Database; using Iceshrimp.Backend.Core.Database;
using Iceshrimp.Backend.Core.Helpers; using Iceshrimp.Backend.Core.Helpers;
using Iceshrimp.Shared.Configuration;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace Iceshrimp.Backend.Core.Services; namespace Iceshrimp.Backend.Core.Services;
@ -52,7 +53,8 @@ public class PolicyService(IServiceScopeFactory scopeFactory)
config.Name); config.Name);
if (match == null) continue; if (match == null) continue;
var deserialized = JsonSerializer.Deserialize(config.Data, match) as IPolicyConfiguration; var deserialized =
JsonSerializer.Deserialize(config.Data, match, JsonSerialization.Options) as IPolicyConfiguration;
if (deserialized?.Apply() is { } policy) policies.Add(policy); if (deserialized?.Apply() is { } policy) policies.Add(policy);
} }