Skip to content

Commit 7a1d18e

Browse files
mitchell-merrymmerry-atlassian
authored andcommitted
Fix the operation comment
1 parent 5609400 commit 7a1d18e

File tree

5 files changed

+23
-9
lines changed

5 files changed

+23
-9
lines changed

src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,9 @@ async function openapiTS(
182182
// 3. operations (only get fully built after all external schemas transformed)
183183
if (Object.keys(ctx.operations).length) {
184184
output.push(options.exportType ? "export type operations = {" : "export interface operations {", "");
185-
for (const k of Object.keys(ctx.operations)) {
186-
output.push(indent(`${escObjKey(k)}: ${ctx.operations[k]};`, 1));
185+
for (const [ key, { operationType, comment }] of Object.entries(ctx.operations)) {
186+
if (comment) output.push(indent(comment, 1));
187+
output.push(indent(`${escObjKey(key)}: ${operationType};`, 1));
187188
}
188189
output.push(`}${options.exportType ? ";" : ""}`, "");
189190
} else {

src/transform/operation-object.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ export default function transformOperationObject(
3030
let { indentLv } = ctx;
3131
const output: string[] = wrapObject ? ["{"] : [];
3232
indentLv++;
33-
const c = getSchemaObjectComment(operationObject, indentLv);
34-
if (c) output.push(indent(c, indentLv));
3533

3634
// parameters
3735
{

src/transform/path-item-object.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ export default function transformPathItemObject(
3030
// if operationId exists, move into an `operations` export and pass the reference in here
3131
else if (operationObject.operationId) {
3232
const operationType = transformOperationObject(operationObject, { path, ctx: { ...ctx, indentLv: 1 } });
33-
ctx.operations[operationObject.operationId] = operationType;
33+
ctx.operations[operationObject.operationId] = {
34+
operationType,
35+
comment: c,
36+
};
3437
output.push(indent(`${method}: operations[${escStr(operationObject.operationId)}];`, indentLv));
3538
} else {
3639
const operationType = transformOperationObject(operationObject, { path, ctx: { ...ctx, indentLv } });

src/types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,10 @@ export interface GlobalContext {
638638
postTransform: OpenAPITSOptions["postTransform"];
639639
immutableTypes: boolean;
640640
indentLv: number;
641-
operations: Record<string, string>;
641+
operations: Record<string, {
642+
comment?: string;
643+
operationType: string;
644+
}>;
642645
pathParamsAsTypes: boolean;
643646
silent: boolean;
644647
supportArrayLength: boolean;

test/path-item-object.test.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { PathItemObject } from "../src/types";
1+
import type { GlobalContext, PathItemObject } from "../src/types";
22
import transformPathItemObject, { TransformPathItemObjectOptions } from "../src/transform/path-item-object.js";
33

44
const options: TransformPathItemObjectOptions = {
@@ -23,6 +23,7 @@ describe("Path Item Object", () => {
2323
test("basic", () => {
2424
const schema: PathItemObject = {
2525
get: {
26+
description: "Basic GET",
2627
responses: {
2728
200: {
2829
description: "OK",
@@ -57,6 +58,7 @@ describe("Path Item Object", () => {
5758
},
5859
},
5960
post: {
61+
description: "Basic POST",
6062
requestBody: {
6163
content: {
6264
"application/json": { $ref: 'components["schemas"]["User"]' },
@@ -70,6 +72,7 @@ describe("Path Item Object", () => {
7072
};
7173
const generated = transformPathItemObject(schema, options);
7274
expect(generated).toBe(`{
75+
/** @description Basic GET */
7376
get: {
7477
responses: {
7578
/** @description OK */
@@ -92,6 +95,7 @@ describe("Path Item Object", () => {
9295
};
9396
};
9497
};
98+
/** @description Basic POST */
9599
post: {
96100
requestBody?: {
97101
content: {
@@ -107,9 +111,10 @@ describe("Path Item Object", () => {
107111
});
108112

109113
test("operations", () => {
110-
const operations: Record<string, string> = {};
114+
const operations: GlobalContext['operations'] = {};
111115
const schema: PathItemObject = {
112116
get: {
117+
description: "Get a user",
113118
operationId: "getUser",
114119
responses: {
115120
200: {
@@ -132,10 +137,13 @@ describe("Path Item Object", () => {
132137
};
133138
const generated = transformPathItemObject(schema, { ...options, ctx: { ...options.ctx, operations } });
134139
expect(generated).toBe(`{
140+
/** @description Get a user */
135141
get: operations["getUser"];
136142
}`);
137143
expect(operations).toEqual({
138-
getUser: `{
144+
getUser: {
145+
comment: "/** @description Get a user */",
146+
operationType: `{
139147
responses: {
140148
/** @description Get User */
141149
200: {
@@ -148,6 +156,7 @@ describe("Path Item Object", () => {
148156
};
149157
};
150158
}`,
159+
}
151160
});
152161
});
153162
});

0 commit comments

Comments
 (0)