Skip to content

Commit 5effff4

Browse files
authored
fix: fix no-undefined-server-variable crashing on null in server list (#2142)
1 parent 1289304 commit 5effff4

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

.changeset/good-vans-sell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@redocly/cli": patch
3+
---
4+
5+
Fixed `no-undefined-server-variable` crash when encountering `null` values in the server list.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { outdent } from 'outdent';
2+
import { lintDocument } from '../../../lint.js';
3+
import { parseYamlToDocument, replaceSourceWithRef } from '../../../../__tests__/utils.js';
4+
import { BaseResolver } from '../../../resolve.js';
5+
import { createConfig } from '../../../config/index.js';
6+
7+
describe('Oas3 no-undefined-server-variable', () => {
8+
it('should not crash on null server', async () => {
9+
const document = parseYamlToDocument(
10+
outdent`
11+
openapi: 3.0.0
12+
servers:
13+
- null
14+
`,
15+
'foobar.yaml'
16+
);
17+
18+
const results = await lintDocument({
19+
externalRefResolver: new BaseResolver(),
20+
document,
21+
config: await createConfig({ rules: { 'no-undefined-server-variable': 'error' } }),
22+
});
23+
24+
expect(replaceSourceWithRef(results)).toHaveLength(0);
25+
});
26+
});

packages/core/src/rules/oas3/no-undefined-server-variable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { Oas3Rule } from '../../visitors.js';
33
export const NoUndefinedServerVariable: Oas3Rule = () => {
44
return {
55
Server(server, { report, location }) {
6-
if (!server.url) return;
6+
if (!server?.url) return;
77
const urlVariables = server.url.match(/{[^}]+}/g)?.map((e) => e.slice(1, e.length - 1)) || [];
88
const definedVariables = (server?.variables && Object.keys(server.variables)) || [];
99

0 commit comments

Comments
 (0)