feat: setup the post-bundle decorators#2733
Conversation
🦋 Changeset detectedLatest commit: b4f9dc4 The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
Coverage Report
File Coverage
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@cursor review |
| (location.absolutePointer.length === removed.length || | ||
| location.absolutePointer[removed.length] === '/') | ||
| ) | ||
| (sourceKey) => sourceKey === undefined || !removedKeys.has(sourceKey) |
There was a problem hiding this comment.
This is confusing:
sourceKey === undefined
There was a problem hiding this comment.
Changed logic
| const componentLevelLocalPointer = localPointer.split('/').slice(0, 4).join('/'); | ||
| const pointer = `${fileLocation}#${componentLevelLocalPointer}`; | ||
| const targetPointer = getContainingComponentKey(ref.$ref); | ||
| if (!targetPointer) return; |
There was a problem hiding this comment.
It's not obvious that external refs are undefined, so we can skip registering it.
...nts-from-config/oas3-parameter-ref-to-schema-with-unused-schema-opposite-ref/parameters.yaml
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix prepared a fix for the issue found in the latest run.
- ✅ Fixed: OAS2 getContainingComponentKey lacks local-ref guard unlike OAS3
- Added
if (!pointer.startsWith('#/')) returnso external/URL refs are ignored before parseRef strips the URI, matching OAS3 behavior and preventing mis-keying.
- Added
Or push these changes by commenting:
@cursor push 72816a3152
Preview (72816a3152)
diff --git a/packages/core/src/decorators/oas2/remove-unused-components.ts b/packages/core/src/decorators/oas2/remove-unused-components.ts
--- a/packages/core/src/decorators/oas2/remove-unused-components.ts
+++ b/packages/core/src/decorators/oas2/remove-unused-components.ts
@@ -26,6 +26,7 @@
}
function getContainingComponentKey(pointer: string): string | undefined {
+ if (!pointer.startsWith('#/')) return;
const [type, name] = parseRef(pointer).pointer;
if (!type || !name) return undefined;
if (!OAS2_COMPONENT_TYPES.includes(type as keyof Oas2Components)) return undefined;This Bugbot Autofix run was free. To enable autofix for future PRs, go to the Cursor dashboard.
Reviewed by Cursor Bugbot for commit b4f9dc4. Configure here.
| if (!type || !name) return undefined; | ||
| if (!OAS2_COMPONENT_TYPES.includes(type as keyof Oas2Components)) return undefined; | ||
| return `${type}/${name}`; | ||
| } |
There was a problem hiding this comment.
OAS2 getContainingComponentKey lacks local-ref guard unlike OAS3
Low Severity
The OAS2 getContainingComponentKey does not verify the pointer is a local ref before parsing, unlike the OAS3 version which guards with pointer.startsWith('#/components/'). Because parseRef strips the URI portion, an external ref like external.yaml#/definitions/Foo produces the same key (definitions/Foo) as the local #/definitions/Foo. When keepUrlRefs is enabled, unresolved external URL refs could be misidentified as references to local components, causing incorrect usedIn tracking.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit b4f9dc4. Configure here.



What/Why/How?
Currently we run
remove-unused-componentsas a regular decorator in the same walk as bundle phase. This creates a lot of side issues with refs in this decorator, which is hard to fix right in the decorator. I decided to createpostBundleDecoratorsand run them after doc is bundled.What i did:
postBundleDecoratorsin types, constants and method config.runPostBundleDecoratorsto walk through the doc one more time and invoke allpostBundleDecoratorson bundled doc.bundleDocumentwith a new phase.remove-unused-componentsto track components by local pointer strings (#/components/schemas/Foo) instead of resolved Location objects. This removes the dependency onresolve()during ref tracking and makes "used-in" tracking simpler and more reliable when running post-bundle (where all refs are already inlined).remove-unused-components.Reference
Testing
Screenshots (optional)
Check yourself
Security
Note
Medium Risk
Changes the bundling pipeline by adding a second post-bundle walk, which can affect decorator ordering and bundled output. Refactors
remove-unused-componentsreference tracking, so regressions could lead to over/under-pruning components across specs.Overview
Adds a new post-bundle decorator phase to
bundleDocument, allowing decorators to run after bundling and$refresolution via plugin-definedpostBundleDecorators.Moves the built-in
remove-unused-componentsdecorator for OAS2/OAS3 into this post-bundle phase and refactors it to track usage by component pointer keys instead of resolvedLocation/resolve()results, improving correctness for refs resolved during bundling.Updates config/plugin types and built-in plugin wiring to support
postBundleDecorators, and adds new e2e bundle fixtures/snapshots to cover tricky ref and recursion scenarios.Reviewed by Cursor Bugbot for commit b4f9dc4. Bugbot is set up for automated code reviews on this repo. Configure here.