feat: setup the post-bundle decorators#2733
Conversation
🦋 Changeset detectedLatest commit: 83d363a 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 |
...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.
Autofix Details
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.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit e144213. Configure here.
|
|
||
| const componentLevelLocalPointer = localPointer.split('/').slice(0, 4).join('/'); | ||
| const pointer = `${fileLocation}#${componentLevelLocalPointer}`; | ||
| const targetPointer = getContainingComponentKey(ref.$ref); |
There was a problem hiding this comment.
| const targetPointer = getContainingComponentKey(ref.$ref); | |
| const targetPointer = getComponentKey(ref.$ref); |
There was a problem hiding this comment.
Maybe use something like isComponent instead?
There was a problem hiding this comment.
I think it is better to keep componentKey instead of raw location.pointer, because we have one unified type of targetPointers and we can easily compare them. For example, when we have a circular ref, we can easily compare it. In this case we will have two equal refs: schemas/RecursiveRef. In case of just local pointers we will have #/components/schemas/RecursiveRef and #/components/schemas/RecursiveRef/properties/prop/items/anyOf/0 and it will be hard to compare.
components:
schemas:
RecursiveRef:
type: object
properties:
prop:
type: array
items:
anyOf:
- $ref: '#/components/schemas/RecursiveRef'
tatomyr
left a comment
There was a problem hiding this comment.
Left minor comments. Otherwise looks good to me.
| @@ -0,0 +1,5 @@ | |||
| --- | |||
| "@redocly/openapi-core": minor | |||
There was a problem hiding this comment.
It also affects how the CLI behaves and I think it would be useful to specify it directly in it's changelog.
There was a problem hiding this comment.
Thanks, will add it.


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 runremove-unused-componentsafter doc is bundled.What i did:
bundleDocumentnew phase to walk through the doc one more time and invokeremove-unused-componentson bundled doc.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
Resolves #2350
Resolves #1783
Testing
Screenshots (optional)
Check yourself
Security
Note
Medium Risk
Changes bundling flow to run
remove-unused-componentsin a new post-bundle pass and refactors its reference-tracking logic; this can change final bundled output and component pruning behavior across OAS2/OAS3.Overview
Moves
remove-unused-componentsout of the main bundle walk and into a dedicated post-bundle pass (with a freshresolveDocument), ensuring components that become unused only after$refresolution are removed.Refactors the OAS2/OAS3
remove-unused-componentsdecorators to track component usage via stable#/...pointer-derived keys (usingparseRef) instead of resolvedLocationobjects, and adds new e2e bundle fixtures covering parameter-to-schema refs and recursive refs. Includes a changeset bumping@redocly/openapi-coreand@redocly/clias minor.Reviewed by Cursor Bugbot for commit 83d363a. Bugbot is set up for automated code reviews on this repo. Configure here.