Skip to content

Commit 88276bd

Browse files
fix: return on invalid ref pointer in remove-unused-components for OAS2 (#2313)
1 parent 628792f commit 88276bd

File tree

5 files changed

+33
-1
lines changed

5 files changed

+33
-1
lines changed

.changeset/eager-things-train.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
"@redocly/openapi-core": patch
3+
"@redocly/cli": patch
34
---
45

56
Fixed undefined variable used in the `remove-unused-components` decorator, which prevented an invalid reference error from being reported.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Something:
2+
type: string
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
swagger: '2.0'
2+
info:
3+
title: API
4+
version: '1.0.0'
5+
paths:
6+
/pets:
7+
get:
8+
produces:
9+
- application/json
10+
responses:
11+
'200':
12+
description: Success
13+
schema:
14+
$ref: 'schemas.yaml#/Something.fail'

packages/core/src/decorators/oas2/__tests__/remove-unused-components.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import * as path from 'node:path';
12
import { outdent } from 'outdent';
23
import { parseYamlToDocument } from '../../../../__tests__/utils.js';
3-
import { bundleDocument } from '../../../bundle.js';
4+
import { bundleDocument, bundle } from '../../../bundle.js';
45
import { BaseResolver } from '../../../resolve.js';
56
import { createConfig } from '../../../config/index.js';
67

@@ -226,4 +227,16 @@ describe('oas2 remove-unused-components', () => {
226227
},
227228
});
228229
});
230+
231+
it('should report invalid ref errors', async () => {
232+
const { problems } = await bundle({
233+
ref: path.join(__dirname, 'fixtures/handle-invalid-ref/swagger.yaml'),
234+
config: await createConfig({}),
235+
removeUnusedComponents: true,
236+
});
237+
238+
expect(problems).toHaveLength(1);
239+
expect(problems[0].ruleId).toEqual('bundler');
240+
expect(problems[0].message).toEqual("Can't resolve $ref");
241+
});
229242
});

packages/core/src/decorators/oas2/remove-unused-components.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ export const RemoveUnusedComponents: Oas2Decorator = () => {
6161
if (!resolvedRef.location) return;
6262

6363
const [fileLocation, localPointer] = resolvedRef.location.absolutePointer.split('#', 2);
64+
if (!localPointer) return;
65+
6466
const componentLevelLocalPointer = localPointer.split('/').slice(0, 3).join('/');
6567
const pointer = `${fileLocation}#${componentLevelLocalPointer}`;
6668

0 commit comments

Comments
 (0)