Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/introspection-nullable-type-union.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@sapiom/agent": patch
---

Fix `exampleFromJsonSchema` skeleton generation for JSON Schema `type` unions
that include `null` (e.g. `{ type: ["string", "null"] }` from Zod `.nullable()`).
Previously the array form fell through to `null`, so workflow input prefills
showed `null` instead of a type-appropriate placeholder.
44 changes: 44 additions & 0 deletions packages/agent/src/introspection.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { exampleFromJsonSchema } from "./introspection.js";

describe("exampleFromJsonSchema", () => {
it("prefers an author-declared example", () => {
expect(
exampleFromJsonSchema({
type: "string",
examples: ["from-author"],
}),
).toBe("from-author");
});

it("builds a string skeleton for nullable string types (type union array)", () => {
expect(exampleFromJsonSchema({ type: ["string", "null"] })).toBe("");
});

it("builds a number skeleton for nullable number types", () => {
expect(exampleFromJsonSchema({ type: ["number", "null"] })).toBe(0);
});

it("builds an integer skeleton for nullable integer types", () => {
expect(exampleFromJsonSchema({ type: ["integer", "null"] })).toBe(0);
});

it("builds a boolean skeleton for nullable boolean types", () => {
expect(exampleFromJsonSchema({ type: ["boolean", "null"] })).toBe(false);
});

it("returns null when null is the only type in the union", () => {
expect(exampleFromJsonSchema({ type: ["null"] })).toBeNull();
});

it("recurses into object properties with nullable scalar fields", () => {
expect(
exampleFromJsonSchema({
type: "object",
properties: {
name: { type: ["string", "null"] },
count: { type: ["integer", "null"] },
},
}),
).toEqual({ name: "", count: 0 });
});
});
6 changes: 5 additions & 1 deletion packages/agent/src/introspection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ function skeletonFromJsonSchema(schema: Record<string, unknown>): unknown {
return skeletonFromJsonSchema(branches[0]);
}

switch (schema.type) {
const type = Array.isArray(schema.type)
? (schema.type as string[]).find((t) => t !== "null") ?? "null"
: schema.type;

switch (type) {
case 'object': {
const props = (schema.properties ?? {}) as Record<string, Record<string, unknown>>;
const out: Record<string, unknown> = {};
Expand Down
Loading