Skip to content

Commit 16f7c9c

Browse files
authored
fix: skip vendor extensions at the path level(#891) (#1440)
* fix: skip vendor extensions at the path level(#891) * fix: use HTTP-method allowlist instead of skipping x-* for path-level filtering * fix: update test to match openapi example --------- Co-authored-by: nicumicle <20170987+nicumicleI@users.noreply.github.com>
1 parent 568f26c commit 16f7c9c

3 files changed

Lines changed: 54 additions & 0 deletions

File tree

demo/examples/petstore.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ x-tagGroups:
9292
- store_model
9393
paths:
9494
/pet:
95+
x-custom-string: hello
96+
x-custom-bool: true
9597
parameters:
9698
- name: Accept-Language
9799
in: header

packages/docusaurus-plugin-openapi-docs/src/openapi/openapi.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,41 @@ describe("openapi", () => {
271271
expect(templatedVerbItem.api.postman).toBeDefined();
272272
});
273273
});
274+
275+
describe("vendor extensions at path level", () => {
276+
it("does not throw and skips x-* keys and unknown keys on path objects", async () => {
277+
const openapiData = {
278+
openapi: "3.0.0",
279+
info: { title: "Vendor Ext API", version: "1.0.0" },
280+
paths: {
281+
"/items": {
282+
"x-custom-string": "test",
283+
"x-custom-bool": true,
284+
unknownKey: { someField: true },
285+
get: {
286+
summary: "List items",
287+
operationId: "listItems",
288+
responses: { "200": { description: "OK" } },
289+
},
290+
},
291+
},
292+
};
293+
294+
const options: APIOptions = {
295+
specPath: "dummy",
296+
outputDir: "build",
297+
};
298+
const sidebarOptions = {} as SidebarOptions;
299+
300+
const [items] = await processOpenapiFile(
301+
openapiData as any,
302+
options,
303+
sidebarOptions
304+
);
305+
306+
const apiItems = items.filter((item) => item.type === "api");
307+
expect(apiItems).toHaveLength(1);
308+
expect((apiItems[0] as any).api.method).toBe("get");
309+
});
310+
});
274311
});

packages/docusaurus-plugin-openapi-docs/src/openapi/openapi.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,25 @@ function createItems(
139139
items.push(infoPage);
140140
}
141141

142+
const HTTP_METHODS = new Set([
143+
"get",
144+
"put",
145+
"post",
146+
"delete",
147+
"options",
148+
"head",
149+
"patch",
150+
"trace",
151+
]);
152+
142153
for (let [path, pathObject] of Object.entries(openapiData.paths)) {
143154
const { $ref, description, parameters, servers, summary, ...rest } =
144155
pathObject;
145156
for (let [method, operationObject] of Object.entries({ ...rest })) {
157+
if (!HTTP_METHODS.has(method.toLowerCase())) {
158+
continue;
159+
}
160+
146161
const title =
147162
operationObject.summary ??
148163
operationObject.operationId ??

0 commit comments

Comments
 (0)