openapi_generator
基于 npm 版本的 openapi-generator-cli 实现,用于生成 OpenAPI 客户端 SDK 的代码生成器。
Dart/Flutter 的 OpenAPI 生成器
基于 npm 版本的 openapi-generator-cli 实现,用于生成 OpenAPI 客户端 SDK 的代码生成器。
openapi_generator 的注解包 https://pub.dev/packages/openapi_generator。
一个 Dart 包装器,用于 openapi-generator,灵感来自 Node 实现。
—以下为英文项目原文快照,最新内容请访问 GitHub。
pub package Pub Likes Pub Points Pub Popularity GitHub Repo stars codecov
Like this library? Give us a star or a like!
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 -versionIf you do not have Java installed, download it from Adoptium or Oracle.
skipIfSpecIsUnchanged is DeprecatedskipIfSpecIsUnchanged option is now deprecated and will be removed in the next major release.lib/ folder, this works out of the box.lib/, you must update or add a build.yaml to include your spec file as a source.targets:
$default:
sources:
- $package$
- lib/**
- openapi.yaml # or your spec file path
forceAlwaysRun (defaults to false) option to ensure the generator always runs.
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.
| Library | Description | Latest Version |
|---|---|---|
| openapi-generator | Dev dependency for generating OpenAPI client SDK via Dart source gen (usage) | pub package |
| openapi-generator-annotations | Annotations for configuring OpenAPI client SDK generation (usage) | pub package |
| openapi-generator-cli | CLI wrapper for OpenAPI code generation (usage) | pub package |
Add the annotations package to your pubspec.yaml:
dependencies:
openapi_generator_annotations: ^<latest-version>
Add the generator as a dev dependency:
dev_dependencies:
openapi_generator: ^<latest-version>
Beta Features:
For beta features, use thebetabranch:dependencies: openapi_generator_annotations: git: url: https://github.com/gibahjoe/openapi-generator-dart.git ref: beta path: openapi-generator-annotations dev_dependencies: openapi_generator: git: url: https://github.com/gibahjoe/openapi-generator-dart.git ref: beta path: openapi-generator
Annotate a Dart class with @Openapi() to configure code generation:
import 'package:openapi_generator_annotations/openapi_generator_annotations.dart';
@Openapi(
additionalProperties: DioProperties(pubName: 'petstore_api', pubAuthor: 'Johnny Depp'),
inputSpec: RemoteSpec(path: 'https://petstore3.swagger.io/api/v3/openapi.json'),
typeMappings: {'Pet': 'ExamplePet'},
generatorName: Generator.dio,
runSourceGenOnOutput: true,
outputDirectory: 'api/petstore_api',
)
class Example {}
Run the build command:
dart run build_runner build --delete-conflicting-outputs
# or, for Flutter projects:
flutter pub run build_runner build --delete-conflicting-outputs
The generated SDK will appear in the specified output directory.
Example:
@Openapi(
additionalProperties: DioProperties(pubName: 'petstore_api', pubAuthor: 'Johnny Depp'),
inputSpec: RemoteSpec(path: 'https://petstore3.swagger.io/api/v3/openapi.json'),
typeMappings: {'Pet': 'ExamplePet'},
generatorName: Generator.dio,
runSourceGenOnOutput: true,
outputDirectory: 'api/petstore_api',
)
class Example {}
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.
Example:
@Openapi(
additionalProperties: DioProperties(pubName: 'custom_api', pubAuthor: 'Jane Doe'),
inputSpec: InputSpec(path: 'openapi-spec.yaml'),
generatorName: Generator.dio,
templateDirectory: 'templates/dart',
typeMappings: {'date': 'DateTime'},
importMappings: {'DateTime': 'package:my_project/date_time.dart'},
reservedWordsMappings: {'class': 'clazz'},
outputDirectory: 'api/custom_api',
)
class CustomApi {}
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:
.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