Skip to content

Commit fe125cb

Browse files
authored
feat: extract description from requestBody to use it in type (#1306)
* feat: extract description from requestBody to use it in type * chore: update changeset
1 parent d4758a1 commit fe125cb

File tree

8 files changed

+118
-1
lines changed

8 files changed

+118
-1
lines changed

.changeset/major-spies-peel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"swagger-typescript-api": patch
3+
---
4+
5+
better extracting description for request body extacting types

src/schema-routes/schema-routes.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,17 @@ export class SchemaRoutes {
611611
});
612612
}
613613

614+
if (
615+
schema &&
616+
schema.typeData &&
617+
!schema.typeData.description &&
618+
requestBody?.description
619+
) {
620+
schema.typeData.description = requestBody.description;
621+
}
622+
614623
return {
624+
...(requestBody || {}),
615625
paramName: requestBodyName || requestBody?.name || DEFAULT_BODY_ARG_NAME,
616626
contentTypes,
617627
contentKind,
@@ -995,6 +1005,7 @@ export class SchemaRoutes {
9951005
: void 0,
9961006
body: requestBodyInfo.type
9971007
? {
1008+
...requestBodyInfo,
9981009
name: nameResolver.resolve([
9991010
requestBodyInfo.paramName,
10001011
...RESERVED_BODY_ARG_NAMES,

templates/base/data-contract-jsdoc.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const { data, utils } = it;
33
const { formatDescription, require, _ } = utils;
44
5-
const stringify = (value) => _.isObject(value) ? JSON.stringify(value) : _.isString(value) ? `"${value}"` : value
5+
const stringify = (value) => _.isObject(value) ? JSON.stringify(value) : _.isString(value) ? `"${value}"` : value;
66
77
const jsDocLines = _.compact([
88
data.title,

tests/__snapshots__/extended.test.ts.snap

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ export type AllTriggersData = Trigger[];
116116

117117
export type BatchCreateDataData = DataResponse[];
118118

119+
/** A collection of data records including \`value\` (required) and optionally including: \`lat\`, \`lon\`, \`ele\` (latitude, longitude, and elevation values), and \`created_at\` (a date/time string). */
119120
export type BatchCreateDataPayload = {
120121
/** @format dateTime */
121122
created_at?: string;
@@ -128,6 +129,7 @@ export type BatchCreateDataPayload = {
128129

129130
export type BatchCreateGroupFeedDataData = DataResponse[];
130131

132+
/** A collection of data records including \`value\` (required) and optionally including: \`lat\`, \`lon\`, \`ele\` (latitude, longitude, and elevation values), and \`created_at\` (a date/time string). */
131133
export type BatchCreateGroupFeedDataPayload = {
132134
/** @format dateTime */
133135
created_at?: string;
@@ -225,6 +227,7 @@ export interface CreateDashboardPayload {
225227

226228
export type CreateDataData = Data;
227229

230+
/** Data record including a \`value\` field (required) and optionally including: \`lat\`, \`lon\`, \`ele\` (latitude, longitude, and elevation values), and \`created_at\` (a date/time string). */
228231
export interface CreateDataPayload {
229232
/** @format dateTime */
230233
created_at?: string;
@@ -274,6 +277,7 @@ export type CreateGroupFeedData = Feed;
274277

275278
export type CreateGroupFeedDataData = DataResponse;
276279

280+
/** Data record including a \`value\` field (required) and optionally including: \`lat\`, \`lon\`, \`ele\` (latitude, longitude, and elevation values), and \`created_at\` (a date/time string). */
277281
export interface CreateGroupFeedDataPayload {
278282
/** @format dateTime */
279283
created_at?: string;
@@ -338,6 +342,7 @@ export interface CreateTriggerPayload {
338342

339343
export type CreateWebhookFeedDataData = Data;
340344

345+
/** Webhook payload containing data \`value\` parameter. */
341346
export interface CreateWebhookFeedDataPayload {
342347
value?: string;
343348
}
@@ -621,6 +626,7 @@ export interface ReplaceDashboardPayload {
621626

622627
export type ReplaceDataData = DataResponse;
623628

629+
/** Data record including a \`value\` field (required) and optionally including: \`lat\`, \`lon\`, \`ele\` (latitude, longitude, and elevation values), and \`created_at\` (a date/time string). */
624630
export interface ReplaceDataPayload {
625631
/** @format dateTime */
626632
created_at?: string;
@@ -732,6 +738,7 @@ export interface UpdateDashboardPayload {
732738

733739
export type UpdateDataData = DataResponse;
734740

741+
/** Data record including a \`value\` field (required) and optionally including: \`lat\`, \`lon\`, \`ele\` (latitude, longitude, and elevation values), and \`created_at\` (a date/time string). */
735742
export interface UpdateDataPayload {
736743
/** @format dateTime */
737744
created_at?: string;
@@ -5285,8 +5292,10 @@ export interface Category {
52855292
name?: string;
52865293
}
52875294

5295+
/** List of user object */
52885296
export type CreateUsersWithArrayInputPayload = User[];
52895297

5298+
/** List of user object */
52905299
export type CreateUsersWithListInputPayload = User[];
52915300

52925301
/**
@@ -68537,8 +68546,10 @@ export interface Category {
6853768546
name?: string;
6853868547
}
6853968548

68549+
/** List of user object */
6854068550
export type CreateUsersWithArrayInputPayload = User[];
6854168551

68552+
/** List of user object */
6854268553
export type CreateUsersWithListInputPayload = User[];
6854368554

6854468555
export type FindPetsByStatusData = Pet[];

tests/spec/extractRequestBody/__snapshots__/basic.test.ts.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,10 @@ export interface UploadFilePayloadTTT {
178178
file?: File;
179179
}
180180
181+
/** List of user object */
181182
export type CreateUsersWithArrayInputPayloadTTT = UserTTT[];
182183
184+
/** List of user object */
183185
export type CreateUsersWithListInputPayloadTTT = UserTTT[];
184186
185187
export type QueryParamsType = Record<string | number, any>;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`basic > requestBodyDescription 1`] = `
4+
"/* eslint-disable */
5+
/* tslint:disable */
6+
// @ts-nocheck
7+
/*
8+
* ---------------------------------------------------------------
9+
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
10+
* ## ##
11+
* ## AUTHOR: acacode ##
12+
* ## SOURCE: https://github.com/acacode/swagger-typescript-api ##
13+
* ---------------------------------------------------------------
14+
*/
15+
16+
/** FODSFDSF DSF DSF */
17+
export type CreateKeksPayload = object;
18+
19+
export type CreateKeksData = object;
20+
"
21+
`;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import * as fs from "node:fs/promises";
2+
import * as os from "node:os";
3+
import * as path from "node:path";
4+
5+
import { afterAll, beforeAll, describe, expect, test } from "vitest";
6+
7+
import { generateApi } from "../../../src/index.js";
8+
9+
describe("basic", async () => {
10+
let tmpdir = "";
11+
12+
beforeAll(async () => {
13+
tmpdir = await fs.mkdtemp(path.join(os.tmpdir(), "swagger-typescript-api"));
14+
});
15+
16+
afterAll(async () => {
17+
await fs.rm(tmpdir, { recursive: true });
18+
});
19+
20+
test("requestBodyDescription", async () => {
21+
await generateApi({
22+
fileName: "schema",
23+
input: path.resolve(import.meta.dirname, "schema.yml"),
24+
output: tmpdir,
25+
silent: true,
26+
generateClient: false,
27+
generateRouteTypes: false,
28+
extractResponseBody: true,
29+
extractRequestParams: true,
30+
extractRequestBody: true,
31+
extractResponseError: true,
32+
extractResponses: true,
33+
extractEnums: true,
34+
});
35+
36+
const content = await fs.readFile(path.join(tmpdir, "schema.ts"), {
37+
encoding: "utf8",
38+
});
39+
40+
expect(content).toMatchSnapshot();
41+
});
42+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
openapi: 3.0.3
2+
info:
3+
title: teesttt
4+
version: 0.0.0
5+
security:
6+
- registryCookieAuth: []
7+
- cookieAuth: [ ]
8+
paths:
9+
/api/v333/keks:
10+
post:
11+
operationId: createKeks
12+
requestBody:
13+
description: FODSFDSF DSF DSF
14+
required: true
15+
content:
16+
application/json:
17+
schema:
18+
type: object
19+
responses:
20+
'200':
21+
description: OK.
22+
content:
23+
'application/json':
24+
schema:
25+
type: object

0 commit comments

Comments
 (0)