Skip to content

Commit 03c26d8

Browse files
committed
mark mergeExamples as @internal and add tests
1 parent c76a111 commit 03c26d8

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

express-zod-api/src/json-schema-helpers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ export const processPropertyNames = (
7979
if (!isOptional) requiredKeys.push(...keys);
8080
};
8181

82-
const mergeExamples = (
82+
/** @internal */
83+
export const mergeExamples = (
8384
flat: FlattenObjectSchema,
8485
entry: z.core.JSONSchema.BaseSchema,
8586
isOptional: boolean,

express-zod-api/tests/json-schema-helpers.spec.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { z } from "zod";
22
import {
33
flattenIO,
44
isJsonObjectSchema,
5+
mergeExamples,
56
propsMerger,
67
canMerge,
78
nestOptional,
@@ -199,6 +200,55 @@ describe("JSON Schema helpers", () => {
199200
});
200201
});
201202

203+
describe("mergeExamples()", () => {
204+
test("should do nothing when entry has no examples", () => {
205+
const flat = { type: "object" as const, properties: {} };
206+
mergeExamples(flat, { type: "string" }, false);
207+
expect(flat).toEqual({ type: "object", properties: {} });
208+
});
209+
210+
test("should concatenate examples when optional", () => {
211+
const flat = {
212+
type: "object" as const,
213+
properties: {},
214+
examples: [{ a: 1 }],
215+
};
216+
mergeExamples(flat, { examples: [{ b: 2 }, { c: 3 }] }, true);
217+
expect(flat.examples).toEqual([{ a: 1 }, { b: 2 }, { c: 3 }]);
218+
});
219+
220+
test("should initialize examples when optional and flat has none", () => {
221+
const flat: { type: "object"; properties: {}; examples?: unknown[] } = {
222+
type: "object",
223+
properties: {},
224+
};
225+
mergeExamples(flat, { examples: [{ a: 1 }] }, true);
226+
expect(flat.examples).toEqual([{ a: 1 }]);
227+
});
228+
229+
test("should produce combinations when required", () => {
230+
const flat = {
231+
type: "object" as const,
232+
properties: {},
233+
examples: [{ a: 1 }],
234+
};
235+
mergeExamples(flat, { examples: [{ b: 2 }, { b: 3 }] }, false);
236+
expect(flat.examples).toEqual([
237+
{ a: 1, b: 2 },
238+
{ a: 1, b: 3 },
239+
]);
240+
});
241+
242+
test("should handle required with no prior examples", () => {
243+
const flat: { type: "object"; properties: {}; examples?: unknown[] } = {
244+
type: "object",
245+
properties: {},
246+
};
247+
mergeExamples(flat, { examples: [{ a: 1 }] }, false);
248+
expect(flat.examples).toEqual([{ a: 1 }]);
249+
});
250+
});
251+
202252
describe("pullRequestExamples()", () => {
203253
test("should return empty array for empty properties", () => {
204254
expect(pullRequestExamples({ type: "object", properties: {} })).toEqual(

0 commit comments

Comments
 (0)