Skip to content

Commit e1bef9d

Browse files
authored
feat: add timeout support (#972)
## 🧰 Changes - Add the ability to pass a timeout to openapi-parser, which will pass it through to json-schema-ref-parser ## 🧬 QA & Testing - Added a test ## questions - I copied the doc string from json-schema-ref-parser - does it make sense to change in this context, or keep it as is? - Is there any other surface where I ought to test the option besides `validate`?
1 parent 7ba052a commit e1bef9d

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

packages/parser/src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ export interface ParserOptions {
131131
};
132132
};
133133

134+
/**
135+
* The maximum amount of time (in milliseconds) that JSON Schema $Ref Parser will spend dereferencing a single schema.
136+
* It will throw a timeout error if the operation takes longer than this.
137+
*/
138+
timeoutMs?: number;
139+
134140
validate?: {
135141
errors?: {
136142
/**

packages/parser/src/util.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,7 @@ export function convertOptionsForParser(options: ParserOptions): Partial<$RefPar
7171
timeout: options?.resolve?.http && 'timeout' in options.resolve.http ? options.resolve.http.timeout : 5000,
7272
},
7373
},
74+
75+
timeoutMs: options?.timeoutMs,
7476
};
7577
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { describe, it, expect, assert } from 'vitest';
2+
3+
import { validate } from '../../../src/index.js';
4+
import { relativePath } from '../../utils.js';
5+
import { toValidate } from '../../vitest.matchers.js';
6+
7+
expect.extend({ toValidate });
8+
9+
describe('API validation timeout', () => {
10+
it('should timeout validation if timeout is exceeded', async () => {
11+
try {
12+
await validate(relativePath('specs/large-file-memory-leak/cloudflare.json'), { timeoutMs: 5 });
13+
assert.fail();
14+
} catch (err) {
15+
expect(err).toBeInstanceOf(Error);
16+
expect(err.message).toContain('timeout');
17+
}
18+
});
19+
20+
it('should succeed validation if timeout is not exceeded', async () => {
21+
await expect(relativePath('specs/circular/circular.yaml')).toValidate({ options: { timeoutMs: 5000 } });
22+
});
23+
});

0 commit comments

Comments
 (0)