Skip to content

fix: check if path item is method operation #366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/types/OpenAPI3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,27 @@ export interface OpenAPI3Schemas {
}

export interface OpenAPI3Paths {
[path: string]: {
[method: string]: OpenAPI3Operation | Parameter[];
};
[path: string]: OpenAPI3PathItemObject;
}

export type OpenAPI3PathItemObject = {
$ref?: string;
summary?: string;
description?: string;
get?: OpenAPI3Operation;
put?: OpenAPI3Operation;
post?: OpenAPI3Operation;
delete?: OpenAPI3Operation;
options?: OpenAPI3Operation;
head?: OpenAPI3Operation;
patch?: OpenAPI3Operation;
trace?: OpenAPI3Operation;
parameters?: Parameter[];
};

export interface OpenAPI3Operation {
operationId?: string;
summary?: string;
description?: string;
parameters?: Parameter[];
requestBody?: OpenAPI3RequestBody;
Expand Down
25 changes: 18 additions & 7 deletions src/v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,29 +261,40 @@ export default function generateTypesV3(

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

Object.entries(methods).forEach(([method, operation]) => {
Object.entries(pathItem).forEach(([field, operation]) => {
// skip the parameters "method" for shared parameters - we'll handle it later
if (method !== "parameters") {
const isMethod = [
"get",
"put",
"post",
"delete",
"options",
"head",
"patch",
"trace",
].includes(field);

if (isMethod) {
operation = operation as OpenAPI3Operation;

if (operation.operationId) {
output += `"${method}": operations["${operation.operationId}"];\n`;
output += `"${field}": operations["${operation.operationId}"];\n`;
operations[operation.operationId] = operation;
} else {
if (operation.description) output += comment(operation.description);
output += `"${method}": ${transformOperation(
output += `"${field}": ${transformOperation(
operation as OpenAPI3Operation
)}`;
}
}
});

if (methods.parameters) {
if (pathItem.parameters) {
// Handle shared parameters
output += transformParameters(methods.parameters as Parameter[]);
output += transformParameters(pathItem.parameters as Parameter[]);
}
output += `}\n`;
});
Expand Down
91 changes: 91 additions & 0 deletions tests/v3/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -988,4 +988,95 @@ describe("OpenAPI3 features", () => {
`)
);
});

it("paths include 'summary' and 'description'", () => {
const schema: OpenAPI3 = {
openapi: "3.0.1",
paths: {
"/": {
summary: "root summary",
description: "root description",
get: {
summary: "get summary",
description: "get description",
responses: {
"200": {
content: {
"application/json": {
schema: {
type: "object",
properties: {
title: { type: "string" },
body: { type: "string" },
},
required: ["title", "body"],
},
},
},
},
},
},
},
},
components: {
schemas: {
ErrorResponse: {
type: "object",
properties: {
error: { type: "string" },
message: { type: "string" },
},
required: ["error", "message"],
},
SearchResponse: {
type: "object",
properties: {
title: { type: "string" },
date: { type: "string" },
},
required: ["title", "date"],
},
},
responses: {
NotFound: {
content: {
"application/json": {
schema: { $ref: "#/components/schemas/ErrorResponse" },
},
},
},
},
},
};

expect(swaggerToTS(schema)).toBe(
format(`
export interface paths {
'/': {
/**
* get description
*/
get: {
responses: {
'200': {
'application/json': { title: string; body: string }
}
}
}
};
}

export interface operations {}

export interface components {
schemas: {
ErrorResponse: { error: string; message: string };
SearchResponse: { title: string; date: string }
}
responses: {
NotFound: { [key: string]: any }
}
}`)
);
});
});