Skip to content

Commit af406dd

Browse files
authored
Merge pull request #1843 from counterfact/copilot/fix-typeerror-non-http-verbs
Fix TypeError when OpenAPI Path Item Object contains non-HTTP-verb fields
2 parents 60a320e + 231bc6a commit af406dd

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"counterfact": patch
3+
---
4+
5+
Fix TypeError when an OpenAPI Path Item Object contains non-HTTP-verb fields such as `summary`, `description`, `servers`, or `parameters`. These fields are now correctly ignored during code generation instead of being treated as operations.

src/typescript-generator/generate.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,26 @@ export async function generate(
128128
(securityRequirement?.data as Record<string, unknown>) ?? {},
129129
) as SecurityScheme[];
130130

131+
const HTTP_VERBS = new Set([
132+
"get",
133+
"put",
134+
"post",
135+
"delete",
136+
"options",
137+
"head",
138+
"patch",
139+
"trace",
140+
]);
141+
131142
paths.forEach((pathDefinition, key: string) => {
132143
debug("processing path %s", key);
133144

134145
const path = key === "/" ? "/index" : key;
135146
pathDefinition.forEach((operation, requestMethod: string) => {
147+
if (!HTTP_VERBS.has(requestMethod)) {
148+
return;
149+
}
150+
136151
repository
137152
.get(`routes${path}.ts`)
138153
.export(new OperationCoder(operation, requestMethod, securitySchemes));

test/typescript-generator/generate.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,50 @@ describe("end-to-end test", () => {
5959
});
6060
});
6161

62+
describe("path item non-HTTP-verb fields", () => {
63+
it("ignores summary and description at the path item level without throwing", async () => {
64+
await usingTemporaryFiles(async ($) => {
65+
const spec = {
66+
openapi: "3.1.0",
67+
info: { title: "Test", version: "1.0.0" },
68+
paths: {
69+
"/test": {
70+
summary: "Test Summary",
71+
description: "Test Description",
72+
get: {
73+
operationId: "getTest",
74+
responses: { "200": { description: "OK" } },
75+
},
76+
},
77+
},
78+
};
79+
80+
await $.add("openapi.json", JSON.stringify(spec));
81+
82+
const basePath = $.path("");
83+
const repository = new Repository();
84+
85+
repository.writeFiles = async () => {
86+
await Promise.resolve(undefined);
87+
};
88+
89+
await expect(
90+
generate(
91+
$.path("openapi.json"),
92+
basePath,
93+
{ routes: true, types: true },
94+
repository,
95+
),
96+
).resolves.toBeUndefined();
97+
98+
await repository.finished();
99+
100+
const scripts = [...repository.scripts.keys()];
101+
expect(scripts.some((s) => s.includes("routes/test.ts"))).toBe(true);
102+
});
103+
});
104+
});
105+
62106
describe("_.context type generation", () => {
63107
it("generates a fallback _.context.ts when no routes directory exists", async () => {
64108
await usingTemporaryFiles(async ($) => {

0 commit comments

Comments
 (0)