From defd0d713bd3972df113dce495a14da2223c5058 Mon Sep 17 00:00:00 2001 From: Steven Serrata <9343811+sserrata@users.noreply.github.com> Date: Wed, 2 Jul 2025 09:43:59 -0500 Subject: [PATCH] fix(webhooks): use event name when summary missing --- .../openapi/__fixtures__/webhook/openapi.yaml | 17 +++++++++++ .../src/openapi/openapi.ts | 10 ++++++- .../src/openapi/webhooks.test.ts | 30 +++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 packages/docusaurus-plugin-openapi-docs/src/openapi/__fixtures__/webhook/openapi.yaml create mode 100644 packages/docusaurus-plugin-openapi-docs/src/openapi/webhooks.test.ts diff --git a/packages/docusaurus-plugin-openapi-docs/src/openapi/__fixtures__/webhook/openapi.yaml b/packages/docusaurus-plugin-openapi-docs/src/openapi/__fixtures__/webhook/openapi.yaml new file mode 100644 index 000000000..cd7df61c9 --- /dev/null +++ b/packages/docusaurus-plugin-openapi-docs/src/openapi/__fixtures__/webhook/openapi.yaml @@ -0,0 +1,17 @@ +openapi: 3.0.3 +info: + title: Webhook Example + version: 1.0.0 +paths: {} +webhooks: + order.created: + post: + requestBody: + description: example body + content: + application/json: + schema: + type: object + responses: + "200": + description: OK diff --git a/packages/docusaurus-plugin-openapi-docs/src/openapi/openapi.ts b/packages/docusaurus-plugin-openapi-docs/src/openapi/openapi.ts index 503133feb..697fd2d1a 100644 --- a/packages/docusaurus-plugin-openapi-docs/src/openapi/openapi.ts +++ b/packages/docusaurus-plugin-openapi-docs/src/openapi/openapi.ts @@ -274,11 +274,19 @@ function createItems( for (let [path, pathObject] of Object.entries( openapiData["x-webhooks"] ?? openapiData["webhooks"] ?? {} )) { + const eventName = path; path = "webhook"; const { $ref, description, parameters, servers, summary, ...rest } = pathObject; for (let [method, operationObject] of Object.entries({ ...rest })) { method = "event"; + if ( + operationObject.summary === undefined && + operationObject.operationId === undefined + ) { + operationObject.summary = eventName; + } + const title = operationObject.summary ?? operationObject.operationId ?? @@ -290,7 +298,7 @@ function createItems( const baseId = operationObject.operationId ? kebabCase(operationObject.operationId) - : kebabCase(operationObject.summary); + : kebabCase(operationObject.summary ?? eventName); const extensions = []; const commonExtensions = ["x-codeSamples"]; diff --git a/packages/docusaurus-plugin-openapi-docs/src/openapi/webhooks.test.ts b/packages/docusaurus-plugin-openapi-docs/src/openapi/webhooks.test.ts new file mode 100644 index 000000000..1cbfeab88 --- /dev/null +++ b/packages/docusaurus-plugin-openapi-docs/src/openapi/webhooks.test.ts @@ -0,0 +1,30 @@ +/* ============================================================================ + * Copyright (c) Palo Alto Networks + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * ========================================================================== */ + +import path from "path"; + +// eslint-disable-next-line import/no-extraneous-dependencies +import { posixPath } from "@docusaurus/utils"; + +import { readOpenapiFiles, processOpenapiFiles } from "."; + +describe("webhooks", () => { + it("uses event name when summary and operationId are missing", async () => { + const files = await readOpenapiFiles( + posixPath(path.join(__dirname, "__fixtures__/webhook/openapi.yaml")) + ); + + const [items] = await processOpenapiFiles( + files, + { specPath: "", outputDir: "" } as any, + {} + ); + + const webhookItem = items.find((item) => item.type === "api"); + expect(webhookItem?.id).toBe("order-created"); + }); +});