GithubHelp home page GithubHelp logo

yerkejs / swagger-dart-code-generator Goto Github PK

View Code? Open in Web Editor NEW

This project forked from epam-cross-platform-lab/swagger-dart-code-generator

0.0 0.0 0.0 1.84 MB

Swagger/OpenAPI code generator based on Chopper and JsonAnnotation for Flutter

License: Apache License 2.0

Kotlin 0.04% Swift 0.14% Objective-C 0.01% Dart 99.81%

swagger-dart-code-generator's Introduction

Swagger dart code generator

Code partially generated with chopper

๐Ÿ“ฃ Build dart types from Swagger/OpenAPI schemas

pub package GitHub issues GitHub last commit build Discord 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.

Bloc

Installation

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

dependencies:
  chopper: ^4.0.1
  json_annotation: ^4.1.0

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

dev_dependencies:
  build_runner: ^2.1.4
  chopper_generator: ^4.0.2
  json_serializable: ^5.0.0
  swagger_dart_code_generator: any

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!
Option Default value Is required Description
with_base_url true false If this option is false, generator will ignore base url in swagger file.
use_required_attribute_for_headers true false If this option is false, generator will not add @required attribute to headers.
with_converter true false If option is true, combination of all mappings will be generated.
ignore_headers false false If option is true, headers will not be generated.
enums_case_sensitive true false If value is false, 'enumValue' will be defined like Enum.enumValue even it's json key equals 'ENUMVALUE'
include_paths [] false List of Regex If not empty - includes only paths matching reges
exclude_paths [] false List of Regex If not empty -exclude paths matching reges
use_default_null_for_lists false false If option is true, default value for lists will be null, otherwise - []
build_only_models false false If option is true, chopper classes will not be generated.
separate_models false false If option true, generates models into separate file.
include_if_null null false Sets includeIfNull JsonAnnotation feature and sets value for it. If null - not set anything.
default_values_map [] false Contains map of types and theirs default values. See DefaultValueMap.
response_override_value_map [] false Contains map of responses and theirs overridden values. See ResponseOverrideValueMap.
input_folder - true Path to folder with .swagger files (for ex. swagger_examples, or lib/swaggers).
output_folder - true Path 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/**

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: 'default'
            - type_name: 'List<String>'
              default_value: '[]'
          exclude_paths:
            - '\/cars\/get'
          include_paths:
            - '\/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:

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
              overridden_value: 'List<dynamic>'
              - url: '/news/latest'
              method: put
              overridden_value: 'MyPerfectType'

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

swagger-dart-code-generator's People

Contributors

fryette avatar liemvu avatar shcherbuk96 avatar vipw avatar vovanella95 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.