Skip to content

Commit 2fd6adf

Browse files
authored
fix: check if path item is method operation (#366)
* Add summary and description to type New `OpenAPI3PathItem` limits possible methods Missing `servers` key added as `any` type * Failing test added paths include 'summary' and 'description' * Add method check * Format type file * Update PathItemObject * Remove /search path from summary test * Add test for get:{description} * Remove all extraneous fields from test schema
1 parent 18acecb commit 2fd6adf

File tree

3 files changed

+70
-10
lines changed

3 files changed

+70
-10
lines changed

src/types/OpenAPI3.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,27 @@ export interface OpenAPI3Schemas {
99
}
1010

1111
export interface OpenAPI3Paths {
12-
[path: string]: {
13-
[method: string]: OpenAPI3Operation | Parameter[];
14-
};
12+
[path: string]: OpenAPI3PathItemObject;
1513
}
1614

15+
export type OpenAPI3PathItemObject = {
16+
$ref?: string;
17+
summary?: string;
18+
description?: string;
19+
get?: OpenAPI3Operation;
20+
put?: OpenAPI3Operation;
21+
post?: OpenAPI3Operation;
22+
delete?: OpenAPI3Operation;
23+
options?: OpenAPI3Operation;
24+
head?: OpenAPI3Operation;
25+
patch?: OpenAPI3Operation;
26+
trace?: OpenAPI3Operation;
27+
parameters?: Parameter[];
28+
};
29+
1730
export interface OpenAPI3Operation {
1831
operationId?: string;
32+
summary?: string;
1933
description?: string;
2034
parameters?: Parameter[];
2135
requestBody?: OpenAPI3RequestBody;

src/v3.ts

+18-7
Original file line numberDiff line numberDiff line change
@@ -261,29 +261,40 @@ export default function generateTypesV3(
261261

262262
function transformPaths(paths: OpenAPI3Paths): string {
263263
let output = "";
264-
Object.entries(paths).forEach(([path, methods]) => {
264+
Object.entries(paths).forEach(([path, pathItem]) => {
265265
output += `"${path}": {\n`;
266266

267-
Object.entries(methods).forEach(([method, operation]) => {
267+
Object.entries(pathItem).forEach(([field, operation]) => {
268268
// skip the parameters "method" for shared parameters - we'll handle it later
269-
if (method !== "parameters") {
269+
const isMethod = [
270+
"get",
271+
"put",
272+
"post",
273+
"delete",
274+
"options",
275+
"head",
276+
"patch",
277+
"trace",
278+
].includes(field);
279+
280+
if (isMethod) {
270281
operation = operation as OpenAPI3Operation;
271282

272283
if (operation.operationId) {
273-
output += `"${method}": operations["${operation.operationId}"];\n`;
284+
output += `"${field}": operations["${operation.operationId}"];\n`;
274285
operations[operation.operationId] = operation;
275286
} else {
276287
if (operation.description) output += comment(operation.description);
277-
output += `"${method}": ${transformOperation(
288+
output += `"${field}": ${transformOperation(
278289
operation as OpenAPI3Operation
279290
)}`;
280291
}
281292
}
282293
});
283294

284-
if (methods.parameters) {
295+
if (pathItem.parameters) {
285296
// Handle shared parameters
286-
output += transformParameters(methods.parameters as Parameter[]);
297+
output += transformParameters(pathItem.parameters as Parameter[]);
287298
}
288299
output += `}\n`;
289300
});

tests/v3/index.test.ts

+35
Original file line numberDiff line numberDiff line change
@@ -988,4 +988,39 @@ describe("OpenAPI3 features", () => {
988988
`)
989989
);
990990
});
991+
992+
it("paths include 'summary' and 'description'", () => {
993+
const schema: OpenAPI3 = {
994+
openapi: "3.0.1",
995+
paths: {
996+
"/": {
997+
summary: "root summary",
998+
description: "root description",
999+
get: {
1000+
summary: "get summary",
1001+
description: "get description",
1002+
responses: { },
1003+
},
1004+
},
1005+
}
1006+
};
1007+
1008+
expect(swaggerToTS(schema)).toBe(
1009+
format(`
1010+
export interface paths {
1011+
'/': {
1012+
/**
1013+
* get description
1014+
*/
1015+
get: {
1016+
responses: { }
1017+
}
1018+
};
1019+
}
1020+
1021+
export interface operations {}
1022+
1023+
export interface components {}`)
1024+
);
1025+
});
9911026
});

0 commit comments

Comments
 (0)