Skip to content

Commit a0ee91d

Browse files
authored
fix(openapi): set response description correctly (#559)
1 parent fc0adc6 commit a0ee91d

3 files changed

Lines changed: 41 additions & 31 deletions

File tree

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+
Fix setting of response description

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,15 @@ const postsRouter = c.router({
5555
method: 'GET',
5656
path: '/posts/:id/comments',
5757
responses: {
58-
200: z.object({
59-
booleanString: z.boolean().transform((v) => v.toString()),
60-
comments: z.union([
61-
z.array(commentSchema),
62-
z.array(commentSchema.extend({ author: z.string() })),
63-
]),
64-
}),
58+
200: z
59+
.object({
60+
booleanString: z.boolean().transform((v) => v.toString()),
61+
comments: z.union([
62+
z.array(commentSchema),
63+
z.array(commentSchema.extend({ author: z.string() })),
64+
]),
65+
})
66+
.describe('Post comments'),
6567
},
6668
},
6769
}),
@@ -343,6 +345,7 @@ const expectedApiDoc = {
343345
content: {
344346
'application/json': {
345347
schema: {
348+
description: 'Post comments',
346349
properties: {
347350
comments: {
348351
oneOf: [
@@ -390,7 +393,7 @@ const expectedApiDoc = {
390393
},
391394
},
392395
},
393-
description: '200',
396+
description: 'Post comments',
394397
},
395398
},
396399
summary: undefined,

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

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -233,30 +233,32 @@ export const generateOpenApi = (
233233
? getOpenApiSchemaFromZod(path.route.body)
234234
: null;
235235

236-
const responses = Object.keys(path.route.responses).reduce((acc, key) => {
237-
const keyAsNumber = Number(key);
238-
239-
const responseSchema = getOpenApiSchemaFromZod(
240-
path.route.responses[keyAsNumber],
241-
true,
242-
);
243-
244-
return {
245-
...acc,
246-
[keyAsNumber]: {
247-
description: `${keyAsNumber}`,
248-
...(responseSchema
249-
? {
250-
content: {
251-
'application/json': {
252-
...convertSchemaObjectToMediaTypeObject(responseSchema),
236+
const responses = Object.entries(path.route.responses).reduce(
237+
(acc, [statusCode, response]) => {
238+
const responseSchema = getOpenApiSchemaFromZod(response, true);
239+
const description =
240+
isZodType(response) && response.description
241+
? response.description
242+
: statusCode;
243+
244+
return {
245+
...acc,
246+
[statusCode]: {
247+
description,
248+
...(responseSchema
249+
? {
250+
content: {
251+
'application/json': {
252+
...convertSchemaObjectToMediaTypeObject(responseSchema),
253+
},
253254
},
254-
},
255-
}
256-
: {}),
257-
},
258-
};
259-
}, {});
255+
}
256+
: {}),
257+
},
258+
};
259+
},
260+
{},
261+
);
260262

261263
const contentType =
262264
path.route?.method !== 'GET'

0 commit comments

Comments
 (0)