Skip to content

Commit

Permalink
Fix Maximum call stack size exceeded error when dereferencing large O…
Browse files Browse the repository at this point in the history
…penAPI document
  • Loading branch information
jcmosc committed Apr 25, 2024
1 parent 50c294c commit a9eb059
Show file tree
Hide file tree
Showing 3 changed files with 281,577 additions and 1 deletion.
11 changes: 10 additions & 1 deletion packages/criteria-openapi/src/openapi-index/OpenAPIIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,16 @@ export class OpenAPIIndex extends DocumentIndex {
this.references.set(reference, info)
})

return chainForEach(foundReferences.values(), (info: ReferenceInfo<Metadata>) => {
// Help prevent Maximum call stack size exceeded errors
const unindexedReferences = [...foundReferences.values()].filter((info) => {
if (this.isURIIndexed(info.resolvedURI)) {
return false
}
const { absoluteURI, fragment } = splitFragment(info.resolvedURI)
return !this.isURIIndexed(absoluteURI)
})

return chainForEach(unindexedReferences.values(), (info: ReferenceInfo<Metadata>) => {
if (this.isURIIndexed(info.resolvedURI)) {
return
}
Expand Down
28 changes: 28 additions & 0 deletions packages/criteria-openapi/test/v3.0/github/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* eslint-env jest */
import { resolve } from 'path'
import { dereferenceOpenAPI } from '../../../src/v3.0'
import retrieveFromFilesystem from '../../util/retrieveFromFilesystem'
import openAPI from './openapi.json'

describe('GitHub OpenAPI', () => {
describe('dereferenceOpenAPI()', () => {
describe('synchronous', () => {
test('does not result in Maximum call stack size exceeded', () => {
const output = dereferenceOpenAPI(openAPI as any, {
baseURI: resolve(__dirname, 'openapi.json'),
retrieve: retrieveFromFilesystem
}) as any
expect(output).toMatchObject({ openapi: '3.0.3' })
})
})
describe('asynchronous', () => {
test('does not result in Maximum call stack size exceeded', async () => {
const output = (await dereferenceOpenAPI(openAPI as any, {
baseURI: resolve(__dirname, 'openapi.json'),
retrieve: async (uri) => await retrieveFromFilesystem(uri)
})) as any
expect(output).toMatchObject({ openapi: '3.0.3' })
})
})
})
})
Loading

0 comments on commit a9eb059

Please sign in to comment.