|
|
@@ -1,13 +1,11 @@
|
|
|
import fs from 'fs/promises';
|
|
|
+import type { OpenAPI3 } from 'openapi-typescript';
|
|
|
import { tmpdir } from 'os';
|
|
|
import path from 'path';
|
|
|
-
|
|
|
-import type { OpenAPI3 } from 'openapi-typescript';
|
|
|
import { describe, expect, it } from 'vitest';
|
|
|
|
|
|
import { generateOperationIds } from './generate-operation-ids';
|
|
|
|
|
|
-
|
|
|
async function createTempOpenAPIFile(spec: OpenAPI3): Promise<string> {
|
|
|
const tempDir = await fs.mkdtemp(path.join(tmpdir(), 'openapi-test-'));
|
|
|
const filePath = path.join(tempDir, 'openapi.json');
|
|
|
@@ -19,15 +17,14 @@ async function cleanup(filePath: string): Promise<void> {
|
|
|
try {
|
|
|
await fs.unlink(filePath);
|
|
|
await fs.rmdir(path.dirname(filePath));
|
|
|
- }
|
|
|
- catch (err) {
|
|
|
+ } catch (err) {
|
|
|
// eslint-disable-next-line no-console
|
|
|
console.error('Cleanup failed:', err);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
describe('generateOperationIds', () => {
|
|
|
- it('should generate correct operationId for simple paths', async() => {
|
|
|
+ it('should generate correct operationId for simple paths', async () => {
|
|
|
const spec: OpenAPI3 = {
|
|
|
openapi: '3.0.0',
|
|
|
info: { title: 'Test API', version: '1.0.0' },
|
|
|
@@ -46,13 +43,12 @@ describe('generateOperationIds', () => {
|
|
|
|
|
|
expect(parsed.paths['/foo'].get.operationId).toBe('getFoo');
|
|
|
expect(parsed.paths['/foo'].post.operationId).toBe('postFoo');
|
|
|
- }
|
|
|
- finally {
|
|
|
+ } finally {
|
|
|
await cleanup(filePath);
|
|
|
}
|
|
|
});
|
|
|
|
|
|
- it('should generate correct operationId for paths with parameters', async() => {
|
|
|
+ it('should generate correct operationId for paths with parameters', async () => {
|
|
|
const spec: OpenAPI3 = {
|
|
|
openapi: '3.0.0',
|
|
|
info: { title: 'Test API', version: '1.0.0' },
|
|
|
@@ -72,14 +68,15 @@ describe('generateOperationIds', () => {
|
|
|
const parsed = JSON.parse(result);
|
|
|
|
|
|
expect(parsed.paths['/foo/{id}'].get.operationId).toBe('getFooById');
|
|
|
- expect(parsed.paths['/foo/{id}/bar/{page}'].get.operationId).toBe('getBarByPageByIdForFoo');
|
|
|
- }
|
|
|
- finally {
|
|
|
+ expect(parsed.paths['/foo/{id}/bar/{page}'].get.operationId).toBe(
|
|
|
+ 'getBarByPageByIdForFoo',
|
|
|
+ );
|
|
|
+ } finally {
|
|
|
await cleanup(filePath);
|
|
|
}
|
|
|
});
|
|
|
|
|
|
- it('should generate correct operationId for nested resources', async() => {
|
|
|
+ it('should generate correct operationId for nested resources', async () => {
|
|
|
const spec: OpenAPI3 = {
|
|
|
openapi: '3.0.0',
|
|
|
info: { title: 'Test API', version: '1.0.0' },
|
|
|
@@ -96,13 +93,12 @@ describe('generateOperationIds', () => {
|
|
|
const parsed = JSON.parse(result);
|
|
|
|
|
|
expect(parsed.paths['/foo/bar'].get.operationId).toBe('getBarForFoo');
|
|
|
- }
|
|
|
- finally {
|
|
|
+ } finally {
|
|
|
await cleanup(filePath);
|
|
|
}
|
|
|
});
|
|
|
|
|
|
- it('should preserve existing operationId when overwriteExisting is false', async() => {
|
|
|
+ it('should preserve existing operationId when overwriteExisting is false', async () => {
|
|
|
const existingOperationId = 'existingOperation';
|
|
|
const spec: OpenAPI3 = {
|
|
|
openapi: '3.0.0',
|
|
|
@@ -118,17 +114,18 @@ describe('generateOperationIds', () => {
|
|
|
|
|
|
const filePath = await createTempOpenAPIFile(spec);
|
|
|
try {
|
|
|
- const result = await generateOperationIds(filePath, { overwriteExisting: false });
|
|
|
+ const result = await generateOperationIds(filePath, {
|
|
|
+ overwriteExisting: false,
|
|
|
+ });
|
|
|
const parsed = JSON.parse(result);
|
|
|
|
|
|
expect(parsed.paths['/foo'].get.operationId).toBe(existingOperationId);
|
|
|
- }
|
|
|
- finally {
|
|
|
+ } finally {
|
|
|
await cleanup(filePath);
|
|
|
}
|
|
|
});
|
|
|
|
|
|
- it('should overwrite existing operationId when overwriteExisting is true', async() => {
|
|
|
+ it('should overwrite existing operationId when overwriteExisting is true', async () => {
|
|
|
const spec: OpenAPI3 = {
|
|
|
openapi: '3.0.0',
|
|
|
info: { title: 'Test API', version: '1.0.0' },
|
|
|
@@ -143,17 +140,18 @@ describe('generateOperationIds', () => {
|
|
|
|
|
|
const filePath = await createTempOpenAPIFile(spec);
|
|
|
try {
|
|
|
- const result = await generateOperationIds(filePath, { overwriteExisting: true });
|
|
|
+ const result = await generateOperationIds(filePath, {
|
|
|
+ overwriteExisting: true,
|
|
|
+ });
|
|
|
const parsed = JSON.parse(result);
|
|
|
|
|
|
expect(parsed.paths['/foo'].get.operationId).toBe('getFoo');
|
|
|
- }
|
|
|
- finally {
|
|
|
+ } finally {
|
|
|
await cleanup(filePath);
|
|
|
}
|
|
|
});
|
|
|
|
|
|
- it('should generate correct operationId for root path', async() => {
|
|
|
+ it('should generate correct operationId for root path', async () => {
|
|
|
const spec: OpenAPI3 = {
|
|
|
openapi: '3.0.0',
|
|
|
info: { title: 'Test API', version: '1.0.0' },
|
|
|
@@ -170,13 +168,12 @@ describe('generateOperationIds', () => {
|
|
|
const parsed = JSON.parse(result);
|
|
|
|
|
|
expect(parsed.paths['/'].get.operationId).toBe('getRoot');
|
|
|
- }
|
|
|
- finally {
|
|
|
+ } finally {
|
|
|
await cleanup(filePath);
|
|
|
}
|
|
|
});
|
|
|
|
|
|
- it('should generate operationId for all HTTP methods', async() => {
|
|
|
+ it('should generate operationId for all HTTP methods', async () => {
|
|
|
const spec: OpenAPI3 = {
|
|
|
openapi: '3.0.0',
|
|
|
info: { title: 'Test API', version: '1.0.0' },
|
|
|
@@ -207,13 +204,14 @@ describe('generateOperationIds', () => {
|
|
|
expect(parsed.paths['/foo'].options.operationId).toBe('optionsFoo');
|
|
|
expect(parsed.paths['/foo'].head.operationId).toBe('headFoo');
|
|
|
expect(parsed.paths['/foo'].trace.operationId).toBe('traceFoo');
|
|
|
- }
|
|
|
- finally {
|
|
|
+ } finally {
|
|
|
await cleanup(filePath);
|
|
|
}
|
|
|
});
|
|
|
|
|
|
- it('should throw error for non-existent file', async() => {
|
|
|
- await expect(generateOperationIds('non-existent-file.json')).rejects.toThrow();
|
|
|
+ it('should throw error for non-existent file', async () => {
|
|
|
+ await expect(
|
|
|
+ generateOperationIds('non-existent-file.json'),
|
|
|
+ ).rejects.toThrow();
|
|
|
});
|
|
|
});
|