This repository has been archived by the owner on Aug 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a migration layer for
@apidevtools/swagger-parser
(#140)
* chore: add a simple migration layer (wip) * feat: add dereference to the migration layer * feat: load files, add throwOnError to load * chore: clean up * fix: tests
- Loading branch information
Showing
8 changed files
with
256 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,23 @@ | ||
{ | ||
"openapi": "3.1.0", | ||
"info": { | ||
"title": "Hello World", | ||
"version": "1.0.0" | ||
}, | ||
"paths": { | ||
"/foobar": { | ||
"post": { | ||
"requestBody": { | ||
"$ref": "#/components/requestBodies/Foobar" | ||
} | ||
} | ||
} | ||
}, | ||
"components": { | ||
"requestBodies": { | ||
"Foobar": { | ||
"content": {} | ||
} | ||
} | ||
} | ||
} |
This file contains 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,136 @@ | ||
import OriginalSwaggerParser from '@apidevtools/swagger-parser' | ||
import path from 'node:path' | ||
import { describe, expect, it, vi } from 'vitest' | ||
|
||
import { dereference } from '../src/utils/dereference' | ||
import { load } from '../src/utils/load' | ||
import { fetchUrls } from '../src/utils/load/plugins/fetchUrls' | ||
import { readFiles } from '../src/utils/load/plugins/readFiles' | ||
import { validate } from '../src/utils/validate' | ||
|
||
const myAPI = JSON.stringify({ | ||
openapi: '3.1.0', | ||
info: { | ||
title: 'Hello World', | ||
version: '1.0.0', | ||
}, | ||
paths: { | ||
'/foobar': { | ||
post: { | ||
requestBody: { | ||
$ref: '#/components/requestBodies/Foobar', | ||
}, | ||
}, | ||
}, | ||
}, | ||
components: { | ||
requestBodies: { | ||
Foobar: { | ||
content: {}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
class SwaggerParser { | ||
static async validate(api: string, callback: (err: any, api: any) => void) { | ||
try { | ||
const { filesystem } = await load(api, { | ||
plugins: [fetchUrls(), readFiles()], | ||
throwOnError: true, | ||
}) | ||
|
||
validate(filesystem, { | ||
throwOnError: true, | ||
}).then((result) => { | ||
callback(null, result.schema) | ||
}) | ||
} catch (error) { | ||
callback(error, null) | ||
} | ||
} | ||
|
||
static async dereference(api: string) { | ||
const { filesystem } = await load(api, { | ||
plugins: [fetchUrls(), readFiles()], | ||
throwOnError: true, | ||
}) | ||
|
||
return dereference(filesystem).then((result) => result.schema) | ||
} | ||
} | ||
|
||
// https://github.com/APIDevTools/swagger-parser?tab=readme-ov-file#example | ||
describe('validate', async () => { | ||
it('validates', async () => { | ||
return new Promise((resolve, reject) => { | ||
SwaggerParser.validate(myAPI, (err, api) => { | ||
if (err) { | ||
reject(err) | ||
} else { | ||
expect(api.info.title).toBe('Hello World') | ||
expect(api.info.version).toBe('1.0.0') | ||
|
||
resolve(null) | ||
} | ||
}) | ||
}) | ||
}) | ||
|
||
it('throws an error for invalid documents', async () => { | ||
return new Promise((resolve, reject) => { | ||
SwaggerParser.validate('invalid', (err) => { | ||
if (err) { | ||
resolve(null) | ||
} else { | ||
reject() | ||
} | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
// https://apitools.dev/swagger-parser/docs/swagger-parser.html#dereferenceapi-options-callback | ||
describe('dereference', () => { | ||
it('dereferences', async () => { | ||
let api = await SwaggerParser.dereference(myAPI) | ||
|
||
// The `api` object is a normal JavaScript object, | ||
// so you can easily access any part of the API using simple dot notation | ||
expect(api?.paths?.['/foobar']?.post?.requestBody?.content).toEqual({}) | ||
}) | ||
|
||
it('dereferences URLs', async () => { | ||
global.fetch = async (url: string) => | ||
({ | ||
text: async () => { | ||
if (url === 'http://example.com/specification/openapi.yaml') { | ||
return myAPI | ||
} | ||
|
||
throw new Error('Not found') | ||
}, | ||
}) as Response | ||
|
||
let api = await SwaggerParser.dereference( | ||
'http://example.com/specification/openapi.yaml', | ||
) | ||
|
||
// The `api` object is a normal JavaScript object, | ||
// so you can easily access any part of the API using simple dot notation | ||
expect(api?.paths?.['/foobar']?.post?.requestBody?.content).toEqual({}) | ||
}) | ||
|
||
it('dereferences files', async () => { | ||
const EXAMPLE_FILE = path.join( | ||
new URL(import.meta.url).pathname, | ||
'../../tests/migration-layer.json', | ||
) | ||
|
||
let api = await SwaggerParser.dereference(EXAMPLE_FILE) | ||
|
||
// The `api` object is a normal JavaScript object, | ||
// so you can easily access any part of the API using simple dot notation | ||
expect(api?.paths?.['/foobar']?.post?.requestBody?.content).toEqual({}) | ||
}) | ||
}) |