Skip to content

Commit 884a648

Browse files
committed
wip5 $ref
1 parent 9eb6133 commit 884a648

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

packages/typed-openapi/tests/openapi-schema-to-ts.test.ts

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const printTraversable = (traversable: t.F<any>, ctx: OpenapiSchemaConvertContex
4848
format: FORMAT = defaults.format,
4949
maxWidth: MAX_WIDTH = defaults.maxWidth,
5050
} = options;
51-
const out = t.foldWithIndex<string>((x, ix) => {
51+
const out = t.foldWithIndex<string | { $ref: string }>((x, ix) => {
5252
const { depth } = ix;
5353
const OFFSET = OFF + depth * 2;
5454
const JOIN = ",\n" + " ".repeat(depth + 1);
@@ -64,7 +64,7 @@ const printTraversable = (traversable: t.F<any>, ctx: OpenapiSchemaConvertContex
6464
const refSchema = ctx.refs.get(x.def["$ref"]);
6565
console.log({ x, ref: x.def["$ref"], refSchema });
6666
const refTraversable = openApiSchemaToTs({ schema: refSchema, ctx });
67-
return refTraversable.toType();
67+
return toType(refTraversable);
6868
}
6969
case x.tag === URI.eq:
7070
return jsonToString(x.def, options, ix);
@@ -161,7 +161,7 @@ test("empty object", () => {
161161
});
162162
test("object with properties", () => {
163163
expect(getSchemaBox({ type: "object", properties: { str: { type: "string" } } })).toMatchInlineSnapshot(
164-
`"{ 'str': string }"`,
164+
`"{ str: string }"`,
165165
);
166166
});
167167
test("object with properties nullable", () => {
@@ -177,7 +177,7 @@ test("object with properties nullable", () => {
177177
},
178178
},
179179
}),
180-
).toMatchInlineSnapshot(`"{ 'str': string, 'nb': number, 'nullable': (string | null) }"`);
180+
).toMatchInlineSnapshot(`"{ str: string, nb: number, nullable: (string | null) }"`);
181181
});
182182
test("object with all properties required", () => {
183183
// AllPropertiesRequired
@@ -187,7 +187,7 @@ test("object with all properties required", () => {
187187
properties: { str: { type: "string" }, nb: { type: "number" } },
188188
required: ["str", "nb"],
189189
}),
190-
).toMatchInlineSnapshot(`"{ 'str': string, 'nb': number }"`);
190+
).toMatchInlineSnapshot(`"{ str: string, nb: number }"`);
191191
});
192192
test("object with some optional properties", () => {
193193
// SomeOptionalProps
@@ -197,7 +197,7 @@ test("object with some optional properties", () => {
197197
properties: { str: { type: "string" }, nb: { type: "number" } },
198198
required: ["str"],
199199
}),
200-
).toMatchInlineSnapshot(`"{ 'str': string, 'nb'?: (number | undefined) }"`);
200+
).toMatchInlineSnapshot(`"{ str: string, nb?: (number | undefined) }"`);
201201
});
202202
test("object with nested property", () => {
203203
// ObjectWithNestedProp
@@ -215,13 +215,13 @@ test("object with nested property", () => {
215215
},
216216
},
217217
}),
218-
).toMatchInlineSnapshot(`"{ 'str': string, 'nb': number, 'nested': { 'nested_prop': boolean } }"`);
218+
).toMatchInlineSnapshot(`"{ str: string, nb: number, nested: { nested_prop: boolean } }"`);
219219
});
220220
test("object with additional properties", () => {
221221
// ObjectWithAdditionalPropsNb
222222
expect(
223223
getSchemaBox({ type: "object", properties: { str: { type: "string" } }, additionalProperties: { type: "number" } }),
224-
).toMatchInlineSnapshot(`"({ 'str': string } & Record<string, number>)"`);
224+
).toMatchInlineSnapshot(`"({ str: string } & Record<string, number>)"`);
225225
});
226226
test("object with nested record boolean", () => {
227227
// ObjectWithNestedRecordBoolean
@@ -231,7 +231,7 @@ test("object with nested record boolean", () => {
231231
properties: { str: { type: "string" } },
232232
additionalProperties: { type: "object", properties: { prop: { type: "boolean" } } },
233233
}),
234-
).toMatchInlineSnapshot(`"({ 'str': string } & Record<string, { 'prop': boolean }>)"`);
234+
).toMatchInlineSnapshot(`"({ str: string } & Record<string, { prop: boolean }>)"`);
235235
});
236236
test("array with object with nested property", () => {
237237
expect(
@@ -245,7 +245,7 @@ test("array with object with nested property", () => {
245245
},
246246
},
247247
}),
248-
).toMatchInlineSnapshot(`"({ 'str': string, 'nullable': (string | null) })[]"`);
248+
).toMatchInlineSnapshot(`"({ str: string, nullable: (string | null) })[]"`);
249249
});
250250
test("array with array", () => {
251251
expect(
@@ -269,17 +269,17 @@ test("object with enum", () => {
269269
enumprop: { type: "string", enum: ["aaa", "bbb", "ccc"] },
270270
},
271271
}),
272-
).toMatchInlineSnapshot(`"{ 'enumprop': ('aaa' | 'bbb' | 'ccc') }"`);
272+
).toMatchInlineSnapshot(`"{ enumprop: ("aaa" | "bbb" | "ccc") }"`);
273273
});
274274
test("string enum", () => {
275275
expect(getSchemaBox({ type: "string", enum: ["aaa", "bbb", "ccc"] })).toMatchInlineSnapshot(
276-
`"('aaa' | 'bbb' | 'ccc')"`,
276+
`"("aaa" | "bbb" | "ccc")"`,
277277
);
278278
});
279279
test("string enum", () => {
280280
// StringENum
281281
expect(getSchemaBox({ type: "string", enum: ["aaa", "bbb", "ccc"] })).toMatchInlineSnapshot(
282-
`"('aaa' | 'bbb' | 'ccc')"`,
282+
`"("aaa" | "bbb" | "ccc")"`,
283283
);
284284
});
285285
test("object with union", () => {
@@ -291,7 +291,7 @@ test("object with union", () => {
291291
union: { oneOf: [{ type: "string" }, { type: "number" }] },
292292
},
293293
}),
294-
).toMatchInlineSnapshot(`"{ 'union': (string | number) }"`);
294+
).toMatchInlineSnapshot(`"{ union: (string | number) }"`);
295295
});
296296
test("union", () => {
297297
expect(getSchemaBox({ oneOf: [{ type: "string" }, { type: "number" }] })).toMatchInlineSnapshot(
@@ -335,7 +335,7 @@ test("object with array union", () => {
335335
unionOrArrayOfUnion: { anyOf: [{ type: "string" }, { type: "number" }] },
336336
},
337337
}),
338-
).toMatchInlineSnapshot(`"{ 'unionOrArrayOfUnion': (string | number) }"`);
338+
).toMatchInlineSnapshot(`"{ unionOrArrayOfUnion: (string | number) }"`);
339339
});
340340
test("object with intersection", () => {
341341
// ObjectWithIntersection
@@ -346,11 +346,11 @@ test("object with intersection", () => {
346346
intersection: { allOf: [{ type: "string" }, { type: "number" }] },
347347
},
348348
}),
349-
).toMatchInlineSnapshot(`"{ 'intersection': (string & number) }"`);
349+
).toMatchInlineSnapshot(`"{ intersection: (string & number) }"`);
350350
});
351351
test("string enum", () => {
352352
expect(getSchemaBox({ type: "string", enum: ["aaa", "bbb", "ccc"] })).toMatchInlineSnapshot(
353-
`"('aaa' | 'bbb' | 'ccc')"`,
353+
`"("aaa" | "bbb" | "ccc")"`,
354354
);
355355
});
356356
test("number enum", () => {
@@ -361,7 +361,7 @@ test("number enum - single", () => {
361361
});
362362
test("enum", () => {
363363
expect(getSchemaBox({ enum: ["red", "amber", "green", null, 42, true] })).toMatchInlineSnapshot(
364-
`"('red' | 'amber' | 'green' | null | 42 | true)"`,
364+
`"("red" | "amber" | "green" | null | 42 | true)"`,
365365
);
366366
});
367367
test("object with array of object with properties", () => {
@@ -400,7 +400,7 @@ test("object with array of object with properties", () => {
400400
required: ["members"],
401401
}),
402402
).toMatchInlineSnapshot(
403-
`"{ 'members': ({ 'id': string, 'firstName'?: ((string | null) | undefined), 'lastName'?: ((string | null) | undefined), 'email': string, 'profilePictureURL'?: ((string | null) | undefined) })[] }"`,
403+
`"{ members: ({ id: string, firstName?: ((string | null) | undefined), lastName?: ((string | null) | undefined), email: string, profilePictureURL?: ((string | null) | undefined) })[] }"`,
404404
);
405405
});
406406

@@ -425,7 +425,7 @@ describe("getSchemaBox with context", () => {
425425

426426
const ctx = makeCtx(schemas);
427427
expect(printTraversable(openApiSchemaToTs({ schema: schemas["Root"]!, ctx }), ctx)).toMatchInlineSnapshot(
428-
`"t.object({ str: t.string, nb: t.number, nested: t.eq({ $ref: "#/components/schemas/Nested" }) })"`,
428+
`"{ str: string, nb: number, nested: { nested_prop: boolean } }"`,
429429
);
430430
});
431431

@@ -450,7 +450,7 @@ describe("getSchemaBox with context", () => {
450450

451451
const ctx = makeCtx(schemas);
452452
expect(printTraversable(openApiSchemaToTs({ schema: schemas["Extends"]!, ctx }), ctx)).toMatchInlineSnapshot(
453-
`"t.intersect(t.eq({ $ref: "#/components/schemas/Base" }), t.object({ str: t.string, nb: t.optional(t.number) }))"`,
453+
`"({ baseProp: string } & { str: string, nb?: (number | undefined) })"`,
454454
);
455455
});
456456

@@ -483,7 +483,7 @@ describe("getSchemaBox with context", () => {
483483

484484
const ctx = makeCtx(schemas);
485485
expect(printTraversable(openApiSchemaToTs({ schema: schemas["Root2"]!, ctx }), ctx)).toMatchInlineSnapshot(
486-
`"t.object({ str: t.string, nb: t.number, nested: t.eq({ $ref: "#/components/schemas/Nested2" }) })"`,
486+
`"{ str: string, nb: number, nested: { nested_prop: boolean, deeplyNested: (("aaa" | "bbb" | "ccc"))[] } }"`,
487487
);
488488
});
489489

@@ -510,7 +510,7 @@ describe("getSchemaBox with context", () => {
510510
const ctx = makeCtx(schemas);
511511

512512
expect(printTraversable(openApiSchemaToTs({ schema: schemas["Root3"]!, ctx }), ctx)).toMatchInlineSnapshot(
513-
`"t.object({ str: t.string, nb: t.number, nested: t.eq({ $ref: "#/components/schemas/Nested3" }), arrayOfNested: t.array(t.eq({ $ref: "#/components/schemas/Nested3" })) })"`,
513+
`"{ str: string, nb: number, nested: { 'nested_prop': boolean, 'backToRoot': string }, arrayOfNested: ({ 'nested_prop': boolean, 'backToRoot': string })[] }"`,
514514
);
515515
});
516516

@@ -539,7 +539,7 @@ describe("getSchemaBox with context", () => {
539539
const result = openApiSchemaToTs({ schema: schemas["Root4"]!, ctx });
540540

541541
expect(printTraversable(result, ctx)).toMatchInlineSnapshot(
542-
`"t.object({ str: t.string, nb: t.number, self: t.eq({ $ref: "#/components/schemas/Root4" }), nested: t.eq({ $ref: "#/components/schemas/Nested4" }), arrayOfSelf: t.array(t.eq({ $ref: "#/components/schemas/Root4" })) })"`,
542+
`"{ str: string, nb: number, self: { 'str': string, 'nb': number, 'self': string, 'nested': string, 'arrayOfSelf': (string)[] }, nested: { 'nested_prop': boolean, 'backToRoot': string }, arrayOfSelf: ({ 'str': string, 'nb': number, 'self': string, 'nested': string, 'arrayOfSelf': (string)[] })[] }"`,
543543
);
544544
});
545545

@@ -573,7 +573,7 @@ describe("getSchemaBox with context", () => {
573573
const result = openApiSchemaToTs({ schema: schemas["Root"]!, ctx });
574574

575575
expect(printTraversable(result, ctx)).toMatchInlineSnapshot(
576-
`"t.object({ recursive: t.eq({ $ref: "#/components/schemas/User" }), basic: t.number })"`,
576+
`"{ recursive: { 'name': string, 'middle': string }, basic: number }"`,
577577
);
578578
});
579579

@@ -610,7 +610,7 @@ describe("getSchemaBox with context", () => {
610610
const result = openApiSchemaToTs({ schema: schemas["Root"]!, ctx });
611611

612612
expect(printTraversable(result, ctx)).toMatchInlineSnapshot(
613-
`"t.object({ user: t.union(t.eq({ $ref: "#/components/schemas/User" }), t.eq({ $ref: "#/components/schemas/Member" })), users: t.array(t.union(t.eq({ $ref: "#/components/schemas/User" }), t.eq({ $ref: "#/components/schemas/Member" }))), basic: t.number })"`,
613+
`"{ user: ({ name: string } | { name: string }), users: (({ name: string } | { name: string }))[], basic: number }"`,
614614
);
615615
});
616616

@@ -626,8 +626,8 @@ describe("getSchemaBox with context", () => {
626626
} satisfies SchemasObject;
627627

628628
const ctx = makeCtx(schemas);
629-
const result = openApiSchemaToTs({ schema: schemat.Member, ctx });
629+
const result = openApiSchemaToTs({ schema: schemas.Member, ctx });
630630

631-
expect(printTraversable(result, ctx)).toMatchInlineSnapshot(`"t.object({ name: t.union(t.string, t.null) })"`);
631+
expect(printTraversable(result, ctx)).toMatchInlineSnapshot(`"{ name: (string | null) }"`);
632632
});
633633
});

0 commit comments

Comments
 (0)