/** * OpenAPI Generator v2 测试脚本 */ import * as fs from 'fs' import * as path from 'path' // 创建测试用的 OpenAPI 3.0 文档 const testOpenAPI3Doc = { openapi: '3.0.0', info: { title: 'Test API', version: '1.0.0', description: 'Test API for generator v2' }, servers: [ { url: 'https://api.example.com/v1' } ], paths: { '/users': { get: { tags: ['User'], summary: '获取用户列表', description: '获取所有用户的列表', parameters: [ { name: 'page', in: 'query', description: '页码', schema: { type: 'integer', default: 1 } }, { name: 'size', in: 'query', description: '每页大小', schema: { type: 'integer', default: 10 } } ], responses: { '200': { description: '成功', content: { 'application/json': { schema: { type: 'object', properties: { code: { type: 'integer' }, message: { type: 'string' }, data: { type: 'array', items: { $ref: '#/components/schemas/User' } } } } } } } } }, post: { tags: ['User'], summary: '创建用户', description: '创建一个新用户', requestBody: { required: true, content: { 'application/json': { schema: { $ref: '#/components/schemas/CreateUserRequest' } } } }, responses: { '201': { description: '创建成功', content: { 'application/json': { schema: { type: 'object', properties: { code: { type: 'integer' }, message: { type: 'string' }, data: { $ref: '#/components/schemas/User' } } } } } } } } }, '/users/{id}': { get: { tags: ['User'], summary: '获取用户详情', description: '根据ID获取用户详情', parameters: [ { name: 'id', in: 'path', required: true, description: '用户ID', schema: { type: 'integer' } } ], responses: { '200': { description: '成功', content: { 'application/json': { schema: { type: 'object', properties: { code: { type: 'integer' }, message: { type: 'string' }, data: { $ref: '#/components/schemas/User' } } } } } } } }, put: { tags: ['User'], summary: '更新用户', description: '更新用户信息', parameters: [ { name: 'id', in: 'path', required: true, description: '用户ID', schema: { type: 'integer' } } ], requestBody: { required: true, content: { 'application/json': { schema: { $ref: '#/components/schemas/UpdateUserRequest' } } } }, responses: { '200': { description: '更新成功', content: { 'application/json': { schema: { type: 'object', properties: { code: { type: 'integer' }, message: { type: 'string' }, data: { $ref: '#/components/schemas/User' } } } } } } } }, delete: { tags: ['User'], summary: '删除用户', description: '删除指定用户', parameters: [ { name: 'id', in: 'path', required: true, description: '用户ID', schema: { type: 'integer' } } ], responses: { '200': { description: '删除成功', content: { 'application/json': { schema: { type: 'object', properties: { code: { type: 'integer' }, message: { type: 'string' } } } } } } } } }, '/posts': { get: { tags: ['Post'], summary: '获取文章列表', responses: { '200': { description: '成功', content: { 'application/json': { schema: { type: 'object', properties: { code: { type: 'integer' }, message: { type: 'string' }, data: { type: 'array', items: { $ref: '#/components/schemas/Post' } } } } } } } } } } }, components: { schemas: { User: { type: 'object', required: ['id', 'name', 'email'], properties: { id: { type: 'integer', description: '用户ID' }, name: { type: 'string', description: '用户名' }, email: { type: 'string', format: 'email', description: '邮箱地址' }, avatar: { type: 'string', description: '头像URL' }, status: { $ref: '#/components/schemas/UserStatus' }, createdAt: { type: 'string', format: 'date-time', description: '创建时间' }, updatedAt: { type: 'string', format: 'date-time', description: '更新时间' } } }, CreateUserRequest: { type: 'object', required: ['name', 'email'], properties: { name: { type: 'string', description: '用户名' }, email: { type: 'string', format: 'email', description: '邮箱地址' }, avatar: { type: 'string', description: '头像URL' } } }, UpdateUserRequest: { type: 'object', properties: { name: { type: 'string', description: '用户名' }, email: { type: 'string', format: 'email', description: '邮箱地址' }, avatar: { type: 'string', description: '头像URL' }, status: { $ref: '#/components/schemas/UserStatus' } } }, UserStatus: { type: 'string', enum: ['active', 'inactive', 'banned'], description: '用户状态' }, Post: { type: 'object', required: ['id', 'title', 'content', 'authorId'], properties: { id: { type: 'integer', description: '文章ID' }, title: { type: 'string', description: '文章标题' }, content: { type: 'string', description: '文章内容' }, authorId: { type: 'integer', description: '作者ID' }, author: { $ref: '#/components/schemas/User' }, tags: { type: 'array', items: { type: 'string' }, description: '标签列表' }, publishedAt: { type: 'string', format: 'date-time', description: '发布时间' } } } } } } // 创建测试用的 Swagger 2.0 文档 const testSwagger2Doc = { swagger: '2.0', info: { title: 'Test API v2', version: '1.0.0', description: 'Test API for Swagger 2.0' }, host: 'api.example.com', basePath: '/v2', schemes: ['https'], paths: { '/users': { get: { tags: ['User'], summary: '获取用户列表', parameters: [ { name: 'page', in: 'query', type: 'integer', default: 1 } ], responses: { '200': { description: '成功', schema: { type: 'object', properties: { code: { type: 'integer' }, data: { type: 'array', items: { $ref: '#/definitions/User' } } } } } } }, post: { tags: ['User'], summary: '创建用户', parameters: [ { name: 'user', in: 'body', required: true, schema: { $ref: '#/definitions/CreateUserRequest' } } ], responses: { '201': { description: '创建成功', schema: { type: 'object', properties: { code: { type: 'integer' }, data: { $ref: '#/definitions/User' } } } } } } } }, definitions: { User: { type: 'object', required: ['id', 'name'], properties: { id: { type: 'integer' }, name: { type: 'string' }, email: { type: 'string' } } }, CreateUserRequest: { type: 'object', required: ['name'], properties: { name: { type: 'string' }, email: { type: 'string' } } } } } // 创建测试文件 function createTestFiles() { const testDir = path.join(__dirname, '../.temp/test-docs') // 确保目录存在 fs.mkdirSync(testDir, { recursive: true }) // 写入测试文档 fs.writeFileSync( path.join(testDir, 'openapi3.json'), JSON.stringify(testOpenAPI3Doc, null, 2) ) fs.writeFileSync( path.join(testDir, 'swagger2.json'), JSON.stringify(testSwagger2Doc, null, 2) ) console.log('✅ 测试文档已创建') console.log(`📁 OpenAPI 3.0: ${path.join(testDir, 'openapi3.json')}`) console.log(`📁 Swagger 2.0: ${path.join(testDir, 'swagger2.json')}`) } // 生成测试命令 function generateTestCommands() { const testDir = path.join(__dirname, '../.temp/test-docs') const outputDir = path.join(__dirname, '../.temp/generated') console.log('\n🚀 测试命令:') console.log('\n1. 测试 OpenAPI 3.0:') console.log(`npm run build && node dist/bin/generateFromOpenAPI.js -i "${path.join(testDir, 'openapi3.json')}" -o "${outputDir}/openapi3" -n "testApi3" -r "/users"`) console.log('\n2. 测试 Swagger 2.0:') console.log(`npm run build && node dist/bin/generateFromOpenAPI.js -i "${path.join(testDir, 'swagger2.json')}" -o "${outputDir}/swagger2" -n "testApi2" -r "/users"`) console.log('\n3. 测试 Vite 模式:') console.log(`npm run build && node dist/bin/generateFromOpenAPI.js -i "${path.join(testDir, 'openapi3.json')}" -o "${outputDir}/vite" -n "testApiVite" --vite`) console.log('\n4. 对比原版生成器:') console.log(`npm run build && node dist/bin/generateFromSwagger.js -i "${path.join(testDir, 'openapi3.json')}" -o "${outputDir}/original" -n "testApiOriginal"`) } // 执行测试 if (require.main === module) { createTestFiles() generateTestCommands() console.log('\n📋 使用说明:') console.log('1. 运行 npm run build 编译 TypeScript') console.log('2. 复制上面的测试命令执行') console.log('3. 查看生成的文件对比效果') console.log('4. 验证类型定义和配置文件的正确性') }