Skip to content

Commit 0427ea3

Browse files
committed
fix: don't use $ref in OpenAPI
1 parent c2bf257 commit 0427ea3

3 files changed

Lines changed: 52 additions & 5 deletions

File tree

.changeset/long-snails-hammer.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ts-rest/open-api': patch
3+
---
4+
5+
Don't use $ref in OpenAPI

libs/ts-rest/open-api/src/lib/ts-rest-open-api.spec.ts

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ type Post = {
99
published: boolean;
1010
};
1111

12+
const commentSchema = z.object({
13+
id: z.number(),
14+
title: z.string(),
15+
});
16+
1217
const postsRouter = c.router({
1318
getPost: {
1419
method: 'GET',
@@ -47,7 +52,10 @@ const postsRouter = c.router({
4752
path: '/posts/:id/comments',
4853
responses: {
4954
200: z.object({
50-
comments: z.array(z.string()),
55+
comments: z.union([
56+
z.array(commentSchema),
57+
z.array(commentSchema.extend({ author: z.string() })),
58+
]),
5159
}),
5260
},
5361
},
@@ -197,10 +205,43 @@ const expectedApiDoc = {
197205
additionalProperties: false,
198206
properties: {
199207
comments: {
200-
items: {
201-
type: 'string',
202-
},
203-
type: 'array',
208+
anyOf: [
209+
{
210+
items: {
211+
additionalProperties: false,
212+
properties: {
213+
id: {
214+
type: 'number',
215+
},
216+
title: {
217+
type: 'string',
218+
},
219+
},
220+
required: ['id', 'title'],
221+
type: 'object',
222+
},
223+
type: 'array',
224+
},
225+
{
226+
items: {
227+
additionalProperties: false,
228+
properties: {
229+
author: {
230+
type: 'string',
231+
},
232+
id: {
233+
type: 'number',
234+
},
235+
title: {
236+
type: 'string',
237+
},
238+
},
239+
required: ['id', 'title', 'author'],
240+
type: 'object',
241+
},
242+
type: 'array',
243+
},
244+
],
204245
},
205246
},
206247
required: ['comments'],

libs/ts-rest/open-api/src/lib/ts-rest-open-api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ const getJsonSchemaFromZod = (zodObject: unknown) => {
4949
const schema = zodToJsonSchema(zodObject, {
5050
name: 'zodObject',
5151
target: 'openApi3',
52+
$refStrategy: 'none',
5253
});
5354

5455
return schema.definitions['zodObject'];

0 commit comments

Comments
 (0)