搜尋套件、儲存庫與 APP 分類

套件詳情v7.0.0

openapi_generator_cli

一個 Dart 封裝器,用於 openapi-generator,靈感來自 Node 實現。

macOSWindowsLinux
近 30 天下載
2萬
喜歡數
21
Pub 分數
160

安裝套件

flutter pub add openapi_generator_cli

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

一個 Dart 封裝的 OpenAPI Generator,靈感來自 Node.js 版本,可無縫地從 OpenAPI 規範生成 Dart 程式碼。它支援多個平台,包括 macOS、Windows 和 Linux,非常適合需要自動化 API 客戶端建立的開發者。該工具透過基於 OpenAPI 3.x 規範產生類型安全、結構良好的 Dart 模型與服務,簡化與 RESTful API 的整合。在需要頻繁更新 API 合約的專案中特別實用。

適用情境

  • 自動化 API 客戶端生成
  • 與CI/CD流水線整合
  • 維持 API 客戶端的最新狀態
  • 從 OpenAPI 規範生成模型和服務
  • 支援團隊在API契約上的協作

主要特色

  • 跨平台支援
  • 與 OpenAPI 3.x 集成
  • 類型安全的 Dart 輸出
  • 命令列介面
  • 可自定義的程式碼產生範本

適合對象

  • 使用 OpenAPI 規範的 Dart 開發人員
  • 需要自動化 API 客戶端更新的團隊
  • 具有不斷演進的 REST API 的專案
  • 涉及 API 變更的 CI/CD 工作流程
  • 熟悉 openapi-generator 的開發者

使用建議

  • 需要本地安裝 openapi-generator
  • 可能需要為自定義模板進行配置
  • 取決於OpenAPI規範的品質
  • 不是執行時庫,而是一個程式碼產生器
  • 除了 CLI 外,IDE 集成有限

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

openapi-generator-dart

pub package Pub Likes Pub Points Pub Popularity GitHub Repo stars codecov

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) 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

Quick Start

1. Add Dependencies

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 the beta branch:

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
2. Annotate Your Dart Class

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 {}
3. Generate the SDK

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.


Next Generation Features (v5.0+)

  • Spec Caching: Automatically caches your OpenAPI spec for faster incremental builds.
  • Remote Spec Support: Pulls and caches remote specs; always fetches the latest version unless using a local copy.
  • Smart Generation: Skips code generation if there are no changes in the spec or based on flags.
  • All previous features remain available.

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 {}

Advanced Configuration

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 {}

Troubleshooting

Common Issues
  • 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

所屬分類

依賴

依賴清單 4 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。