|
| 1 | +import OriginalSwaggerParser from '@apidevtools/swagger-parser' |
| 2 | +import path from 'node:path' |
| 3 | +import { describe, expect, it, vi } from 'vitest' |
| 4 | + |
| 5 | +import { dereference } from '../src/utils/dereference' |
| 6 | +import { load } from '../src/utils/load' |
| 7 | +import { fetchUrls } from '../src/utils/load/plugins/fetchUrls' |
| 8 | +import { readFiles } from '../src/utils/load/plugins/readFiles' |
| 9 | +import { validate } from '../src/utils/validate' |
| 10 | + |
| 11 | +const myAPI = JSON.stringify({ |
| 12 | + openapi: '3.1.0', |
| 13 | + info: { |
| 14 | + title: 'Hello World', |
| 15 | + version: '1.0.0', |
| 16 | + }, |
| 17 | + paths: { |
| 18 | + '/foobar': { |
| 19 | + post: { |
| 20 | + requestBody: { |
| 21 | + $ref: '#/components/requestBodies/Foobar', |
| 22 | + }, |
| 23 | + }, |
| 24 | + }, |
| 25 | + }, |
| 26 | + components: { |
| 27 | + requestBodies: { |
| 28 | + Foobar: { |
| 29 | + content: {}, |
| 30 | + }, |
| 31 | + }, |
| 32 | + }, |
| 33 | +}) |
| 34 | + |
| 35 | +class SwaggerParser { |
| 36 | + static async validate(api: string, callback: (err: any, api: any) => void) { |
| 37 | + try { |
| 38 | + const { filesystem } = await load(api, { |
| 39 | + plugins: [fetchUrls(), readFiles()], |
| 40 | + throwOnError: true, |
| 41 | + }) |
| 42 | + |
| 43 | + validate(filesystem, { |
| 44 | + throwOnError: true, |
| 45 | + }).then((result) => { |
| 46 | + callback(null, result.schema) |
| 47 | + }) |
| 48 | + } catch (error) { |
| 49 | + callback(error, null) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + static async dereference(api: string) { |
| 54 | + const { filesystem } = await load(api, { |
| 55 | + plugins: [fetchUrls(), readFiles()], |
| 56 | + throwOnError: true, |
| 57 | + }) |
| 58 | + |
| 59 | + return dereference(filesystem).then((result) => result.schema) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// https://github.com/APIDevTools/swagger-parser?tab=readme-ov-file#example |
| 64 | +describe('validate', async () => { |
| 65 | + it('validates', async () => { |
| 66 | + return new Promise((resolve, reject) => { |
| 67 | + SwaggerParser.validate(myAPI, (err, api) => { |
| 68 | + if (err) { |
| 69 | + reject(err) |
| 70 | + } else { |
| 71 | + expect(api.info.title).toBe('Hello World') |
| 72 | + expect(api.info.version).toBe('1.0.0') |
| 73 | + |
| 74 | + resolve(null) |
| 75 | + } |
| 76 | + }) |
| 77 | + }) |
| 78 | + }) |
| 79 | + |
| 80 | + it('throws an error for invalid documents', async () => { |
| 81 | + return new Promise((resolve, reject) => { |
| 82 | + SwaggerParser.validate('invalid', (err) => { |
| 83 | + if (err) { |
| 84 | + resolve(null) |
| 85 | + } else { |
| 86 | + reject() |
| 87 | + } |
| 88 | + }) |
| 89 | + }) |
| 90 | + }) |
| 91 | +}) |
| 92 | + |
| 93 | +// https://apitools.dev/swagger-parser/docs/swagger-parser.html#dereferenceapi-options-callback |
| 94 | +describe('dereference', () => { |
| 95 | + it('dereferences', async () => { |
| 96 | + let api = await SwaggerParser.dereference(myAPI) |
| 97 | + |
| 98 | + // The `api` object is a normal JavaScript object, |
| 99 | + // so you can easily access any part of the API using simple dot notation |
| 100 | + expect(api?.paths?.['/foobar']?.post?.requestBody?.content).toEqual({}) |
| 101 | + }) |
| 102 | + |
| 103 | + it('dereferences URLs', async () => { |
| 104 | + global.fetch = async (url: string) => |
| 105 | + ({ |
| 106 | + text: async () => { |
| 107 | + if (url === 'http://example.com/specification/openapi.yaml') { |
| 108 | + return myAPI |
| 109 | + } |
| 110 | + |
| 111 | + throw new Error('Not found') |
| 112 | + }, |
| 113 | + }) as Response |
| 114 | + |
| 115 | + let api = await SwaggerParser.dereference( |
| 116 | + 'http://example.com/specification/openapi.yaml', |
| 117 | + ) |
| 118 | + |
| 119 | + // The `api` object is a normal JavaScript object, |
| 120 | + // so you can easily access any part of the API using simple dot notation |
| 121 | + expect(api?.paths?.['/foobar']?.post?.requestBody?.content).toEqual({}) |
| 122 | + }) |
| 123 | + |
| 124 | + it('dereferences files', async () => { |
| 125 | + const EXAMPLE_FILE = path.join( |
| 126 | + new URL(import.meta.url).pathname, |
| 127 | + '../../tests/migration-layer.json', |
| 128 | + ) |
| 129 | + |
| 130 | + let api = await SwaggerParser.dereference(EXAMPLE_FILE) |
| 131 | + |
| 132 | + // The `api` object is a normal JavaScript object, |
| 133 | + // so you can easily access any part of the API using simple dot notation |
| 134 | + expect(api?.paths?.['/foobar']?.post?.requestBody?.content).toEqual({}) |
| 135 | + }) |
| 136 | +}) |
0 commit comments