Repository navigation
fix(Documentation): omit empty request bodies and unreferenced components #3809
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
base: master
Are you sure you want to change the base?
Changes from all commits
7e989c8
8abacf5
0fd4242
0c2ae3a
0a785b7
585d014
ea11a55
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ import { | |
| getInputSources, | ||
| makeCleanId, | ||
| normalizeParams, | ||
| isObject, | ||
| } from "./common-helpers"; | ||
| import type { CommonConfig } from "./config-type"; | ||
| import { processContainers } from "./logical-container"; | ||
|
|
@@ -390,5 +391,34 @@ export class Documentation extends OpenApiBuilder { | |
| const handler = this.#makeEndpointHandler(rest); | ||
| const onEndpoint = hasHeadMethod ? withHead(handler) : handler; | ||
| walkRouting({ ...rest, onEndpoint }); | ||
| this.#removeUnusedSchemas(); | ||
| } | ||
|
|
||
| /** | ||
| * @desc Schemas having .meta({ id }) are registered while depicting the request, but when path params are stripped | ||
| * @desc from the body, the operation refers to the stripped copy instead, leaving the original one unreferenced. | ||
| * */ | ||
| #removeUnusedSchemas() { | ||
| const { components: { schemas, ...components } = {}, ...doc } = | ||
| this.rootDoc; | ||
| if (!schemas) return; | ||
| const prefix = "#/components/schemas/"; | ||
| const used = new Set<string>(); | ||
| const queue: unknown[] = [doc, components]; // everything but the schemas, including webhooks | ||
|
jagreehal marked this conversation as resolved.
Resolved
|
||
| while (queue.length) { | ||
| const entry = queue.pop(); | ||
| // both $ref values and discriminator mappings are strings | ||
| if (typeof entry === "string" && entry.startsWith(prefix)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| const name = entry.slice(prefix.length); | ||
|
Comment on lines
+411
to
+412
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| if (!used.has(name)) { | ||
| used.add(name); | ||
| queue.push(schemas[name]); | ||
| } | ||
| } | ||
| if (isObject(entry)) | ||
| for (const value of Object.values(entry)) queue.push(value); // no spread: argument limit | ||
|
Comment on lines
+418
to
+419
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Track visited objects in the schema-reference traversal. The loop at lines 418-419 queues the values of every object it reaches. It does not skip an object that was already expanded. A shared object reached through several paths is expanded once per path. The Nested shared objects outside Such input is unusual, so the impact is limited to documentation generation with contrived metadata. The guard is small and does not change the output. A repeated visit only re-queues strings that Suggested fix
|
||
| } | ||
| for (const name of Object.keys(schemas)) | ||
| if (!used.has(name)) delete schemas[name]; | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.