Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/fix-path-item-non-http-verb-fields.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"counterfact": patch
---

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.
15 changes: 15 additions & 0 deletions src/typescript-generator/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,26 @@ export async function generate(
(securityRequirement?.data as Record<string, unknown>) ?? {},
) as SecurityScheme[];

const HTTP_VERBS = new Set([
"get",
"put",
"post",
"delete",
"options",
"head",
"patch",
"trace",
]);

paths.forEach((pathDefinition, key: string) => {
debug("processing path %s", key);

const path = key === "/" ? "/index" : key;
pathDefinition.forEach((operation, requestMethod: string) => {
if (!HTTP_VERBS.has(requestMethod)) {
return;
}

repository
.get(`routes${path}.ts`)
.export(new OperationCoder(operation, requestMethod, securitySchemes));
Expand Down
44 changes: 44 additions & 0 deletions test/typescript-generator/generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,50 @@ describe("end-to-end test", () => {
});
});

describe("path item non-HTTP-verb fields", () => {
it("ignores summary and description at the path item level without throwing", async () => {
await usingTemporaryFiles(async ($) => {
const spec = {
openapi: "3.1.0",
info: { title: "Test", version: "1.0.0" },
paths: {
"/test": {
summary: "Test Summary",
description: "Test Description",
get: {
operationId: "getTest",
responses: { "200": { description: "OK" } },
},
},
},
};

await $.add("openapi.json", JSON.stringify(spec));

const basePath = $.path("");
const repository = new Repository();

repository.writeFiles = async () => {
await Promise.resolve(undefined);
};

await expect(
generate(
$.path("openapi.json"),
basePath,
{ routes: true, types: true },
repository,
),
).resolves.toBeUndefined();

await repository.finished();

const scripts = [...repository.scripts.keys()];
expect(scripts.some((s) => s.includes("routes/test.ts"))).toBe(true);
});
});
});

describe("_.context type generation", () => {
it("generates a fallback _.context.ts when no routes directory exists", async () => {
await usingTemporaryFiles(async ($) => {
Expand Down
Loading