swagger_parser 1.7.0 copy "swagger_parser: ^1.7.0" to clipboard
swagger_parser: ^1.7.0 copied to clipboard

Package that generates REST clients and data classes from OpenApi definition file

Swagger Parser #

pub version pub likes dart style Star on Github Last commit on Github Tests

Dart package that generates REST clients and data classes from OpenApi definition file #

Features #

  • Supports OpenApi v2, v3.0 and v3.1
  • Support JSON and YAML format
  • Generate REST client files based on Retrofit
  • Generate data classes (also on freezed)
  • Support for multiple languages (Dart, Kotlin)
  • Web interface at https://carapacik.github.io/swagger_parser

Usage #

Install #

In your pubspec.yaml, add the following dependencies:

dependencies:
  # dio: ^5.3.0
  # freezed_annotation: ^2.4.1 # for freezed
  # json_annotation: ^4.8.1
  # retrofit: ^4.0.1

dev_dependencies:
  # build_runner: ^2.4.6
  # freezed: ^2.4.2 # for freezed
  # json_serializable: ^6.7.1
  # retrofit_generator: ^7.0.8
  swagger_parser:

Configure package #

Add your OpenApi json file configuration to your pubspec.yaml or create a new config file called swagger_parser.yaml. An example of YAML is shown below

swagger_parser:
  # Required. Sets the OpenApi schema path directory for api definition
  schema_path: assets/openapi.json
  # Required. Sets output directory for generated files (Clients and Dtos)
  output_directory: lib/api
  # Optional. Sets the programming language. Current available languages are: dart, kotlin. Default: dart
  language: dart
  # Optional (dart only). Set 'true' to generate data classes using freezed package. Default: false
  freezed: false
  # Optional (dart only). Set 'true' to generate interface with all clients instances. Default: true
  root_interface: true
  # Optional. Set 'true' to put all clients in one folder. Default: false
  squish_clients: false
  # Optional. Set postfix for Client class and file. Default: Client
  client_postfix: Client
  # Optional. Set 'true' to use only the endpoint path for the method name. Set 'false' to use operationId. Default: false
  path_method_name: false
  # Optional. Set 'true' to include toJson() in enums. If set to false, serialization will use .name instead. Default: false
  enums_to_json: false
  # Optional. Set 'true' to set enum prefix from parent component. Default: false
  enums_prefix: false
  # Optional. Set 'false' to not put a comment at the beginning of the generated files. Default: true
  mark_files_as_generated: true
  # Optional. Set regex replacement rules for the names of the generated classes/enums. All rules are applied in order.
  replacement_rules: 
    # Example of rule
    - pattern: "[0-9]+"
      replacement: ""

Run the generator #

To generate boilerplate code, run the generate program inside directory where your pubspec.yaml file is located:

dart run swagger_parser:generate

If you name your configuration file something other than swagger_parser.yaml or pubspec.yaml you will need to specify the name of the YAML file as an argument.

dart run swagger_parser:generate -f <path to your config file>

(Only for freezed) Generate files using build_runner for retrofit, json_serializable and freezed #

For freezed with retrofit use build.yaml file with this content:

global_options:
  freezed:
    runs_before:
      - json_serializable
  json_serializable:
    runs_before:
      - retrofit_generator

To run the code generation with build_runner, execute the following command:

dart run build_runner build -d
56
likes
0
pub points
79%
popularity

Publisher

verified publishercarapacik.dev

Package that generates REST clients and data classes from OpenApi definition file

Homepage
Repository (GitHub)
View/report issues

Topics

#swagger #openapi #codegen #api #json

License

unknown (LICENSE)

Dependencies

args, collection, path, yaml

More

Packages that depend on swagger_parser