一個 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