Skip to content

Repository files navigation

OpenAPI SDK generator - API client generator

API client generator is a console application capable of auto-generating a PSR18/PSR7 compliant API client based on OpenAPI v3 specification according to PHP best practices and your code style standards.

Build StatusCoverage StatusPHPStan Level

Requirements

Why using it?

  • With generated client you are always sure that your OpenAPI specification is up-to-date.
  • Work with objects instead of raw data, it is OOP friendly.
  • Saves your time. You don't need to write data mappers youself to populate those objects with the data from the response.
  • All the basic type validations in the request and the response done automatically.
  • Despite the fact the code is generated it's clear and readable, simple to debug and to reason about.
  • Highly configurable and extensible.
  • Reliable and well tested.
  • Simply, if something can be automated it should be automated. Focus on important stuff.

Features

  • Supports yaml or json specification file formats.
  • Validates your OpenAPI specification.
  • Supports multiple content types:
    • application/json
    • application/x-www-form-urlencoded
    • application/xml
  • Supports new PHP versions synthax features.
  • It is base client independent, you are free to choose any existing PSR-18 compliant client. Just choose the one which you already use, so generated client would not cause conflicts in your dependencies.
  • Applies code style rules to generated code, you can specify your own.
  • Generates README and composer.json files with possibility to use your own template.
  • Supports allOf OpenAPI parameter.

Example

Check out example directory to see the code generated by api-client-generator.

Try it out:

cd example && \
composer install && \
php test-example.php

Usage

With Docker

$ docker run -it \
-v {path-to-specification}/openapi.yaml:/openapi.yaml:ro \
-v {path-to-client}/some-api-client:/client \
-e NAMESPACE=Group\\SomeApiClient \
-e OPENAPI=/openapi.yaml \
-e OUTPUT_DIR=/client \
-e PACKAGE=group/some-api-client \
dhlabs/api-client-generator

Without Docker

Preconditions: PHP 7.4

Clone the repository and run:

OPENAPI={path-to-specification}/openapi.yaml NAMESPACE=Group\SomeApiClient PACKAGE=group/some-api-client OUTPUT_DIR={path-to-client}/generated ./bin/api-client-generator generate

Configuration

The following environment variables are available:

VariableRequiredDefaultEnumExample
NAMESPACEyesGroup\SomeApiClient
PACKAGEyesgroup/some-api-client
OPENAPI yes/api/openapi.yaml
OUTPUT_DIRyes/client
CODE_STYLEno{path-to-repository}/.php_cs.php/client/myCodeStyle.php
SOURCE_DIRnosrcsrc
CLIENT_PHP_VERSIONno7.47.0, 7.1, 7.2, 7.3, 7.47.4
COMPOSER_JSON_TEMPLATE_DIRno{path-to-repository}/template/composer.json.twig/path/composer.json.twig
README_MD_TEMPLATE_DIRno{path-to-repository}/template/README.md.twig/path/README.md.twig
HTTP_MESSAGEnoguzzleguzzle, nyholmnyholm
CONTAINERnopimplepimplepimple

Running tests

$ composer install
$ make test

(check make for all available routines).

About

API client generator is a console application capable of generating an API client based on OpenAPI(Swagger) specification.

Resources

Code of conduct

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages