一个 Flutter/Dart 包,可从 OpenAPI 规范生成客户端 SDK,灵感来自 npm 的 openapi-generator-cli。它可自动化创建类型安全的 Dart 代码以进行 API 交互,减少手动工作量和潜在错误。生成的代码支持多种平台,包括 Android、iOS、Web、macOS、Windows 和 Linux,可在不同环境中实现一致的 API 客户端使用。非常适合依赖 OpenAPI/Swagger 定义来维护其应用程序中强大且更新及时的 API 集成的团队。
适合场景
从 OpenAPI 规范自动生成 API 客户端
使用类型安全集成 REST API
跨平台 Flutter 应用 API 客户端
保持 API 合同与后端更改同步
减少 API 服务层中的样板代码
主要特点
从 OpenAPI 3.x 规范生成 Dart 代码
支持多平台输出
生成类型安全的 API 模型和服务
通过 Flutter pub add 集成
使用类似 CLI 的工作流程
适合对象
使用 OpenAPI 定义的 API 的 Flutter 开发人员
需要自动化 API 客户端更新的团队
跨平台需要一致API集成的项目
希望减少手动 API 服务编码的开发人员
使用建议
需要有效的 OpenAPI 规范文件
生成的代码可能需要针对复杂端点进行自定义
在 CI/CD 流水线中使用效果最佳,以确保一致性
不是运行时库——仅生成源代码
原始 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