一個專為 openapi_generator 工具設計的 Dart 注解套件,讓開發者能直接從 OpenAPI 規範產生 Dart 模型與 API 客戶端。它透過基於標準化 API 定義的自動化程式碼產生,簡化將 RESTful API 整合至 Flutter 及其他 Dart 應用程式的過程。註解會引導產生器產出符合 OpenAPI 3.0+ 標準的乾淨、類型安全程式碼。此套件非常適合遵循 API 優先開發實務的團隊。
適用情境
從 OpenAPI 規範生成客戶端代碼
自動化 API 模型建立
啟用 API-first 開發工作流程
在Flutter應用中整合REST API
簡化後端與前端的同步
主要特色
OpenAPI 模式定義的註釋
與 openapi_generator 工具整合
支援 OpenAPI 3.0+
類型安全的生成程式碼
跨平台相容性
適合對象
以 API 為先的開發團隊
Flutter 應用程式使用 REST API
使用 OpenAPI 規範的專案
尋求自動化代碼生成的開發者
需要一致 API 合同的團隊
使用建議
需要設置 openapi_generator 工具
依賴於準確的 OpenAPI 規範
生成的代碼可能需要手動調整
不適用於執行時間API修改
僅限靜態程式碼產生
原始 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