From 6881816c948887ef93d07559ab0812da72e86511 Mon Sep 17 00:00:00 2001 From: adam Date: Wed, 12 Mar 2025 16:05:22 -0400 Subject: [PATCH] self referencing serialization ignored --- Models/Account.cs | 2 ++ Models/Channel.cs | 19 +++++++++++++++++++ Program.cs | 5 ++++- .../Controllers/api/ChannelsControler.cs | 4 ++++ 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/Models/Account.cs b/Models/Account.cs index bb3eaf3..7ca9aba 100644 --- a/Models/Account.cs +++ b/Models/Account.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; using System.Reflection; +using System.Text.Json.Serialization; using System.Threading.Tasks; public class Account @@ -27,5 +28,6 @@ public class Account public bool IsBot { get; set; } //webhook counts public Channel SeenInChannel { get; set; } public string Protocol { get; set; } + [JsonIgnore] public User IsUser {get; set;} } \ No newline at end of file diff --git a/Models/Channel.cs b/Models/Channel.cs index f9c820d..0026965 100644 --- a/Models/Channel.cs +++ b/Models/Channel.cs @@ -7,6 +7,7 @@ using System.Reflection; using System.Threading.Tasks; using Microsoft.AspNetCore.Components.Web; using Microsoft.EntityFrameworkCore; +using Newtonsoft.Json; using static vassago.Models.Enumerations; public class Channel @@ -17,6 +18,7 @@ public class Channel public string DisplayName { get; set; } [DeleteBehavior(DeleteBehavior.Cascade)] public List SubChannels { get; set; } + [JsonIgnore] public Channel ParentChannel { get; set; } public string Protocol { get; set; } [DeleteBehavior(DeleteBehavior.Cascade)] @@ -82,6 +84,23 @@ public class Channel } } } + + /// + ///break self-referencing loops for library-agnostic serialization + /// + public Channel AsSerializable() + { + var toReturn = this.MemberwiseClone() as Channel; + toReturn.ParentChannel = null; + if(toReturn.Users?.Count > 0) + { + foreach (var account in toReturn.Users) + { + account.SeenInChannel = null; + } + } + return toReturn; + } } public class DefinitePermissionSettings diff --git a/Program.cs b/Program.cs index 7b5c9f0..6e8287e 100644 --- a/Program.cs +++ b/Program.cs @@ -11,7 +11,10 @@ var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllersWithViews(); builder.Services.AddSingleton(); builder.Services.AddDbContext(); -builder.Services.AddControllers().AddNewtonsoftJson(); +builder.Services.AddControllers().AddNewtonsoftJson(options => { + options.SerializerSettings.ReferenceLoopHandling = + Newtonsoft.Json.ReferenceLoopHandling.Ignore; + }); builder.Services.AddProblemDetails(); builder.Services.Configure(o => { o.ViewLocationFormats.Clear(); diff --git a/WebInterface/Controllers/api/ChannelsControler.cs b/WebInterface/Controllers/api/ChannelsControler.cs index 7081646..3001123 100644 --- a/WebInterface/Controllers/api/ChannelsControler.cs +++ b/WebInterface/Controllers/api/ChannelsControler.cs @@ -33,6 +33,10 @@ public class ChannelsController : ControllerBase { _logger.LogError($"attempt to update channel {channel.Id}, not found"); return NotFound(); + } + else + { + _logger.LogDebug($"patching {channel.DisplayName} (id: {channel.Id})"); } //settable values: lewdness filter level, meanness filter level. maybe i could decorate them... fromDb.LewdnessFilterLevel = channel.LewdnessFilterLevel;