一个 Dart 封装的 OpenAPI Generator,灵感来自 Node.js 版本,可无缝地从 OpenAPI 规范生成 Dart 代码。它支持多个平台,包括 macOS、Windows 和 Linux,非常适合需要自动化 API 客户端创建的开发者。该工具通过基于 OpenAPI 3.x 规范生成类型安全、结构良好的 Dart 模型和服务,简化了与 RESTful API 的集成。在需要频繁更新 API 合同的项目中尤为有用。
适合场景
自动化 API 客户端生成
与CI/CD流水线集成
保持 API 客户端的最新状态
从 OpenAPI 规范生成模型和服务
支持团队在API契约上的协作
主要特点
跨平台支持
与 OpenAPI 3.x 集成
类型安全的 Dart 输出
命令行界面
可自定义的代码生成模板
适合对象
使用 OpenAPI 规范的 Dart 开发人员
需要自动化 API 客户端更新的团队
具有不断演进的 REST API 的项目
涉及 API 变更的 CI/CD 工作流
熟悉 openapi-generator 的开发者
使用建议
需要本地安装 openapi-generator
可能需要为自定义模板进行配置
取决于OpenAPI规范的质量
不是运行时库,而是一个代码生成器
除了命令行界面外,IDE 集成有限
原始 README
以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。
openapi-generator-dart
Like this library? Give us a star or a like!
⚠️ Java Requirement
Java is required to use this library.
The OpenAPI Generator CLI is a Java application.
Please ensure you have Java (version 8 or higher) installed and available in your system PATH.
You can check your Java installation with:
java -version
If you do not have Java installed, download it from Adoptium or Oracle.
⚠️ Deprecation & Breaking Change Notice
skipIfSpecIsUnchanged is Deprecated
The skipIfSpecIsUnchanged option is now deprecated and will be removed in the next major release.
Local OpenAPI specs are now automatically watched for changes.
If your spec file is in the lib/ folder, this works out of the box.
If your spec file is outside lib/, you must update or add a build.yaml to include your spec file as a source.
Example:
targets:$default:sources:-$package$-lib/**-openapi.yaml# or your spec file path
Remote specs:
Use the forceAlwaysRun (defaults to false) option to ensure the generator always runs.
This option is ignored for local specs.
When enabled, it modifies the annotated file to force regeneration. This might cause issues such as merge conflicts
Overview
This repository provides Dart/Flutter libraries for generating OpenAPI client SDKs directly from your OpenAPI specification. Inspired by Openapi Generator Cli (npm), it enables seamless integration into Dart and Flutter projects.
Libraries
Library
Description
Latest Version
openapi-generator
Dev dependency for generating OpenAPI client SDK via Dart source gen (usage)
openapi-generator-annotations
Annotations for configuring OpenAPI client SDK generation (usage)
If you are having issues with the generated code, this is not an problem with this package and creating an issue here will not help solve it. Its best to create the issue in the base OpenApi library since this package is a wrapper around that library for ease of use with Flutter/dart.
Below are some advanced configurations you may try.
Custom Templates:
Use the templateDirectory parameter to specify a custom code generation template.
Type & Import Mappings:
Use typeMappings and importMappings to control how OpenAPI types and models are mapped in Dart.
Reserved Words:
Use reservedWordsMappings to avoid conflicts with Dart reserved words.
Dependency Conflicts:
Use dependency_overrides in the generated package's pubspec.yaml and add pubspec.yaml to .openapi-generator-ignore to prevent overwrites.
Incorrect Generated Code:
Fix your OpenAPI spec (preferred).
Manually edit the generated code and add the files to .openapi-generator-ignore to prevent them from being overwritten.
.openapi-generator-ignore Example:
# Ignore all test files
test/*
# Ignore pubspec.yaml to preserve manual changes
pubspec.yaml