openapi-generator

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

OpenAPI Generator - Quick Reference

OpenAPI Generator - 快速参考

When to Use This Skill

何时使用该工具

  • Generate type-safe API clients from OpenAPI specs
  • Generate server stubs from OpenAPI specs
  • Maintain sync between API and code
  • Multi-language client generation (Java, Python, Go, etc.)
  • Spring Boot server generation
Deep Knowledge: Use
mcp__documentation__fetch_docs
with technology:
openapi-generator
for comprehensive documentation.
  • 从OpenAPI规范生成类型安全的API客户端
  • 从OpenAPI规范生成服务端存根
  • 保持API与代码的同步
  • 多语言客户端生成(Java、Python、Go等)
  • Spring Boot服务端生成
深度文档: 使用
mcp__documentation__fetch_docs
工具,指定技术为
openapi-generator
以获取完整文档。

CLI Usage

CLI 使用方法

Install

安装

bash
npm install @openapitools/openapi-generator-cli -g
bash
npm install @openapitools/openapi-generator-cli -g

or

or

brew install openapi-generator
undefined
brew install openapi-generator
undefined

Generate TypeScript Client

生成TypeScript客户端

bash
openapi-generator-cli generate \
  -i api.yaml \
  -g typescript-fetch \
  -o ./generated/api \
  --additional-properties=supportsES6=true,typescriptThreePlus=true
bash
openapi-generator-cli generate \
  -i api.yaml \
  -g typescript-fetch \
  -o ./generated/api \
  --additional-properties=supportsES6=true,typescriptThreePlus=true

Generate TypeScript Axios Client

生成TypeScript Axios客户端

bash
openapi-generator-cli generate \
  -i api.yaml \
  -g typescript-axios \
  -o ./generated/api
bash
openapi-generator-cli generate \
  -i api.yaml \
  -g typescript-axios \
  -o ./generated/api

Generate Java Client

生成Java客户端

bash
openapi-generator-cli generate \
  -i api.yaml \
  -g java \
  -o ./generated/api \
  --additional-properties=library=webclient,dateLibrary=java8
bash
openapi-generator-cli generate \
  -i api.yaml \
  -g java \
  -o ./generated/api \
  --additional-properties=library=webclient,dateLibrary=java8

Generate Spring Server

生成Spring服务端

bash
openapi-generator-cli generate \
  -i api.yaml \
  -g spring \
  -o ./generated/server \
  --additional-properties=interfaceOnly=true,useSpringBoot3=true
bash
openapi-generator-cli generate \
  -i api.yaml \
  -g spring \
  -o ./generated/server \
  --additional-properties=interfaceOnly=true,useSpringBoot3=true

NPM Script Integration

NPM 脚本集成

package.json

package.json

json
{
  "scripts": {
    "generate:api": "openapi-generator-cli generate -i ./api/openapi.yaml -g typescript-fetch -o ./src/generated/api"
  }
}
json
{
  "scripts": {
    "generate:api": "openapi-generator-cli generate -i ./api/openapi.yaml -g typescript-fetch -o ./src/generated/api"
  }
}

Maven Plugin

Maven 插件

xml
<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>7.0.0</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${project.basedir}/src/main/resources/api.yaml</inputSpec>
                <generatorName>spring</generatorName>
                <configOptions>
                    <interfaceOnly>true</interfaceOnly>
                    <useSpringBoot3>true</useSpringBoot3>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>
xml
<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>7.0.0</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${project.basedir}/src/main/resources/api.yaml</inputSpec>
                <generatorName>spring</generatorName>
                <configOptions>
                    <interfaceOnly>true</interfaceOnly>
                    <useSpringBoot3>true</useSpringBoot3>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>

Common Generators

常见生成器

GeneratorDescription
typescript-fetchTypeScript with Fetch API
typescript-axiosTypeScript with Axios
javaJava client
springSpring Boot server
pythonPython client
goGo client
生成器描述
typescript-fetch基于Fetch API的TypeScript客户端
typescript-axios基于Axios的TypeScript客户端
javaJava客户端
springSpring Boot服务端
pythonPython客户端
goGo客户端

When NOT to Use This Skill

何时不使用该工具

  • TypeScript-only projects (use
    openapi-codegen
    with openapi-typescript instead)
  • Writing OpenAPI specifications (use
    openapi
    skill)
  • GraphQL code generation (use
    graphql-codegen
    skill)
  • tRPC projects (use
    trpc
    skill)
  • Simple type-only generation (openapi-typescript is lighter)
  • 仅TypeScript项目(请结合openapi-typescript使用
    openapi-codegen
  • 编写OpenAPI规范(请使用
    openapi
    工具)
  • GraphQL代码生成(请使用
    graphql-codegen
    工具)
  • tRPC项目(请使用
    trpc
    工具)
  • 仅简单类型生成(openapi-typescript更轻量)

Anti-Patterns

反模式

Anti-PatternWhy It's BadSolution
Committing generated codeMerge conflicts, stale codeAdd to .gitignore, generate in build
Not pinning generator versionBreaking changes between versionsLock version in package.json/pom.xml
Editing generated filesLost on regenerationExtend classes or use custom templates
No spec validation before generationInvalid code generatedUse @redocly/cli lint first
Generating entire API for one endpointBloated clientUse tags or paths filter
Missing operationId in specPoor method namesAdd operationId to all operations
Not configuring additional propertiesSuboptimal outputUse additionalProperties for customization
Generating without .openapi-generator-ignoreOverwrites custom filesAdd ignore file for manual code
反模式问题所在解决方案
提交生成的代码易引发合并冲突、代码过时添加到.gitignore,在构建阶段生成
未固定生成器版本版本间可能存在破坏性变更在package.json/pom.xml中锁定版本
修改生成的文件重新生成后修改内容会丢失继承类或使用自定义模板
生成前未验证规范可能生成无效代码先使用@redocly/cli进行校验
为单个接口生成完整API客户端代码臃肿使用标签或路径过滤
规范中缺少operationId生成的方法命名不佳为所有操作添加operationId
未配置额外属性输出结果不够优化使用additionalProperties进行自定义配置
生成时未使用.openapi-generator-ignore会覆盖自定义文件添加忽略文件以保护手动编写的代码

Quick Troubleshooting

快速故障排查

IssuePossible CauseSolution
Generation failsInvalid OpenAPI specValidate with
@redocly/cli lint
Missing methodsNo operationId in specAdd operationId to all paths
Compilation errors in generated codeSpec type mismatchCheck schema types, run with --skip-validate-spec to debug
Wrong package namesConfig not setUse --additional-properties for package config
Duplicate model namesSame schema name in different tagsRename schemas or use x-model-name
Maven plugin not runningWrong phase or configurationCheck plugin execution phase
NPM global command not foundNot installed globallyInstall with npm install -g or use npx
Generated code has wrong typesGenerator doesn't support typeUse custom templates or different generator
问题可能原因解决方案
生成失败OpenAPI规范无效使用
@redocly/cli lint
验证规范
缺少方法规范中缺少operationId为所有路径添加operationId
生成的代码编译报错规范类型不匹配检查Schema类型,使用--skip-validate-spec参数调试
包名错误未配置相关参数使用--additionalProperties配置包名
模型名称重复不同标签下存在同名Schema重命名Schema或使用x-model-name
Maven插件未运行阶段或配置错误检查插件执行阶段
NPM全局命令找不到未全局安装使用npm install -g安装或使用npx
生成的代码类型错误生成器不支持该类型使用自定义模板或更换生成器