Skip to content
This repository was archived by the owner on Nov 4, 2026. It is now read-only.

Commit 6214db6

Browse files
os-teslaclaude
andauthored
fix(components): header-bar's rightContent slot stops leaking a falsy number (#9163)
`HeaderBarSchema.rightContent` is declared `SchemaNode` on both published faces, so `0` is authorable. The renderer guarded the slot with an `&&` chain whose left operand is the raw slot value, and with `rightContent: 0` that chain evaluates to the number `0`, which React renders — so the header painted a stray "0" beside its chrome. Ported byte-for-byte from objectui#8331, which measured and settled the identical trap one slot over on `DataTableSchema.emptyAction`: the `&&` chain becomes a ternary yielding `null`. The truthiness leg stays rather than becoming nullish — both land on the same rendering today, and truthiness is the rule that keeps this slot's answer independent of `toRenderableSchema`. Only the falsy numbers leaked: `0` and `-0` painted "0", `NaN` painted "NaN". `false` and `''` were already correct and cannot discriminate the two worlds, so they are pinned under a name that says so. Not fixed by objectui#8908: `&&` short-circuits, so the chain produces the raw `0` before the bridge is ever called. Claude-Session: https://claude.ai/code/session_01UzHd6hDYatoDn17BuwKxnZ Co-authored-by: Claude <noreply@anthropic.com>
1 parent 8e74b27 commit 6214db6

3 files changed

Lines changed: 269 additions & 1 deletion

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
'@object-ui/components': patch
3+
---
4+
5+
`ui:header-bar` no longer paints a stray `"0"` beside the header chrome when
6+
`rightContent` is authored as a falsy number (objectui#9033).
7+
8+
`HeaderBarSchema.rightContent` is declared `SchemaNode` on both published faces — the
9+
interface in `@object-ui/types`, and its Zod mirror, whose node union carries a `z.number()`
10+
arm — so `0` is an authorable value for the slot. The renderer guarded it with an `&&` chain
11+
whose left operand is the raw slot value, and with `rightContent: 0` that chain evaluates to
12+
the **number** `0`, which React renders. Measured: `{ type: 'header-bar', title: 'H',
13+
rightContent: 0 }` painted `"Toggle Sidebar0"`.
14+
15+
This is the numeric-falsy JSX trap objectui#8331 measured and closed one slot over on
16+
`DataTableSchema.emptyAction`, and the fix is that same shape rather than a second one: the
17+
`&&` chain becomes a ternary yielding `null`. The **truthiness** leg stays rather than
18+
becoming nullish. Both land on the same rendering today — since objectui#8908
19+
`toRenderableSchema` maps a falsy primitive onto nothing rather than onto its `String` form
20+
— so the choice is about which rule the slot states, and truthiness is the rule that keeps
21+
this slot's answer independent of the bridge. That independence is exactly what kept the
22+
sibling slot out of the defect while the bridge was wrong.
23+
24+
Narrower than it may read, and stated precisely so the repair is not judged by the wrong
25+
row: only the falsy **numbers** leaked. `0` and `-0` painted the character `0`, `NaN` painted
26+
the three characters `NaN`. `false` and `''` were already correct — React ignores `false` as
27+
a child — and cannot discriminate the two worlds.
28+
29+
Adjacent but **not** this defect: objectui#8908 repaired the bridge so a falsy primitive
30+
renders nothing. That repair does not reach this line and never could, because `&&`
31+
short-circuits and the chain produces the raw `0` before any bridge is called. Same symptom,
32+
different mechanism.
33+
34+
The declaration is untouched — objectui#7105 ruled node slots relax the renderer rather than
35+
narrow the declaration, and a `typeof === 'object'` test here would silently drop a bare
36+
string the slot renders as its own text.
37+
38+
Scored `patch`: no new capability, a slot that painted a character it was never asked to
39+
paint stops painting it.
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
/**
2+
* ObjectUI
3+
* Copyright (c) 2024-present ObjectStack Inc.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
/**
10+
* `ui:header-bar`'s `rightContent` slot no longer leaks a stray `"0"`
11+
* (objectui#9033) — the numeric-falsy JSX trap objectui#8331 measured and
12+
* closed one slot over on `DataTableSchema.emptyAction`.
13+
*
14+
* ## The mechanism, and why it is not objectui#8908
15+
*
16+
* `HeaderBarSchema.rightContent` is declared `SchemaNode` on BOTH published
17+
* faces — the interface in `@object-ui/types`, and its zod mirror, whose node
18+
* union carries a `z.number()` arm — so `0` is an authorable value for it.
19+
* `theValidatorAcceptsZero` below asserts that from the published validator
20+
* rather than asserting it in prose, because every row here is worthless if
21+
* the value is unreachable.
22+
*
23+
* The renderer guarded the slot with an `&&` chain whose left operand is the
24+
* raw slot value. With `rightContent: 0` that chain evaluates to the NUMBER
25+
* `0`, and React renders numbers — so the header painted a stray `"0"`.
26+
*
27+
* objectui#8908 repaired `toRenderableSchema` so a falsy primitive becomes
28+
* nothing rather than its `String` form. That repair does ⛔ NOT reach this
29+
* line and never could: `&&` short-circuits, so the bridge is not called at
30+
* all for a falsy value — the chain has already produced the raw `0` before
31+
* any bridge runs. Same symptom from the user's seat, different mechanism,
32+
* different fix. `theBridgeIsNotWhatFixesThis` pins that distinction directly
33+
* so a future reader cannot conclude the bridge covers this slot.
34+
*
35+
* ## Why `false` and `''` are present but are ⛔ NOT evidence
36+
*
37+
* React ignores `false` as a child, and `''` contributes no characters. Both
38+
* rows were GREEN before the repair and are green after it, so neither can
39+
* discriminate the two worlds. They sit in `rowsThatCannotDiscriminate` under
40+
* that name, kept only so a later reader does not "extend coverage" by adding
41+
* them as if they proved something.
42+
*
43+
* The rows that DO discriminate are the falsy NUMBERS, and only they:
44+
* `0` and `-0` (both of which React prints as the single character `0`) and
45+
* `NaN` (which prints the three characters `NaN`).
46+
*
47+
* ## Why the assertions read whole text rather than query for a node
48+
*
49+
* The leak is a bare text node with no element of its own — there is nothing
50+
* to `querySelector`. The instrument is therefore the header's entire
51+
* `textContent`, compared against the baseline the same harness produces for
52+
* `rightContent: undefined`. `theInstrumentSeesSlotText` is the lit control on
53+
* that instrument: with `rightContent: 42` the text MUST grow by `42`, so a
54+
* "no stray characters" row cannot pass because the instrument went blind.
55+
*
56+
* ## The `SidebarProvider` host
57+
*
58+
* `header-bar` renders `SidebarTrigger`, which calls `useSidebar()` and THROWS
59+
* without a provider. A caught throw renders markup that reads as a clean pass
60+
* for every row below, so `theHarnessRendersTheRealHeader` asserts the real
61+
* header is mounted rather than assuming it.
62+
*/
63+
64+
import { describe, it, expect, afterEach } from 'vitest';
65+
import { render, cleanup } from '@testing-library/react';
66+
import { ComponentRegistry } from '@object-ui/core';
67+
// Registered at module scope, NOT in a `beforeAll`: there the cold transform is
68+
// billed to `hookTimeout`, which is narrower than the timeout it replaces
69+
// (objectui#3010 / #3021, and `object-ui/no-dynamic-import-in-test-hook`).
70+
import '../renderers';
71+
import { SidebarProvider } from '../ui';
72+
import { toRenderableSchema } from '@object-ui/react';
73+
import { HeaderBarSchema as HeaderBarSchemaZod } from '@object-ui/types/zod';
74+
75+
afterEach(() => cleanup());
76+
77+
/** The header's own chrome text, contributed by `SidebarTrigger`'s sr-only label. */
78+
const CHROME = 'Toggle Sidebar';
79+
80+
function renderRightContent(rightContent: unknown) {
81+
const C = ComponentRegistry.get('header-bar') as React.ComponentType<any>;
82+
const { container } = render(
83+
<SidebarProvider>
84+
<C schema={{ type: 'header-bar', title: 'H', rightContent }} />
85+
</SidebarProvider>,
86+
);
87+
const header = container.querySelector('header');
88+
if (!header) throw new Error('no <header> — the harness did not mount the real renderer');
89+
return header.textContent ?? '';
90+
}
91+
92+
/** `rightContent` omitted entirely: the chrome, and nothing else. */
93+
function renderBaseline() {
94+
const C = ComponentRegistry.get('header-bar') as React.ComponentType<any>;
95+
const { container } = render(
96+
<SidebarProvider>
97+
<C schema={{ type: 'header-bar', title: 'H' }} />
98+
</SidebarProvider>,
99+
);
100+
const header = container.querySelector('header');
101+
if (!header) throw new Error('no <header> — the harness did not mount the real renderer');
102+
return header.textContent ?? '';
103+
}
104+
105+
describe('ui:header-bar rightContent numeric-falsy leak (objectui#9033)', () => {
106+
describe('harness controls', () => {
107+
it('theHarnessRendersTheRealHeader — a real <header>, not error-boundary markup', () => {
108+
// `useSidebar()` throws without `SidebarProvider`, and the caught throw is
109+
// clean markup that would make every "no stray characters" row below pass
110+
// for entirely the wrong reason.
111+
expect(renderBaseline()).toContain(CHROME);
112+
});
113+
114+
it('theBaselineIsTheChromeAlone', () => {
115+
expect(renderBaseline()).toBe(CHROME);
116+
});
117+
118+
it('theInstrumentSeesSlotText — LIT CONTROL: a truthy number DOES reach the text', () => {
119+
// Green in both worlds BY DESIGN. Without it, a slot that rendered
120+
// nothing at all — or a `textContent` read that stopped working — would
121+
// make every leak row below green while measuring nothing.
122+
expect(renderRightContent(42)).toBe(`${CHROME}42`);
123+
});
124+
125+
it('theValidatorAcceptsZero — the leaked value is AUTHORABLE, not hypothetical', () => {
126+
// The published zod face for this slot is `SchemaNodeSchema.optional()`,
127+
// and that union carries a `z.number()` arm. If this row ever goes red the
128+
// slot stopped admitting numbers and the rows below stopped being about
129+
// anything — ⛔ that is a declaration change, not a licence to delete them
130+
// (objectui#7105: node slots relax the RENDERER).
131+
const parsed = HeaderBarSchemaZod.safeParse({ type: 'header-bar', title: 'H', rightContent: 0 });
132+
expect(parsed.success).toBe(true);
133+
});
134+
});
135+
136+
describe('the falsy NUMBERS — the only rows that discriminate', () => {
137+
it('rightContent: 0 renders the chrome and no stray "0"', () => {
138+
// RED before the repair: `"Toggle Sidebar0"`.
139+
expect(renderRightContent(0)).toBe(CHROME);
140+
});
141+
142+
it('rightContent: -0 renders the chrome and no stray "0"', () => {
143+
// React prints `-0` as the single character `0`, same as `0`.
144+
expect(renderRightContent(-0)).toBe(CHROME);
145+
});
146+
147+
it('rightContent: NaN renders the chrome and no stray "NaN"', () => {
148+
// The loudest row of the three: `NaN` paints three characters, not one.
149+
expect(renderRightContent(NaN)).toBe(CHROME);
150+
expect(renderRightContent(NaN)).not.toContain('NaN');
151+
});
152+
});
153+
154+
describe('live controls — unchanged by the repair, asserted in the SAME run', () => {
155+
it('rightContent: 42 still renders 42', () => {
156+
expect(renderRightContent(42)).toBe(`${CHROME}42`);
157+
});
158+
159+
it("rightContent: 'txt' still renders txt", () => {
160+
expect(renderRightContent('txt')).toBe(`${CHROME}txt`);
161+
});
162+
163+
it('rightContent: undefined renders the baseline chrome only', () => {
164+
expect(renderRightContent(undefined)).toBe(CHROME);
165+
});
166+
167+
it('an authored NODE still renders — the slot is still a node slot', () => {
168+
// The repair must not have narrowed the slot to primitives-only.
169+
expect(renderRightContent({ type: 'text', content: 'node' })).toContain('node');
170+
});
171+
});
172+
173+
describe('rowsThatCannotDiscriminate — ⛔ green before the repair too, kept only to say so', () => {
174+
it('rightContent: false was ALREADY correct — React ignores `false` as a child', () => {
175+
expect(renderRightContent(false)).toBe(CHROME);
176+
});
177+
178+
it("rightContent: '' was ALREADY correct — an empty string contributes no characters", () => {
179+
expect(renderRightContent('')).toBe(CHROME);
180+
});
181+
});
182+
183+
describe('the mechanism, pinned so it is not confused with objectui#8908', () => {
184+
it('theBridgeIsNotWhatFixesThis — `toRenderableSchema` already mapped 0 onto nothing', () => {
185+
// The bridge has been behaviour-preserving for falsy primitives since
186+
// objectui#8908. It was ALREADY correct while this slot leaked, because
187+
// `&&` short-circuits and never calls it for a falsy value. So a reader
188+
// who finds the bridge correct must ⛔ not conclude this slot is covered:
189+
// the guard shape is what covers it.
190+
expect(toRenderableSchema(0)).toBeUndefined();
191+
expect(toRenderableSchema(NaN)).toBeUndefined();
192+
expect(toRenderableSchema(42)).toBe('42');
193+
});
194+
});
195+
});

‎packages/components/src/renderers/navigation/header-bar.tsx‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,41 @@ ComponentRegistry.register('header-bar',
124124
{schema.actions?.map((action, idx) => (
125125
<SchemaRenderer key={idx} schema={toRenderableSchema(action)} />
126126
))}
127-
{schema.rightContent && <SchemaRenderer schema={toRenderableSchema(schema.rightContent)} />}
127+
{/* The numeric-falsy JSX trap. `HeaderBarSchema.rightContent` is
128+
declared `SchemaNode` on both published faces — the interface in
129+
`@object-ui/types` and its zod mirror, whose node union carries a
130+
`z.number()` arm — so `0` is an AUTHORABLE value here. The `&&`
131+
chain this replaced evaluated to the number `0` itself, and React
132+
renders numbers, so `{ type: 'header-bar', rightContent: 0 }`
133+
painted a stray "0" beside the header chrome. A ternary yields
134+
`null` instead. Byte-for-byte the shape objectui#8331 measured and
135+
settled one slot over on `DataTableSchema.emptyAction`; ⛔ not a
136+
second shape for the same trap.
137+
138+
⚠️ `false` and `''` were ALREADY correct — React ignores `false` as
139+
a child and `''` paints nothing — so neither can tell the two
140+
worlds apart. Only the falsy NUMBERS discriminate: `0`, `-0`, and
141+
`NaN`, which painted the three characters "NaN".
142+
143+
The TRUTHINESS leg stays; ⛔ it is not swapped for a nullish one.
144+
Both land on the same rendering today — since objectui#8908
145+
`toRenderableSchema` maps a falsy primitive onto nothing rather
146+
than onto its `String` form — so the choice is about which rule
147+
this slot STATES, and truthiness is the rule that makes the slot's
148+
answer independent of the bridge. That independence is exactly what
149+
kept the sibling slot out of the defect while the bridge was wrong,
150+
and objectui#8331 kept the leg for that reason rather than letting
151+
it vanish as tidying.
152+
153+
⛔ Do not narrow the declaration to close this: objectui#7105 ruled
154+
node slots RELAX THE RENDERER. A `typeof === 'object'` test here
155+
would silently drop a bare string, which this slot renders as its
156+
own text.
157+
158+
Pinned in `header-bar-right-content-numeric-falsy.test.tsx`. */}
159+
{schema.rightContent ? (
160+
<SchemaRenderer schema={toRenderableSchema(schema.rightContent)} />
161+
) : null}
128162
</div>
129163
</header>
130164
),

0 commit comments

Comments
 (0)