Skip to content

Generate pretty data classes, validators and API clients from OpenApi spec with ease.

License

Notifications You must be signed in to change notification settings

atten/dto-code-generator

Repository files navigation

Yet another Code Generator

pipeline coverage

Console tool for generating data classes, validators and API clients. Written in Kotlin, produces Kotlin and Python code for various purposes (see "Available generators" below). For the input it takes OpenApi specification (JSON) or custom codegen schema (also JSON).

The main goal of this project is to reduce integration costs between teams in multi-service environments.

Common use cases

  • Write integration code in agile, fast-moving projects
  • Use code generation in CI
  • Track API changes and check them for backward compatibility on both sides (client/server)
  • Build ETL pipelines with transparent interfaces
  • Unify type declarations across languages

Install

Docker image

docker image pull registry.gitlab.com/atten0/dto-code-generator:master

Other image versions:

https://gitlab.com/atten0/dto-code-generator/container_registry

Binary format (linux/x86)

Download and extract archive into current dir:

wget -qO- https://github.com/atten/dto-code-generator/releases/download/v2.2.0/dto-codegen-2.2.0.zip | busybox unzip -
cd dto-codegen-2.2.0/bin/
chmod +x dto-codegen

Other versions lists:

Usage

./dto-codegen [options] files/directories to parse

Options:

-t, --target
Target implementation
Possible Values: [KT_DATACLASS, KT_SERIALIZABLE_DATACLASS, KT_INTERFACE, PY_DJANGO_MODEL, PY_API_CLIENT, PY_API_ASYNC_CLIENT, PY_AMQP_BLOCKING_CLIENT, PY_AMQP_GEVENT_CLIENT, PY_MARSHMALLOW_DATACLASS, PY_DATACLASS]

-n, --name
Generated class name (inferred from input files if not specified)
Default: <empty string>

--include-path
Include only paths containing given strings
Default: []

--exclude-path
Do not include paths containing given strings
Default: []

-p, --prefixed
If enabled, add prefix to all fields
Default: false

--help
Show help usage and exit

--version, -v
Display version and exit

Example

Script for python API client generation from OpenApi

Downloads remote OpenApi spec (gitlab assets), runs docker container with generator and saves output to file.

#!/usr/bin/env sh

OPENAPI_URL="https://gitlab.com/atten0/dto-code-generator/-/raw/master/src/test/resources/input/openApi.json"
DOCKER_IMAGE="registry.gitlab.com/atten0/dto-code-generator:master"

docker pull $DOCKER_IMAGE

echo "Fetch openapi schema..."
wget --quiet -O /tmp/openapi.json $OPENAPI_URL

echo "Generate python API client..."
docker run --rm \
-v /tmp/openapi.json:/openapi.json:ro \
$DOCKER_IMAGE \
/openapi.json \
-t PY_API_CLIENT \
> ~/client_generated.py

Available generators

Target Language/Framework Serialization Dependencies Example output Coverage
KT_DATACLASS Kotlin kotlinx.serialization entitiesOutput.kt coverage
KT_SERIALIZABLE_DATACLASS Kotlin Jackson entitiesOutputJacksonEnabled.kt coverage
PY_API_CLIENT Python3 Marshmallow requirements.txt endpointsOutput.py coverage
PY_API_ASYNC_CLIENT Python3 (async) Marshmallow requirements.txt endpointsOutput.py coverage
PY_DATACLASS Python3 Dataclass - entitiesOutput.py coverage
PY_MARSHMALLOW_DATACLASS Python3 Marshmallow requirements.txt entitiesOutput.py coverage
PY_DJANGO_MODEL Python3 + Django - requirements.txt entitiesOutput.py coverage
PY_AMQP_BLOCKING_CLIENT Python3 Marshmallow endpointsOutput.py coverage
PY_AMQP_GEVENT_CLIENT Python3 + Gevent Marshmallow endpointsOutput.py coverage

Support and feedback

For usage questions, feature proposals and bug reports: github issues page.

For other matters: author's profile with contacts.

About

Generate pretty data classes, validators and API clients from OpenApi spec with ease.

Resources

License

Stars

Watchers

Forks

Languages