Fix unit/product kind enums serializing as integers

Two bugs found during exploratory testing:

1. UnitKind and ProductKind enums were serialized as 0/1 instead of
   "Global"/"Family" because no global JSON converter was registered.
   Added JsonStringEnumConverter to ConfigureHttpJsonOptions so all
   enum responses (kind, category) serialize as strings. This also
   fixes UnitCategory coming back as a number.

2. units.svelte.ts triggered a Svelte 5 state_unsafe_mutation error
   because the `all` getter called load() (which mutates $state) from
   inside a $derived expression in QuantityInput. Wrapped the load()
   call in untrack() so the side-effect runs outside the reactive
   tracking context.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Josh Rogers
2026-05-14 19:37:16 -05:00
parent 68292c2906
commit b31ff77548
2 changed files with 9 additions and 1 deletions
+4
View File
@@ -1,5 +1,6 @@
using System.Security.Claims;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.RateLimiting;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.RateLimiting;
@@ -20,6 +21,9 @@ using YesChef.Api.Features.Units;
var builder = WebApplication.CreateBuilder(args);
builder.Services.ConfigureHttpJsonOptions(o =>
o.SerializerOptions.Converters.Add(new JsonStringEnumConverter()));
builder.Services.AddDbContext<YesChefDb>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
+5 -1
View File
@@ -1,3 +1,4 @@
import { untrack } from 'svelte';
import { api } from '$lib/api';
export type UnitKind = 'Global' | 'Family';
@@ -24,7 +25,10 @@ let error = $state<string | null>(null);
export const units = {
get all() {
if (cache === null && !loading) {
void load();
// Use untrack so the load() side-effect (which mutates $state) is
// not flagged as an illegal mutation when this getter is read inside
// a $derived expression.
untrack(() => void load());
}
return cache ?? [];
},