Scripts2026年7月12日·1 分钟阅读

Swagger Codegen — Generate API Client Libraries from OpenAPI Definitions

Swagger Codegen reads OpenAPI 2.0 and 3.x specification files and generates client SDKs, server stubs, and API documentation in over 40 languages and frameworks automatically.

Agent 就绪

这个资产会安全暂存

这个资产会先安全暂存。复制的指令会要求 Agent 读取暂存文件,并在激活脚本、MCP 配置或全局配置前先确认。

Stage only · 29/100策略:需暂存
Agent 入口
任意 MCP/CLI Agent
类型
CLI Tool
安装
Single
信任
信任等级:Established
入口
Swagger Codegen
安全暂存命令
npx -y tokrepo@latest install 7d67de0b-7def-11f1-9bc6-00163e2b0d79 --target codex

先暂存文件;激活前需要读取暂存 README 和安装计划。

Introduction

Swagger Codegen automates the creation of API client libraries, server stubs, and documentation from OpenAPI (formerly Swagger) specification files. It eliminates the tedious work of hand-writing HTTP clients and server boilerplate, letting teams focus on business logic while maintaining consistent API contracts across services.

What Swagger Codegen Does

  • Generates client SDKs in 40+ languages including Java, Python, TypeScript, Go, Ruby, C#, and Swift
  • Produces server stubs for frameworks like Spring Boot, Flask, Express, and ASP.NET
  • Creates interactive API documentation from spec files
  • Supports both OpenAPI 2.0 (Swagger) and OpenAPI 3.x specifications
  • Provides a CLI, Maven plugin, and Docker image for integration into any workflow

Architecture Overview

Swagger Codegen parses an OpenAPI specification into an internal model, then applies language-specific Mustache templates to produce source code. Each language generator defines its own template set, type mappings, and file structure. Users can override individual templates or create entirely custom generators by extending the base codegen classes.

Self-Hosting & Configuration

  • Run directly via JAR: java -jar swagger-codegen-cli.jar generate -i spec.yaml -l java -o ./out
  • Available as a Docker image for CI pipelines: docker run swaggerapi/swagger-codegen-cli generate ...
  • Customize output with a JSON config file passed via -c config.json to control package names, base paths, and library choices
  • Override default templates with -t /path/to/templates for full control over generated code style
  • Integrate into Maven or Gradle builds with the official plugin for automatic regeneration on spec changes

Key Features

  • Broad language coverage with 40+ officially maintained generators
  • Template-driven architecture allows full customization of generated output
  • Supports generating models-only, APIs-only, or both together
  • Built-in support for authentication schemes (OAuth2, API key, Basic)
  • Active maintenance with regular releases and community contributions

Comparison with Similar Tools

  • OpenAPI Generator — Community fork with more generators and faster release cadence; Swagger Codegen remains the original project with stable, well-tested output
  • NSwag — Focused on .NET ecosystem with tighter C#/TypeScript integration
  • AutoRest — Microsoft's generator optimized for Azure API patterns
  • Orval — TypeScript-first with React Query and Zod integration
  • oapi-codegen — Lightweight Go-specific generator producing idiomatic Go code

FAQ

Q: What is the difference between Swagger Codegen and OpenAPI Generator? A: OpenAPI Generator was forked from Swagger Codegen 2.x in 2018. Both read OpenAPI specs and generate code, but they have diverged in generator count, template style, and release process. Choose based on your language needs and preferred community.

Q: Can I use Swagger Codegen with OpenAPI 3.1? A: Swagger Codegen 3.x supports OpenAPI 3.0. For 3.1 support, check the latest release notes or consider OpenAPI Generator which added 3.1 support earlier.

Q: How do I customize the generated code? A: Export the default templates with swagger-codegen config-help -l <lang>, modify the Mustache files, and pass them back with the -t flag.

Q: Does it support generating only models without API classes? A: Yes, use --model-package with --generate-models true --generate-apis false to produce only data transfer objects.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产