swagger_dart_code_generator 1.4.0

Flutter Android iOS

Have you been turned into a problem with writing code for Http requests? This package can help. It has all of the Http and model codegen functionality you have been looking for.

Swagger dart code generator

📣 Build dart types from Swagger/OpenAPI schemas #

pub package build Discord codecov Discord

SwaggerDartCodeGenerator is a code generator that looks for *.swagger files and builds .swagger.dart files, based on the schema. Codegenration based on Chopper and JsonAnnotation models and can be configured for your needs.


Overview #

In general case for each .swagger file three outputs will be created.
.dart generated by Swagger dart code generator, contains all models, requests, converters, etc.
[Since v1.2.0] .enums.dart generated by Swagger dart code generator, contains all enums and enums mappings.
.chopper.dart - generated by chopper.
.g.dart - generated by json_serializable.

Bloc

Installation #

Add the following to your pubspec.yaml file to be able to do code generation:

dev_dependencies:
  chopper_generator: ^3.0.5
  json_annotation: ^3.0.1
  json_serializable: ^3.4.1
  swagger_dart_code_generator: any

The generated code uses the following packages in run-time:

dependencies:
  chopper: ^3.0.3

Then run:

pub packages get

or

flutter packages get

Now SwaggerGenerator will generate the API files for you by running:

pub run build_runner build

or

flutter pub run build_runner build

Configuration #

Swagger generator offers some configuration options to generate code. All options should be included on build.yaml file on the root of the project:

targets:
  $default:
    builders:
      swagger_dart_code_generator:
        options:
          # custom configuration options!
OptionDefault valueIs requiredDescription
with_base_urltruefalseIf this option is false, generator will ignore base url in swagger file.
with_convertertruefalseIf option is true, combination of all mappings will be generated.
ignore_headersfalsefalseIf option is true, headers will not be generated.
use_path_for_request_namesfalsefalseIf property == false, then method name == operationId ?? path+methodType. If true - only path+methodType.
enums_case_sensitivetruefalseIf value is false, 'enumValue' will be defined like Enum.enumValue even it's json key equals 'ENUMVALUE'
use_default_null_for_listsfalsefalseIf option is true, default value for lists will be null, otherwice - []
build_only_modelsfalsefalseIf option is true, chopper classes will not be generated.
include_if_nullnullfalseEnables or disables includeIfNull JsonAnnotation feature and sets value for it. See IncludeIfNull.
default_values_map[]falseContains map of types and theirs default values. See DefaultValueMap.
response_override_value_map[]falseContains map of responses and theirs overriden values. See ResponseOverrideValueMap.
input_folder-truePath to folder with .swagger files (for ex. swagger_examples, or lib/swaggers).
output_folder-truePath to output folder (for ex. lib/generated).

It's important to remember that, by default, build will follow Dart's package layout conventions, meaning that only some folders will be considered to parse the input files. So, if you want to reference files from a folder other than lib/, make sure you've included it on sources:

targets:
  $default:
    sources:
      - lib/**
      - swagger_examples/**
      - swaggers/**

IncludeIfNull for model generation #

This option is used to add includeIfEmpty annotation for model fields. If option not enabled or empty - includeIfNull annotation will not added to fields. For mode details see oficial documentation Please see next example:

targets:
  $default:
    builders:
      swagger_dart_code_generator:
        options:
          input_folder: 'lib/swaggers'
          output_folder: 'lib/generated_code/'
          include_if_null:
            enabled: true
            value: false

Default Value Map for model generation #

If you want to add defaultValue: attribute to fields with concrete type you can use default value map. Please see next example:

targets:
  $default:
    builders:
      swagger_dart_code_generator:
        options:
          input_folder: 'lib/swaggers'
          output_folder: 'lib/generated_code/'
          default_values_map:
            - type_name: int
              default_value: '36'
            - type_name: String
              default_value: 'defaut'
            - type_name: 'List<String>'
              default_value: '[]'

Response Override Value Map for requests generation #

If you want to override response for concrete request, you can use response_override_value_map. For example:

targets:
  $default:
    builders:
      swagger_dart_code_generator:
        options:
          input_folder: 'lib/swaggers'
          output_folder: 'lib/generated_code/'
          response_override_value_map:
            - url: '/store/inventory'
              method: get
              overriden_value: 'List<dynamic>'
              - url: '/news/latest'
              method: put
              overriden_value: 'MyPerfectType'

Check the examples to see how to use it in details.

32
likes
90
pub points
52%
popularity

Publisher

mobilepeople.dev

Have you been turned into a problem with writing code for Http requests? This package can help. It has all of the Http and model codegen functionality you have been looking for.

Repository (GitHub)
View/report issues

Documentation

API reference

License

Apache 2.0 (LICENSE)

Dependencies

build, build_config, build_runner, dart_style, json_annotation, markdown, meta, path, recase, remove_from_coverage, universal_io

More

Packages that depend on swagger_dart_code_generator