fix: address Copilot review round 6 — additionalProperties, annotations, prototype-safe records

- rootUnion: decline a union that adds fields under a restrictive root
  `additionalProperties`. The keyword constrains what its SIBLING
  `properties` does not name, so the original schema admits none of the
  branch fields; flattening moves them beside the keyword, where they read as
  allowed and the form would submit what the schema forbids.
- `default` joins the annotation set: it constrains nothing, so two
  declarations suggesting different initial values no longer conflict.
- Property records are read with `Object.hasOwn` and built with
  `fromEntries`/`defineProperty`. `properties` is a JSON record, so
  `constructor` is a legal argument name that `in` reported as a collision
  the root never made, and `__proto__` is one that assignment would drop
  into the legacy prototype setter instead of keeping as a field.
- SchemaForm: a value survives a branch switch unless BOTH branches declare
  the name. A root argument the outgoing branch merely specialized is still a
  root argument in a branch that inherits it, so dropping it erased a valid
  value.

Signed-off-by: cliffhall <cliff@futurescale.com>
This commit is contained in:
cliffhall 2026-08-26 02:56:55 -04:00
commit c15e30414f
2 changed files with 181 additions and 17 deletions

View file

@ -2149,6 +2149,39 @@ describe("SchemaForm multiline strings (#2042)", () => {
expect(onChange).toHaveBeenCalledWith({});
});
it("keeps a root value the incoming branch merely inherits", async () => {
const user = userEvent.setup();
const onChange = vi.fn();
const schema: InspectorFormSchema = {
type: "object",
properties: { count: {} },
anyOf: [
{
type: "object",
title: "A",
properties: { count: { type: "number", title: "Count" } },
},
{
type: "object",
title: "B",
properties: { other: { type: "string", title: "Other" } },
},
],
};
renderWithMantine(
<SchemaForm
schema={schema}
values={{ count: 3 }}
onChange={onChange}
/>,
);
await user.click(screen.getByRole("textbox", { name: /Variant/ }));
await user.click(screen.getByRole("option", { name: "B" }));
// Branch B does not redeclare `count`, so it is a root argument there —
// dropping it would erase a value the schema still accepts.
expect(onChange).toHaveBeenCalledWith({ count: 3 });
});
it("renders no picker for a single-branch union but still shows its fields", () => {
const schema: InspectorFormSchema = {
type: "object",

View file

@ -694,20 +694,23 @@ export function SchemaForm({
if (!nextBranch) return;
setBranchIndex(nextIndex);
const nextProperties = nextBranch.schema.properties ?? {};
// Only what the *base* contributed is carried across — a value the outgoing
// branch declared belongs to that branch's shape, and a name the two
// branches type differently would arrive as the wrong type entirely (a `3`
// typed into branch A's number field landing in branch B's checkbox). A
// field the incoming branch pins to a `const` is likewise not carried: the
// branches of a discriminated union share the discriminator's *name* and
// disagree about its value.
// A value is carried unless it belonged to the outgoing branch's own shape:
// a name **both** branches declare may be typed differently by each, so a
// `3` typed into branch A's number field must not arrive in branch B's
// checkbox. A name only the outgoing branch specialized still survives when
// the incoming branch merely inherits the root's declaration — it is a root
// argument there, and dropping it would erase a valid value. A field the
// incoming branch pins to a `const` is never carried: the branches of a
// discriminated union share the discriminator's *name* and disagree about
// its value.
const outgoing = new Set(activeBranch?.declaredFields ?? []);
const incoming = new Set(nextBranch.declaredFields);
const carried: Record<string, unknown> = {};
for (const [name, fieldSchema] of Object.entries(nextProperties)) {
if (
values[name] !== undefined &&
fieldSchema.const === undefined &&
!outgoing.has(name)
!(outgoing.has(name) && incoming.has(name))
) {
carried[name] = values[name];
}