fix(schedules): reload write-path response with read includes + order GET by index (#229)
Bug 1 (500 on watermark/graphics save): Replace/Add handlers projected the freshly-built entity graph, whose ProgramScheduleItemWatermark / -GraphicsElement join rows carry only foreign-key ids — the Watermark/GraphicsElement navs are null, and Mapper.ProjectToViewModel dereferences them unguarded, throwing an NRE that the controller surfaced as a 500 on PUT/POST. Both handlers now reload the persisted item(s) through the read-side include chain before projecting. Extracted that chain into ProgramScheduleItemQueryExtensions.IncludeScheduleItemDetails() so GET, Replace and Add share one source of truth. Masking: PersistItems returned a lazy LanguageExt Map, and the existing round-trip test only checked .IsRight — never enumerating it, so the deferred NRE never fired. The new ScheduleItemWriteProjectionTests force enumeration (as the controller's .ToList()/serialization does) and seed watermark/graphics via a separate context so the handler's fresh factory context has nothing pre-tracked. Bug 2 (server side): GetProgramScheduleItemsHandler now .OrderBy(i => i.Index) — it previously returned id order, which is not index order. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -60,7 +60,15 @@ public class AddProgramScheduleItemHandler : ProgramScheduleItemCommandBase,
|
||||
await _channel.WriteAsync(new BuildPlayout(playout.Id, PlayoutBuildMode.Refresh), cancellationToken);
|
||||
}
|
||||
|
||||
return ProjectToViewModel(item);
|
||||
// reload with the full navigation graph before projecting: BuildItem creates the watermark/graphics
|
||||
// join rows with only their foreign-key ids set, so the tracked entities have null Watermark /
|
||||
// GraphicsElement navs that ProjectToViewModel dereferences (would 500 on POST — see #229).
|
||||
ProgramScheduleItem persisted = await dbContext.ProgramScheduleItems
|
||||
.Filter(psi => psi.Id == item.Id)
|
||||
.IncludeScheduleItemDetails()
|
||||
.SingleAsync(cancellationToken);
|
||||
|
||||
return ProjectToViewModel(persisted);
|
||||
}
|
||||
|
||||
private static async Task<Validation<BaseError, ProgramSchedule>> Validate(
|
||||
|
||||
+10
-1
@@ -56,7 +56,16 @@ public class ReplaceProgramScheduleItemsHandler(
|
||||
await channel.WriteAsync(new BuildPlayout(playout.Id, PlayoutBuildMode.Refresh), cancellationToken);
|
||||
}
|
||||
|
||||
return programSchedule.Items.Map(ProjectToViewModel);
|
||||
// reload with the full navigation graph before projecting: BuildItem creates the watermark/graphics
|
||||
// join rows with only their foreign-key ids set, so the tracked entities have null Watermark /
|
||||
// GraphicsElement navs that ProjectToViewModel dereferences (would 500 on PUT — see #229).
|
||||
List<ProgramScheduleItem> persisted = await dbContext.ProgramScheduleItems
|
||||
.Filter(psi => psi.ProgramScheduleId == programSchedule.Id)
|
||||
.IncludeScheduleItemDetails()
|
||||
.OrderBy(i => i.Index)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
return persisted.Map(ProjectToViewModel).ToList();
|
||||
}
|
||||
|
||||
private static async Task<Validation<BaseError, ProgramSchedule>> Validate(
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
using ErsatzTV.Core.Domain;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace ErsatzTV.Application.ProgramSchedules;
|
||||
|
||||
internal static class ProgramScheduleItemQueryExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// The single source of truth for the navigation graph a <see cref="ProgramScheduleItem" /> needs before
|
||||
/// it can be projected via <see cref="Mapper.ProjectToViewModel" />. The mapper dereferences the
|
||||
/// <c>Watermark</c> / <c>GraphicsElement</c> navs of each join row without a null guard, so any handler
|
||||
/// that projects freshly-persisted items MUST reload through this include chain (see #229 — the write
|
||||
/// path used to project the tracked-but-unloaded graph and threw a NullReferenceException, surfacing as a
|
||||
/// 500 on PUT/POST whenever an item carried a watermark or graphics element).
|
||||
/// </summary>
|
||||
public static IQueryable<ProgramScheduleItem> IncludeScheduleItemDetails(this IQueryable<ProgramScheduleItem> query) =>
|
||||
query
|
||||
.Include(i => i.Collection)
|
||||
.Include(i => i.MultiCollection)
|
||||
.Include(i => i.SmartCollection)
|
||||
.Include(i => i.RerunCollection)
|
||||
.Include(i => i.Playlist)
|
||||
.Include(i => i.MediaItem)
|
||||
.ThenInclude(i => (i as Movie).MovieMetadata)
|
||||
.ThenInclude(mm => mm.Artwork)
|
||||
.Include(i => i.MediaItem)
|
||||
.ThenInclude(i => (i as Season).SeasonMetadata)
|
||||
.ThenInclude(sm => sm.Artwork)
|
||||
.Include(i => i.MediaItem)
|
||||
.ThenInclude(i => (i as Season).Show)
|
||||
.ThenInclude(s => s.ShowMetadata)
|
||||
.ThenInclude(sm => sm.Artwork)
|
||||
.Include(i => i.MediaItem)
|
||||
.ThenInclude(i => (i as Show).ShowMetadata)
|
||||
.ThenInclude(sm => sm.Artwork)
|
||||
.Include(i => i.MediaItem)
|
||||
.ThenInclude(i => (i as Artist).ArtistMetadata)
|
||||
.ThenInclude(am => am.Artwork)
|
||||
.Include(i => i.PreRollFiller)
|
||||
.Include(i => i.MidRollFiller)
|
||||
.Include(i => i.PostRollFiller)
|
||||
.Include(i => i.TailFiller)
|
||||
.Include(i => i.FallbackFiller)
|
||||
.Include(i => i.ProgramScheduleItemWatermarks)
|
||||
.ThenInclude(i => i.Watermark)
|
||||
.Include(i => i.ProgramScheduleItemGraphicsElements)
|
||||
.ThenInclude(i => i.GraphicsElement);
|
||||
}
|
||||
@@ -20,36 +20,8 @@ public class GetProgramScheduleItemsHandler(IDbContextFactory<TvContext> dbConte
|
||||
|
||||
return await dbContext.ProgramScheduleItems
|
||||
.Filter(psi => psi.ProgramScheduleId == request.Id)
|
||||
.Include(i => i.Collection)
|
||||
.Include(i => i.MultiCollection)
|
||||
.Include(i => i.SmartCollection)
|
||||
.Include(i => i.RerunCollection)
|
||||
.Include(i => i.Playlist)
|
||||
.Include(i => i.MediaItem)
|
||||
.ThenInclude(i => (i as Movie).MovieMetadata)
|
||||
.ThenInclude(mm => mm.Artwork)
|
||||
.Include(i => i.MediaItem)
|
||||
.ThenInclude(i => (i as Season).SeasonMetadata)
|
||||
.ThenInclude(sm => sm.Artwork)
|
||||
.Include(i => i.MediaItem)
|
||||
.ThenInclude(i => (i as Season).Show)
|
||||
.ThenInclude(s => s.ShowMetadata)
|
||||
.ThenInclude(sm => sm.Artwork)
|
||||
.Include(i => i.MediaItem)
|
||||
.ThenInclude(i => (i as Show).ShowMetadata)
|
||||
.ThenInclude(sm => sm.Artwork)
|
||||
.Include(i => i.MediaItem)
|
||||
.ThenInclude(i => (i as Artist).ArtistMetadata)
|
||||
.ThenInclude(am => am.Artwork)
|
||||
.Include(i => i.PreRollFiller)
|
||||
.Include(i => i.MidRollFiller)
|
||||
.Include(i => i.PostRollFiller)
|
||||
.Include(i => i.TailFiller)
|
||||
.Include(i => i.FallbackFiller)
|
||||
.Include(i => i.ProgramScheduleItemWatermarks)
|
||||
.ThenInclude(i => i.Watermark)
|
||||
.Include(i => i.ProgramScheduleItemGraphicsElements)
|
||||
.ThenInclude(i => i.GraphicsElement)
|
||||
.IncludeScheduleItemDetails()
|
||||
.OrderBy(i => i.Index)
|
||||
.ToListAsync(cancellationToken)
|
||||
.Map(programScheduleItems => programScheduleItems.Map(ProjectToViewModel)
|
||||
.Map(psi => EnforceProperties(maybeProgramSchedule, psi)).ToList());
|
||||
|
||||
@@ -0,0 +1,167 @@
|
||||
using System.Threading.Channels;
|
||||
using ErsatzTV.Application;
|
||||
using ErsatzTV.Application.ProgramSchedules;
|
||||
using ErsatzTV.Core;
|
||||
using ErsatzTV.Core.Api.Scheduling;
|
||||
using ErsatzTV.Core.Domain;
|
||||
using ErsatzTV.Core.Scheduling;
|
||||
using ErsatzTV.Infrastructure.Data;
|
||||
using ErsatzTV.Tests.Support;
|
||||
using LanguageExt;
|
||||
using NUnit.Framework;
|
||||
using Shouldly;
|
||||
|
||||
namespace ErsatzTV.Tests.Application.ProgramSchedules;
|
||||
|
||||
/// <summary>
|
||||
/// Regression tests for #229. The write path (Replace = PUT, Add = POST) used to project freshly-persisted
|
||||
/// items whose watermark/graphics join rows only carry foreign-key ids — the Watermark / GraphicsElement navs
|
||||
/// are null, and <c>Mapper.ProjectToViewModel</c> dereferences them without a guard, throwing an NRE that
|
||||
/// surfaced as a 500. The pre-existing round-trip test masked this because the Replace handler returned a
|
||||
/// <b>lazy</b> LanguageExt <c>Map</c> sequence and that test only checked <c>.IsRight</c> — it never enumerated
|
||||
/// the projected items, so the deferred NRE never fired. These tests force enumeration of the write-path
|
||||
/// response (exactly as the controller's <c>.ToList()</c> / single-item serialization does) and seed the
|
||||
/// watermark/graphics entities through a <b>separate</b> context so the handler's fresh factory context has
|
||||
/// nothing pre-tracked (no accidental change-tracker fix-up).
|
||||
/// </summary>
|
||||
[TestFixture]
|
||||
public class ScheduleItemWriteProjectionTests
|
||||
{
|
||||
private InMemoryTvContext _db = null!;
|
||||
private ChannelWriter<IBackgroundServiceRequest> _worker = null!;
|
||||
|
||||
[SetUp]
|
||||
public async Task SetUp()
|
||||
{
|
||||
_db = await InMemoryTvContext.CreateAsync();
|
||||
_worker = System.Threading.Channels.Channel.CreateUnbounded<IBackgroundServiceRequest>().Writer;
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public async Task TearDown() => await _db.DisposeAsync();
|
||||
|
||||
[Test]
|
||||
public async Task Replace_PutResponse_Should_Carry_Watermark_And_Graphics_Names()
|
||||
{
|
||||
int scheduleId = await SeedReferences();
|
||||
|
||||
var handler = new ReplaceProgramScheduleItemsHandler(_db.Factory, _worker);
|
||||
Either<BaseError, IEnumerable<ProgramScheduleItemViewModel>> result = await handler.Handle(
|
||||
new ReplaceProgramScheduleItems(scheduleId, [ItemWithGraphics(0)]),
|
||||
CancellationToken.None);
|
||||
|
||||
result.IsRight.ShouldBeTrue(result.LeftToSeq().HeadOrNone().Match(e => e.Value, () => "unknown"));
|
||||
|
||||
// Force enumeration exactly like the controller (`items.Select(...).ToList()`) — this is what threw the NRE.
|
||||
List<ProgramScheduleItemViewModel> items = result.RightToSeq().Head().ToList();
|
||||
ProgramScheduleItemViewModel item = items.Single();
|
||||
item.Watermarks.Select(w => w.Name).ShouldBe(["WM One", "WM Two"]);
|
||||
item.GraphicsElements.Count.ShouldBe(2);
|
||||
item.GraphicsElements.Select(g => g.Name).ShouldAllBe(n => !string.IsNullOrWhiteSpace(n));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Add_PostResponse_Should_Carry_Watermark_And_Graphics_Names()
|
||||
{
|
||||
int scheduleId = await SeedReferences();
|
||||
|
||||
var handler = new AddProgramScheduleItemHandler(_db.Factory, _worker);
|
||||
Either<BaseError, ProgramScheduleItemViewModel> result = await handler.Handle(
|
||||
AddItemWithGraphics(scheduleId),
|
||||
CancellationToken.None);
|
||||
|
||||
result.IsRight.ShouldBeTrue(result.LeftToSeq().HeadOrNone().Match(e => e.Value, () => "unknown"));
|
||||
|
||||
ProgramScheduleItemViewModel item = result.RightToSeq().Head();
|
||||
item.Watermarks.Select(w => w.Name).ShouldBe(["WM One", "WM Two"]);
|
||||
item.GraphicsElements.Count.ShouldBe(2);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Get_Should_Return_Items_In_Index_Order_Regardless_Of_Id_Order()
|
||||
{
|
||||
int scheduleId;
|
||||
await using (TvContext ctx = _db.CreateContext())
|
||||
{
|
||||
var schedule = new ProgramSchedule
|
||||
{
|
||||
Name = "Shuffled", Items = [], Playouts = [], ProgramScheduleAlternates = []
|
||||
};
|
||||
ctx.ProgramSchedules.Add(schedule);
|
||||
await ctx.SaveChangesAsync();
|
||||
scheduleId = schedule.Id;
|
||||
|
||||
// Insert with ids deliberately out of Index order: id 10 -> Index 2, id 11 -> Index 0, id 12 -> Index 1.
|
||||
// Before the fix the GET returned id order (2, 0, 1); after, it must return Index order (0, 1, 2).
|
||||
ctx.ProgramScheduleItems.Add(OneItemRow(10, scheduleId, index: 2));
|
||||
ctx.ProgramScheduleItems.Add(OneItemRow(11, scheduleId, index: 0));
|
||||
ctx.ProgramScheduleItems.Add(OneItemRow(12, scheduleId, index: 1));
|
||||
await ctx.SaveChangesAsync();
|
||||
}
|
||||
|
||||
var getHandler = new GetProgramScheduleItemsHandler(_db.Factory);
|
||||
List<ProgramScheduleItemViewModel> items =
|
||||
await getHandler.Handle(new GetProgramScheduleItems(scheduleId), CancellationToken.None);
|
||||
|
||||
items.Select(i => i.Index).ShouldBe([0, 1, 2]);
|
||||
}
|
||||
|
||||
private static ProgramScheduleItemOne OneItemRow(int id, int scheduleId, int index) =>
|
||||
new()
|
||||
{
|
||||
Id = id,
|
||||
ProgramScheduleId = scheduleId,
|
||||
Index = index,
|
||||
CollectionType = CollectionType.Collection,
|
||||
CollectionId = 1,
|
||||
PlaybackOrder = PlaybackOrder.Shuffle,
|
||||
ProgramScheduleItemWatermarks = [],
|
||||
ProgramScheduleItemGraphicsElements = []
|
||||
};
|
||||
|
||||
private async Task<int> SeedReferences()
|
||||
{
|
||||
await using TvContext ctx = _db.CreateContext();
|
||||
ctx.Collections.Add(new Collection { Id = 1, Name = "First Collection" });
|
||||
ctx.ChannelWatermarks.Add(new ChannelWatermark { Id = 1, Name = "WM One" });
|
||||
ctx.ChannelWatermarks.Add(new ChannelWatermark { Id = 2, Name = "WM Two" });
|
||||
ctx.GraphicsElements.Add(new GraphicsElement { Id = 1, Name = "GE One", Path = "/ge/1", Kind = GraphicsElementKind.Image });
|
||||
ctx.GraphicsElements.Add(new GraphicsElement { Id = 2, Name = "GE Two", Path = "/ge/2", Kind = GraphicsElementKind.Image });
|
||||
|
||||
var schedule = new ProgramSchedule
|
||||
{
|
||||
Name = "WM Schedule", Items = [], Playouts = [], ProgramScheduleAlternates = []
|
||||
};
|
||||
ctx.ProgramSchedules.Add(schedule);
|
||||
await ctx.SaveChangesAsync();
|
||||
return schedule.Id;
|
||||
}
|
||||
|
||||
private static ReplaceProgramScheduleItem ItemWithGraphics(int index) =>
|
||||
new(
|
||||
index, StartType.Dynamic, StartTime: null, FixedStartTimeBehavior: null, PlayoutMode.One,
|
||||
CollectionType.Collection, CollectionId: 1, MultiCollectionId: null, SmartCollectionId: null,
|
||||
RerunCollectionId: null, MediaItemId: null, PlaylistId: null, SearchTitle: null, SearchQuery: null,
|
||||
PlaybackOrder: PlaybackOrder.Shuffle, MarathonGroupBy: MarathonGroupBy.None,
|
||||
MarathonShuffleGroups: false, MarathonShuffleItems: false, MarathonBatchSize: null,
|
||||
FillWithGroupMode: FillWithGroupMode.None, MultipleMode: MultipleMode.Count, MultipleCount: null,
|
||||
PlayoutDuration: null, TailMode: TailMode.None, DiscardToFillAttempts: null, CustomTitle: null,
|
||||
GuideMode: GuideMode.Normal, PreRollFillerId: null, MidRollFillerId: null, PostRollFillerId: null,
|
||||
TailFillerId: null, FallbackFillerId: null, WatermarkIds: [1, 2], GraphicsElementIds: [1, 2],
|
||||
PreferredAudioLanguageCode: null, PreferredAudioTitle: null, PreferredSubtitleLanguageCode: null,
|
||||
SubtitleMode: null);
|
||||
|
||||
private static AddProgramScheduleItem AddItemWithGraphics(int scheduleId) =>
|
||||
new(
|
||||
scheduleId, StartType.Dynamic, StartTime: null, FixedStartTimeBehavior: null, PlayoutMode.One,
|
||||
CollectionType.Collection, CollectionId: 1, MultiCollectionId: null, SmartCollectionId: null,
|
||||
RerunCollectionId: null, MediaItemId: null, PlaylistId: null, SearchTitle: null, SearchQuery: null,
|
||||
PlaybackOrder: PlaybackOrder.Shuffle, MarathonGroupBy: MarathonGroupBy.None,
|
||||
MarathonShuffleGroups: false, MarathonShuffleItems: false, MarathonBatchSize: null,
|
||||
FillWithGroupMode: FillWithGroupMode.None, MultipleMode: MultipleMode.Count, MultipleCount: null,
|
||||
PlayoutDuration: null, TailMode: TailMode.None, DiscardToFillAttempts: null, CustomTitle: null,
|
||||
GuideMode: GuideMode.Normal, PreRollFillerId: null, MidRollFillerId: null, PostRollFillerId: null,
|
||||
TailFillerId: null, FallbackFillerId: null, WatermarkIds: [1, 2], GraphicsElementIds: [1, 2],
|
||||
PreferredAudioLanguageCode: null, PreferredAudioTitle: null, PreferredSubtitleLanguageCode: null,
|
||||
SubtitleMode: null);
|
||||
}
|
||||
@@ -197,6 +197,17 @@ items), index items from **array order** in the request body rather than trustin
|
||||
index/order field. Exemplar: `ReplaceScheduleItemsRequest.ToCommand(scheduleId)` —
|
||||
`Items.Select((item, index) => item.ToReplaceCommand(index))`.
|
||||
|
||||
**Project the write-path response through the same include chain the GET uses — never off the
|
||||
freshly-built graph.** After `SaveChanges`, a command's entities carry only the foreign-key ids you set
|
||||
(e.g. `ProgramScheduleItemWatermark.WatermarkId`); their reference navs are null, and any mapper that
|
||||
dereferences one unguarded throws an NRE that surfaces as a 500. Reload with the read-side includes
|
||||
before mapping. Exemplars: `ReplaceProgramScheduleItemsHandler` / `AddProgramScheduleItemHandler` reload
|
||||
via `ProgramScheduleItemQueryExtensions.IncludeScheduleItemDetails()` (the one include chain shared with
|
||||
`GetProgramScheduleItemsHandler`). Also beware LanguageExt `Map` is **lazy** — returning
|
||||
`items.Map(ProjectToViewModel)` defers the projection, so a test that only checks `.IsRight` won't catch
|
||||
the NRE; the controller's `.ToList()`/serialization does (regression: `ScheduleItemWriteProjectionTests`).
|
||||
GET handlers that feed an ordered list must also `.OrderBy(i => i.Index)` — id order is not index order.
|
||||
|
||||
## 8. Known API warts (don't "fix" without discussion — they're deliberate synthesized rows)
|
||||
|
||||
`GET /api/blocks` and `GET /api/templates` (via `GetAllBlocksHandler` / `GetAllTemplatesHandler` in
|
||||
|
||||
Reference in New Issue
Block a user