swagger_dart_code_generator 2.7.3 icon indicating copy to clipboard operation
swagger_dart_code_generator: ^2.7.3 copied to clipboard

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 GitHub issues GitHub last commit build codecov Discord

SwaggerDartCodeGenerator is a code generator that looks for *.swagger files and builds .swagger.dart files, based on the schema. Code generation 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.


Installation #

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

  chopper: ^4.0.1
  json_annotation: ^4.4.0

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

  build_runner: ^2.1.7
  chopper_generator: ^4.0.5
  json_serializable: ^6.1.4
  swagger_dart_code_generator: ^2.4.6

Then run:

pub packages get


flutter packages get

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

pub run build_runner build


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:

          # custom configuration options!
OptionDefault valueIs requiredDescription
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).
input_urls[]falseHere you can mention list of files to be downloaded from the internet. You can check example of usage.
with_base_urltruefalseIf this option is false, generator will ignore base url in swagger file.
use_required_attribute_for_headerstruefalseIf this option is false, generator will not add @required attribute to headers.
with_convertertruefalseIf option is true, combination of all mappings will be generated.
ignore_headersfalsefalseIf option is true, headers will not be generated.
additional_headersfalsefalseList of additional headers, not specified in Swagger. Example of usage: build.yaml
enums_case_sensitivetruefalseIf value is false, 'enumValue' will be defined like Enum.enumValue even it's json key equals 'ENUMVALUE'
classes_with_nullabe_lists[]falseList of regex strings. If class name matches any of regex - list properties will have default value null. Otherwise it will be empty list. If you used use_default_null_for_lists: true, just set .* value for this property and result will be same. Check example for more details
build_only_modelsfalsefalseIf option is true, chopper classes will not be generated.
separate_modelsfalsefalseIf option true, generates models into separate file.
include_if_nullnullfalseSets includeIfNull JsonAnnotation feature and sets value for it. If null - not set anything.
default_values_map[]falseContains map of types and theirs default values. See DefaultValueMap.
response_override_value_map[]falseContains map of responses and theirs overridden values. See ResponseOverrideValueMap.
cut_from_model_names-falseIf your model names are long and contain a lot of duplicated words, for example DbUsersModelsV3GeneralUserModel, you can cut off duplicated part, using cut_from_model_names : DbUsersModelsV3. Also, you can use regex expressions in this parameter.
nullable_models-falseList of model names should have force-nullable properties. Example of usage in build.yaml

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:

      - lib/**
      - swagger_examples/**
      - swaggers/**

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:

          input_folder: 'lib/swaggers'
          output_folder: 'lib/generated_code/'
            - type_name: int
              default_value: '36'
            - type_name: String
              default_value: 'default'
            - type_name: 'List<String>'
              default_value: '[]'
            - '\/cars\/get'
            - '\/popular\/cars'

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:

          input_folder: 'lib/swaggers'
          output_folder: 'lib/generated_code/'
            - url: '/store/inventory'
              method: get
              overridden_value: 'List<dynamic>'
              - url: '/news/latest'
              method: put
              overridden_value: 'MyPerfectType'

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

pub points


verified publisher iconmobilepeople.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


API reference


Icon for licenses.Apache-2.0 (LICENSE)


build, code_builder, collection, dart_style, http, json_annotation, markdown, path, recase, universal_io, yaml


Packages that depend on swagger_dart_code_generator