一个专为 openapi_generator 工具设计的 Dart 注解包,使开发者能够直接从 OpenAPI 规范生成 Dart 模型和 API 客户端。它通过基于标准化 API 定义的自动化代码生成,简化了将 RESTful API 集成到 Flutter 和其他 Dart 应用程序中的过程。注解指导生成器生成符合 OpenAPI 3.0+ 标准的干净、类型安全的代码。该包非常适合遵循 API 优先开发实践的团队。
适合场景
从 OpenAPI 规范生成客户端代码
自动化 API 模型创建
启用 API 驱动的开发工作流程
在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