Добавляем зависимости в проект
dependencies:
flutter:
sdk: flutter
dio: ^5.8.0+1
json_annotation: ^4.8.0
retrofit: ^4.4.2
swagger_parser: ^1.22.1
dart_mappable: ^4.5.0
freezed_annotation: ^3.0.0
cupertino_icons: ^1.0.8
dev_dependencies:
flutter_test:
sdk: flutter
build_runner: ^2.3.3
json_serializable: ^6.6.1
dart_mappable_builder: ^4.5.0
freezed: ^3.0.3
retrofit_generator: ^9.1.9
Создаем файл swagger_parser.yaml
swagger_parser:
# You must provide the file path and/or url to the OpenApi schema.
# Sets the OpenApi schema path directory for api definition.
# schema_path: schemes/openapi.json
# Sets the url of the OpenApi schema.
schema_url: http://localhost:8000/api/schema
# Required. Sets output directory for generated files (Clients and DTOs).
output_directory: lib/api
# Optional. Set API name for folder and export file
# If not specified, the file name is used.
name: null
# Optional. Sets the programming language.
# Current available languages are: dart, kotlin.
language: dart
# Optional (dart only).
# Current available serializers are: json_serializable, freezed, dart_mappable.
json_serializer: json_serializable
# Optional. Set default content-type for all requests.
default_content_type: "application/json"
# Optional (dart only).
# Support @Extras annotation for interceptors.
# If the value is 'true', then the annotation will be added to all requests.
extras_parameter_by_default: false
# Optional (dart only).
# Support @DioOptions annotation for interceptors.
# If the value is 'true', then the annotation will be added to all requests.
dio_options_parameter_by_default: false
# Optional (dart only). Set 'true' to generate root client
# with interface and all clients instances.
root_client: true
# Optional (dart only). Set root client name.
root_client_name: RestClient
# Optional (dart only). Set 'true' to generate export file.
export_file: true
# Optional. Set to 'true' to put the all api in its folder.
put_in_folder: false
# Optional. Set 'true' to put all clients in clients folder.
put_clients_in_folder: false
# Optional. Set to 'true' to squash all clients in one client.
merge_clients: false
# Optional. Set postfix for Client class and file.
client_postfix: Client
# Optional. Set 'true' to use only the endpoint path for the method name.
# Set 'false' to use operationId
path_method_name: true
# Optional (dart only). Set 'true' to include toJson() in enums.
# If set to false, serialization will use .name instead.
enums_to_json: false
# Optional. Set 'true' to set enum prefix from parent component.
enums_parent_prefix: true
# Optional (dart only). Set 'true' to maintain backwards compatibility
# when adding new values on the backend.
unknown_enum_value: true
# Optional. Set 'false' to not put a comment at the beginning of the generated files.
mark_files_as_generated: true
# Optional (dart only). Set 'true' to wrap all request return types with HttpResponse.
original_http_response: false
# 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: ""
# Optional. Skip parameters with names.
skipped_parameters:
- 'X-Some-Token'
# Optional (dart & freezed only). Set 'true' to use Freezed 3.x code generation syntax.
# Set 'false' to maintain compatibility with Freezed 2.x.
use_freezed3: true
Создаем файл build.yaml
global_options:
freezed:
runs_before:
- json_serializable
json_serializable:
runs_before:
- retrofit_generator
Генерируем клиент:
dart run swagger_parser
Генерируем код
dart run build_runner build -d