swagger_parser 0.3.1
swagger_parser: ^0.3.1 copied to clipboard
Generate REST clients and data classes from swagger json file
Swagger Parser #
Dart package that generates REST clients and data classes from swagger json file #
Features #
- Supports OpenApi v2, v3.0 and v3.1
- 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
Installation #
Add dependency #
In your pubspec.yaml, add the following dependencies:
dependencies:
# ...
dio: ^4.0.0
json_annotation: ^4.7.0
retrofit: ^3.3.0
dev_dependencies:
# ...
swagger_parser: ^0.3.0
build_runner: ^2.3.0
json_serializable: ^6.5.0
retrofit_generator: ^4.2.0
Obs: versions above are just an initial reference and can be removed if conflicting with your versions.
Create swagger_parser.yaml #
This is the configuration file that will be read for running the parser according to your needs.
swagger_parser:
json_path: assets/swagger.json # Required. Sets the json path directory for api definition
output_directory: lib/api # Required. Sets output directory for generated files (api clients and models)
language: dart # Optional. Sets the programming language. Current available languages are: dart, kotlin. Default: dart
squish_clients: true # Optional. Set 'true' to put all clients in one folder. Default: false
client_postfix: ApiClient # Optional. Set postfix for client folder and Class. Works if there is only a single class or squish is true. Default: Client
freezed: false # Optional (dart only). Set 'true' to generate data classes using freezed package. Default: false
Compile your generator locally #
If you are using Flutter, you can replace all "dart run" calls with "flutter pub run".
To generate boilerplate code, run the generate
program inside directory where your pubspec.yaml
file is located:
dart run swagger_parser:generate
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
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>
Now run the builder for retrofit, json_serializable and freezed:
dart run build_runner build