openapi-to-application-code

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Generate Application from OpenAPI Spec

根据OpenAPI规范生成应用程序

Your goal is to generate a complete, working application from an OpenAPI specification using the active framework's conventions and best practices.
你的目标是使用当前框架的约定和最佳实践,根据OpenAPI规范生成一个完整、可运行的应用程序。

Input Requirements

输入要求

  1. OpenAPI Specification: Provide either:
    • A URL to the OpenAPI spec (e.g.,
      https://api.example.com/openapi.json
      )
    • A local file path to the OpenAPI spec
    • The full OpenAPI specification content pasted directly
  2. Project Details (if not in spec):
    • Project name and description
    • Target framework and version
    • Package/namespace naming conventions
    • Authentication method (if not specified in OpenAPI)
  1. OpenAPI规范:提供以下任一内容:
    • OpenAPI规范的URL(例如:
      https://api.example.com/openapi.json
    • OpenAPI规范的本地文件路径
    • 直接粘贴完整的OpenAPI规范内容
  2. 项目详情(如果规范中未包含):
    • 项目名称和描述
    • 目标框架及版本
    • 包/命名空间命名约定
    • 认证方式(如果OpenAPI中未指定)

Generation Process

生成流程

Step 1: Analyze the OpenAPI Specification

步骤1:分析OpenAPI规范

  • Validate the OpenAPI spec for completeness and correctness
  • Identify all endpoints, HTTP methods, request/response schemas
  • Extract authentication requirements and security schemes
  • Note data model relationships and constraints
  • Flag any ambiguities or incomplete definitions
  • 验证OpenAPI规范的完整性和正确性
  • 识别所有端点、HTTP方法、请求/响应 schema
  • 提取认证要求和安全方案
  • 记录数据模型关系和约束
  • 标记任何模糊或不完整的定义

Step 2: Design Application Architecture

步骤2:设计应用架构

  • Plan directory structure appropriate for the framework
  • Identify controller/handler grouping by resource or domain
  • Design service layer organization for business logic
  • Plan data models and entity relationships
  • Design configuration and initialization strategy
  • 规划适合框架的目录结构
  • 根据资源或领域识别控制器/处理器分组
  • 设计业务逻辑的服务层组织
  • 规划数据模型和实体关系
  • 设计配置和初始化策略

Step 3: Generate Application Code

步骤3:生成应用代码

  • Create project structure with build/package configuration files
  • Generate models/DTOs from OpenAPI schemas
  • Generate controllers/handlers with route mappings
  • Generate service layer with business logic
  • Generate repository/data access layer if applicable
  • Add error handling, validation, and logging
  • Generate configuration and startup code
  • 创建带有构建/包配置文件的项目结构
  • 根据OpenAPI schema生成模型/DTO
  • 生成带有路由映射的控制器/处理器
  • 生成包含业务逻辑的服务层
  • 生成仓库/数据访问层(如适用)
  • 添加错误处理、验证和日志功能
  • 生成配置和启动代码

Step 4: Add Supporting Files

步骤4:添加支持文件

  • Generate appropriate unit tests for services and controllers
  • Create README with setup and running instructions
  • Add .gitignore and environment configuration templates
  • Generate API documentation files
  • Create example requests/integration tests
  • 为服务和控制器生成合适的单元测试
  • 创建包含设置和运行说明的README
  • 添加.gitignore和环境配置模板
  • 生成API文档文件
  • 创建示例请求/集成测试

Output Structure

输出结构

The generated application will include:
project-name/
├── README.md                      # Setup and usage instructions
├── [build-config]                 # Framework-specific build files (pom.xml, build.gradle, package.json, etc.)
├── src/
│   ├── main/
│   │   ├── [language]/
│   │   │   ├── controllers/       # HTTP endpoint handlers
│   │   │   ├── services/          # Business logic
│   │   │   ├── models/            # Data models and DTOs
│   │   │   ├── repositories/      # Data access (if applicable)
│   │   │   └── config/            # Application configuration
│   │   └── resources/             # Configuration files
│   └── test/
│       ├── [language]/
│       │   ├── controllers/       # Controller tests
│       │   └── services/          # Service tests
│       └── resources/             # Test configuration
├── .gitignore
├── .env.example                   # Environment variables template
└── docker-compose.yml             # Optional: Docker setup (if applicable)
生成的应用将包含:
project-name/
├── README.md                      # Setup and usage instructions
├── [build-config]                 # Framework-specific build files (pom.xml, build.gradle, package.json, etc.)
├── src/
│   ├── main/
│   │   ├── [language]/
│   │   │   ├── controllers/       # HTTP endpoint handlers
│   │   │   ├── services/          # Business logic
│   │   │   ├── models/            # Data models and DTOs
│   │   │   ├── repositories/      # Data access (if applicable)
│   │   │   └── config/            # Application configuration
│   │   └── resources/             # Configuration files
│   └── test/
│       ├── [language]/
│       │   ├── controllers/       # Controller tests
│       │   └── services/          # Service tests
│       └── resources/             # Test configuration
├── .gitignore
├── .env.example                   # Environment variables template
└── docker-compose.yml             # Optional: Docker setup (if applicable)

Best Practices Applied

应用的最佳实践

  • Framework Conventions: Follows framework-specific naming, structure, and patterns
  • Separation of Concerns: Clear layers with controllers, services, and repositories
  • Error Handling: Comprehensive error handling with meaningful responses
  • Validation: Input validation and schema validation throughout
  • Logging: Structured logging for debugging and monitoring
  • Testing: Unit tests for services and controllers
  • Documentation: Inline code documentation and setup instructions
  • Security: Implements authentication/authorization from OpenAPI spec
  • Scalability: Design patterns support growth and maintenance
  • 框架约定:遵循框架特定的命名、结构和模式
  • 关注点分离:控制器、服务和仓库各层职责清晰
  • 错误处理:全面的错误处理,返回有意义的响应
  • 验证:全程实现输入验证和schema验证
  • 日志:用于调试和监控的结构化日志
  • 测试:为服务和控制器编写单元测试
  • 文档:内联代码文档和设置说明
  • 安全:实现OpenAPI规范中的认证/授权机制
  • 可扩展性:设计模式支持应用的增长和维护

Next Steps

后续步骤

After generation:
  1. Review the generated code structure and make customizations as needed
  2. Install dependencies according to framework requirements
  3. Configure environment variables and database connections
  4. Run tests to verify generated code
  5. Start the development server
  6. Test endpoints using the provided examples
生成完成后:
  1. 审查生成的代码结构,根据需要进行自定义修改
  2. 根据框架要求安装依赖
  3. 配置环境变量和数据库连接
  4. 运行测试以验证生成的代码
  5. 启动开发服务器
  6. 使用提供的示例测试端点

Questions to Ask if Needed

必要时可询问的问题

  • Should the application include database/ORM setup, or just in-memory/mock data?
  • Do you want Docker configuration for containerization?
  • Should authentication be JWT, OAuth2, API keys, or basic auth?
  • Do you need integration tests or just unit tests?
  • Any specific database technology preferences?
  • Should the API include pagination, filtering, and sorting examples?
  • 应用是否需要包含数据库/ORM设置,还是仅使用内存/模拟数据?
  • 是否需要用于容器化的Docker配置?
  • 认证方式应该是JWT、OAuth2、API密钥还是基础认证?
  • 是否需要集成测试还是仅需单元测试?
  • 有没有特定的数据库技术偏好?
  • API是否需要包含分页、过滤和排序示例?