import { describe, expect, it } from 'bun:test';
import { OpenAPIParseError } from './error';
import { parseOpenAPI } from './parse';
const spec = await Bun.file(new URL('./fixtures/recursive-spec.json', import.meta.url)).text();
const specV2 = await Bun.file(new URL('./fixtures/spec-v2.json', import.meta.url)).text();
const petstoreyaml = await Bun.file(new URL('./fixtures/petstore.yaml', import.meta.url)).text();
const petstoreInvalid = await Bun.file(
new URL('./fixtures/petstore-invalid.json', import.meta.url)
).text();
const html = `
API Documentation `;
describe('#parseOpenAPI', () => {
it(
'parses a recursive OpenAPI document',
async () => {
const result = await parseOpenAPI({
value: spec,
rootURL: null,
});
// Ensure the structure returned is not recursive (not dereferenced).
JSON.stringify(result.filesystem);
},
{ timeout: 30_000 }
);
it('parses a swagger v2', async () => {
const result = await parseOpenAPI({
value: specV2,
rootURL: null,
});
// Ensure the structure returned is not recursive (not dereferenced).
JSON.stringify(result.filesystem);
});
it('throws an error for invalid OpenAPI document', async () => {
expect.assertions(1);
try {
await parseOpenAPI({
value: html,
rootURL: null,
});
} catch (error) {
if (error instanceof OpenAPIParseError) {
expect(error.message).toContain(
'Can’t find supported Swagger/OpenAPI version in the provided document, version must be a string.'
);
}
}
});
it('allows a document yaml', async () => {
const result = await parseOpenAPI({
value: petstoreyaml,
rootURL: null,
});
// Ensure the structure returned is not recursive (not dereferenced).
JSON.stringify(result.filesystem);
});
it('allows a document with errors', async () => {
const result = await parseOpenAPI({
value: petstoreInvalid,
rootURL: null,
});
// Ensure the structure returned is not recursive (not dereferenced).
JSON.stringify(result.filesystem);
expect(result.errors).toHaveLength(1);
});
it('validates OpenAPI 3.2 schema objects as schemas', async () => {
const result = await parseOpenAPI({ value: specV32(), rootURL: null });
expect(result.errors).toEqual([]);
});
it('reports a 3.2 parameter missing `in`', async () => {
const result = await parseOpenAPI({
value: specV32({ name: 'id', required: true, schema: { type: 'string' } }),
rootURL: null,
});
expect(result.errors.map((error) => error.message)).toContain(
"must have required property 'in'"
);
});
it('loads external references with the provided fetch', async () => {
const urls: string[] = [];
await parseOpenAPI({
value: {
openapi: '3.0.3',
info: { title: 'Test', version: '1.0.0' },
paths: {},
components: {
schemas: {
Absolute: { $ref: 'https://example.com/absolute.json' },
Relative: { $ref: './relative.json' },
},
},
},
rootURL: 'https://example.com/openapi.json',
options: {
fetch: async (url) => {
urls.push(url);
return new Response('{"type":"object"}');
},
},
});
expect(urls.sort()).toEqual([
'https://example.com/absolute.json',
'https://example.com/relative.json',
]);
});
});
function specV32(
idParameter: object = {
name: 'id',
in: 'path',
required: true,
schema: { type: 'string', minLength: 1 },
}
) {
return {
openapi: '3.2.0',
info: { title: 'Users', version: '1.0.0' },
paths: {
'/users/{id}': {
get: {
parameters: [
idParameter,
{
name: 'role',
in: 'query',
schema: { $ref: '#/components/schemas/Role' },
},
],
responses: {
'200': {
description: 'OK',
content: {
'application/json': {
schema: { $ref: '#/components/schemas/User' },
},
},
},
},
},
},
},
components: {
schemas: {
Role: { type: 'string', minLength: 1 },
User: {
type: 'object',
properties: {
id: { type: 'string', minLength: 1 },
role: { $ref: '#/components/schemas/Role' },
},
},
},
},
};
}