-
Notifications
You must be signed in to change notification settings - Fork 1
fix: prevent OOM when loading multi-file OpenAPI specs #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| openapi: 3.1.0 | ||
| info: | ||
| title: Shared Models | ||
| version: 0.1.0 | ||
| paths: {} | ||
| components: | ||
| schemas: | ||
| Address: | ||
| title: Address | ||
| type: object | ||
| properties: | ||
| line1: { type: string } | ||
| city: { type: string } | ||
|
|
||
| Company: | ||
| title: Company | ||
| type: object | ||
| properties: | ||
| name: { type: string } | ||
| address: | ||
| $ref: '#/components/schemas/Address' | ||
| status: | ||
| $ref: '#/components/schemas/CompanyStatus' | ||
|
|
||
| CompanyStatus: | ||
| title: CompanyStatus | ||
| type: string | ||
| enum: [PENDING, APPROVED, REJECTED] | ||
|
|
||
| CompanyList: | ||
| title: CompanyList | ||
| type: array | ||
| items: | ||
| $ref: '#/components/schemas/Company' |
63 changes: 63 additions & 0 deletions
63
src/helpers/document-loader/__fixtures__/multi-file-api.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| openapi: 3.1.0 | ||
| info: | ||
| title: Multi File API | ||
| version: 0.1.0 | ||
| paths: | ||
| /companies: | ||
| get: | ||
| operationId: listCompanies | ||
| responses: | ||
| '200': | ||
| description: List of companies | ||
| content: | ||
| application/json: | ||
| schema: | ||
| $ref: 'models.yaml#/components/schemas/CompanyList' | ||
| post: | ||
| operationId: createCompany | ||
| requestBody: | ||
| content: | ||
| application/json: | ||
| schema: | ||
| $ref: 'models.yaml#/components/schemas/Company' | ||
| responses: | ||
| '201': | ||
| description: Created | ||
| content: | ||
| application/json: | ||
| schema: | ||
| $ref: 'models.yaml#/components/schemas/Company' | ||
| /companies/{id}: | ||
| get: | ||
| operationId: getCompany | ||
| parameters: | ||
| - in: path | ||
| name: id | ||
| required: true | ||
| schema: { type: string } | ||
| responses: | ||
| '200': | ||
| description: Company detail | ||
| content: | ||
| application/json: | ||
| schema: | ||
| $ref: 'models.yaml#/components/schemas/Company' | ||
| put: | ||
| operationId: updateCompany | ||
| parameters: | ||
| - in: path | ||
| name: id | ||
| required: true | ||
| schema: { type: string } | ||
| requestBody: | ||
| content: | ||
| application/json: | ||
| schema: | ||
| $ref: 'models.yaml#/components/schemas/Company' | ||
| responses: | ||
| '200': | ||
| description: Updated | ||
| content: | ||
| application/json: | ||
| schema: | ||
| $ref: 'models.yaml#/components/schemas/Company' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import 'ts-array-extensions'; | ||
| import type { JsonMap } from '@laurence79/ts-json'; | ||
| import path from 'path'; | ||
| import { load } from './load'; | ||
|
|
||
| const fixture = (name: string) => path.join(__dirname, '__fixtures__', name); | ||
|
|
||
| describe('load', () => { | ||
| describe('multi-file $ref resolution', () => { | ||
| it('inlines cross-file schemas into the root document', async () => { | ||
| const doc = (await load(fixture('multi-file-api.yaml'))) as JsonMap; | ||
| const components = doc.components as JsonMap; | ||
| const schemas = components.schemas as JsonMap; | ||
|
|
||
| expect(schemas.Company).toBeDefined(); | ||
| expect(schemas.CompanyList).toBeDefined(); | ||
| expect(schemas.Address).toBeDefined(); | ||
| expect(schemas.CompanyStatus).toBeDefined(); | ||
| }); | ||
|
|
||
| it('rewrites $ref to point to local schemas', async () => { | ||
| const doc = (await load(fixture('multi-file-api.yaml'))) as JsonMap; | ||
| const paths = doc.paths as JsonMap; | ||
| const companies = paths['/companies'] as JsonMap; | ||
| const get = companies.get as JsonMap; | ||
| const responses = get.responses as JsonMap; | ||
| const ok = responses['200'] as JsonMap; | ||
| const content = ok.content as JsonMap; | ||
| const json = content['application/json'] as JsonMap; | ||
| const schema = json.schema as JsonMap; | ||
|
|
||
| expect(schema.$ref).toMatch(/#\/components\/schemas\/CompanyList$/); | ||
| }); | ||
|
|
||
| it('does not create nested components trees under inlined schemas', async () => { | ||
| const doc = (await load(fixture('multi-file-api.yaml'))) as JsonMap; | ||
| const components = doc.components as JsonMap; | ||
| const schemas = components.schemas as JsonMap; | ||
|
|
||
| for (const [, schema] of Object.entries(schemas)) { | ||
| expect(schema).not.toHaveProperty('components'); | ||
| } | ||
| }); | ||
|
|
||
| it('inlines each external schema only once regardless of reference count', async () => { | ||
| const doc = (await load(fixture('multi-file-api.yaml'))) as JsonMap; | ||
| const components = doc.components as JsonMap; | ||
| const schemas = components.schemas as JsonMap; | ||
|
|
||
| const schemaNames = Object.keys(schemas).sort(); | ||
| expect(schemaNames).toEqual([ | ||
| 'Address', | ||
| 'Company', | ||
| 'CompanyList', | ||
| 'CompanyStatus' | ||
| ]); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deduplication skips inlining when target path differs
Low Severity
node.$ref = newRef.$is set unconditionally (line 202), butjsonPointer.set(doc, newRef.pointer, cloned)only runs for the first encounter of a givencanonicalKey. SincenewRefdepends oncomponentKeyForPointer(ptr), if the same external schema is referenced from contexts producing different component keys (e.g., one resolving toschemas, another toparameters), the second occurrence's$refwill point to a path that was never populated indoc, creating a dangling reference.Additional Locations (1)
src/helpers/document-loader/load.ts#L196-L199