Swagger Parser
repository·main·Indexed 41 days ago
https://github.com/carapacik/swagger_parserA code generation tool that transforms OpenAPI (Swagger) definitions (v2, v3.0, v3.1) in JSON or YAML formats into type-safe REST clients and data models for Dart and Kotlin. It generates clients based on the retrofit pattern and supports Dart serializers including json_serializable, freezed, and dart_mappable, as well as moshi for Kotlin. Features include a web-based configuration interface and support for Server-Sent-Events (SSE) streams in Dart.
What's inside Swagger Parser
- Swagger Parser is a Dart package designed to automate the creation of REST clients and data classes from OpenAPI definitions. It processes OpenAPI files and generates code that integrates with popular networking and serialization libraries, reducing the manual effort required to implement API clients.
Overview of Swagger Parser
mainSwagger Parser is a Dart package designed to automate the creation of REST clients and data classes from OpenAPI definition files or URLs. It supports OpenAPI versions v2, v3.0, and v3.1 in both JSON and YAML formats.
Key capabilities include:
- Generating REST client files based on the Retrofit pattern.
- Generating data classes using popular serializers: json_serializable, freezed, or dart_mappable.
- Supporting multiple target languages (Dart, Kotlin).
- Supporting Server-Sent-Events (SSE) in Dart.
- Providing a web interface at https://carapacik.github.io/swagger_parser for easier interaction.
Supported OpenAPI versions and formats
mainSwagger Parser supports the following specifications and file formats:
OpenAPI Versions:
- OpenAPI v2
- OpenAPI v3.0
- OpenAPI v3.1
Input Formats:
JSONYAML
Input Methods:
- Generation via a definition link (URL)
- Generation from a local file
- Support for multiple definitions in a single run
Supported languages and serializers
mainThe tool generates code for both Dart and Kotlin. Depending on your target language, you can choose from several supported serialization libraries:
Dart Serializers:
json_serializablefreezeddart_mappable
Kotlin Serializers:
moshi
All generated clients are based on the retrofit pattern.
Quickstart: Generate REST clients and data classes
mainTo use
swagger_parserto generate code from an OpenAPI/Swagger definition, follow these steps:- Add your schema: Place your Swagger/OpenAPI schema file in your project.
- Configure
pubspec.yaml:- Add the path to your schema file as an asset.
- Set the desired output directory for the generated code.
- Generate the parser output: Run the
swagger_parsercommand to generate the initial REST clients and data classes. - Run build_runner: If you are using
retrofitandfreezed, runbuild_runnerto complete the code generation for those specific libraries.
dart run swagger_parser dart run build_runner buildBuild the WebAssembly (WASM) release for the web interface
mainTo create a WebAssembly release compatible with GitHub Pages, use the
flutter build webcommand with the--wasmand--releaseflags. Ensure you set the--base-hrefto/swagger_parser/to match the GitHub Pages hosting path.flutter build web --wasm --release --base-href /swagger_parser/Run the swagger_parser web interface locally
mainTo develop or run the web interface locally using Flutter, use the following commands to fetch dependencies, analyze the code, run tests, and launch the application in Chrome.
flutter pub get flutter analyze flutter test flutter run -d chromeGenerate Server-Sent-Events (SSE) streams
mainYou can generate methods that return
Streamtypes for SSE support. This is determined by theformatandtypein your OpenAPI specification.- To generate
Stream<Uint8List>(raw bytes): Setformat: binaryandtype: integer. - To generate
Stream<String>(text): Setformat: binaryandtype: string.
Example OpenAPI snippet for a byte stream:
paths: /pets: get: operationId: getBinaryStream responses: '200': description: Returns a stream of bytes. content: application/octet-stream: schema: type: integer format: binaryResults in:
@GET('/pets') @DioResponseType(ResponseType.stream) Stream<Uint8List> getBinaryStream();- To generate
Configure build order for `freezed` and `retrofit`
mainWhen using
freezedalongsideretrofit, you must configure abuild.yamlfile to ensure the generators run in the correct order. The required execution sequence is:freezed$\rightarrow$json_serializable$\rightarrow$retrofit_generator.global_options: freezed: runs_before: - json_serializable json_serializable: runs_before: - retrofit_generatorUse the swagger_parser web interface
mainYou can use the web-based interface to generate Dart and Kotlin clients directly in your browser. The configuration form in the web interface is designed to mirror every option available in the
SWPConfiggeneration configuration. This allows for a visual way to configure your client generation without writing configuration files manually.https://carapacik.github.io/swagger_parserInstall swagger_parser and its dependencies
mainTo use
swagger_parser, add it to yourdev_dependencies. Depending on your preferred JSON serializer (freezed, dart_mappable, or json_serializable), you must also include the corresponding runtime and generator dependencies in yourpubspec.yaml.dependencies: # Choose one of the following based on your preference: # dio: ^5.9.2 # freezed_annotation: ^3.1.0 # json_annotation: ^4.12.0 # retrofit: ^4.9.2 # dart_mappable: ^4.8.0 dev_dependencies: swagger_parser: # For freezed: # freezed: ^3.2.5 # json_serializable: ^6.14.0 # retrofit_generator: ^10.2.7 # build_runner: ^2.15.0 # For dart_mappable: # dart_mappable_builder: ^4.9.0 # For json_serializable: # json_serializable: ^6.14.0 # retrofit_generator: ^10.2.7 # build_runner: ^2.15.0Generate files using build_runner (Freezed/Retrofit)
mainIf you are using
freezedwithretrofit, you must configure yourbuild.yamlto ensure the correct generation order. Thefreezedgenerator must run beforejson_serializable, andjson_serializablemust run beforeretrofit_generator.global_options: freezed: runs_before: - json_serializable json_serializable: runs_before: - retrofit_generatorThen run:
dart run build_runner build -d