Skip to content

Commit 08950ad

Browse files
fix(merge): replace discriminated union fields when the variant changes (#36)
The runtime merge recursed into a union's typename-keyed shape, which matches no real field, so when a field's variant changed the old variant's fields stayed in place and the new variant's never landed. Treat union fields atomically and replace them wholesale. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent a2dd9ac commit 08950ad

3 files changed

Lines changed: 72 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'fetchium': patch
3+
---
4+
5+
Fix the runtime merge so a discriminated union field is replaced when its variant changes. Previously the merge recursed into the union's typename-keyed shape, which matches no real field, so a changed variant left the old variant's fields in place and the new variant's fields never landed. Union fields are now replaced wholesale.

‎packages/fetchium/src/__tests__/branch-fixes.test.ts‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,66 @@ describe('Branch Fixes', () => {
117117
});
118118
});
119119

120+
it('should replace a discriminated union field when its variant changes', async () => {
121+
const { client, mockFetch } = getClient();
122+
123+
const QueuedState = t.object({
124+
__typename: t.typename('QueuedState'),
125+
queuedAt: t.string,
126+
});
127+
const RunningState = t.object({
128+
__typename: t.typename('RunningState'),
129+
progress: t.number,
130+
});
131+
132+
class Job extends Entity {
133+
__typename = t.typename('Job');
134+
id = t.id;
135+
state = t.union(QueuedState, RunningState);
136+
}
137+
138+
mockFetch.get('/jobs/[id]', {
139+
job: {
140+
__typename: 'Job',
141+
id: '1',
142+
state: { __typename: 'QueuedState', queuedAt: '2026-06-10T00:00:00Z' },
143+
},
144+
});
145+
146+
await testWithClient(client, async () => {
147+
class GetJob extends RESTQuery {
148+
params = { id: t.id };
149+
path = `/jobs/${this.params.id}`;
150+
result = { job: t.entity(Job) };
151+
}
152+
153+
const relay = fetchQuery(GetJob, { id: '1' });
154+
const result = await relay;
155+
156+
expect((result.job.state as any).__typename).toBe('QueuedState');
157+
expect((result.job.state as any).queuedAt).toBe('2026-06-10T00:00:00Z');
158+
159+
// Variant flips: the job starts running. The new state has a different
160+
// __typename and a disjoint field set.
161+
mockFetch.get('/jobs/[id]', {
162+
job: {
163+
__typename: 'Job',
164+
id: '1',
165+
state: { __typename: 'RunningState', progress: 42 },
166+
},
167+
});
168+
169+
const result2 = await relay.value!.__refetch();
170+
171+
// The union field must be replaced wholesale, not merged: the new
172+
// variant's discriminator and fields are present, and no field from
173+
// the old variant survives.
174+
expect((result2.job.state as any).__typename).toBe('RunningState');
175+
expect((result2.job.state as any).progress).toBe(42);
176+
expect('queuedAt' in (result2.job.state as any)).toBe(false);
177+
});
178+
});
179+
120180
it('should handle nullable nested object with null value', async () => {
121181
const { client, mockFetch } = getClient();
122182
class Profile extends Entity {

‎packages/fetchium/src/applyEntities.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type { QueryClient } from './QueryClient.js';
1111
import type { EntityInstance } from './EntityInstance.js';
1212
import type { ParseContext, ParsedEntity } from './parseEntities.js';
1313
import { FormattedValue, ValidatorDef } from './typeDefs.js';
14+
import { Mask } from './types.js';
1415
import { createLiveCollection, LiveCollectionBinding } from './LiveCollection.js';
1516
import { PROXY_ID } from './proxyId.js';
1617

@@ -206,7 +207,12 @@ function mergeFields(
206207
} else {
207208
const newVal = data[fieldKey];
208209
const oldVal = existingData[fieldKey];
209-
if (isPlainObject(newVal) && isPlainObject(oldVal)) {
210+
// Replace a union wholesale instead of merging by field, otherwise a
211+
// changed variant keeps the old variant's fields. Unlike entity unions,
212+
// there is no partial-update path to preserve here: a partial variant
213+
// payload fails validation, so every update carries the full variant.
214+
const isUnion = propShape instanceof ValidatorDef && (propShape.mask & Mask.UNION) !== 0;
215+
if (!isUnion && isPlainObject(newVal) && isPlainObject(oldVal)) {
210216
const nestedShape =
211217
propShape instanceof ValidatorDef && propShape.shape !== undefined
212218
? (propShape.shape as Record<string, unknown>)

0 commit comments

Comments
 (0)