swagger-codegenでAPIクライアントを生成するメモ


swagger-codegenとは

swagger-codegenとは、OpenAPIで書かれたAPI定義ファイルから、
APIクライアント(サーバスタブ)を生成するものです。

どんなコードが生成されるの? / 何の言語に対応してるの?

templateを見ましょう。

生成の仕方 (例: PHP)

1. インストール

https://github.com/swagger-api/swagger-codegen#compatibility
※ OpenAPIのバージョンに対応するバージョンをインストールしましょう。

2. 設定可能なconfigを確認し、jsonファイルで記述

確認

swagger-codegen config-help -lphp

記述

config.json
{
  "modelPackage" : "Model",
  "apiPackage" : "Api",
  "variableNamingConvention" : "camelCase"
  "artifactVersion" : "1.0.0",
  "hideGenerationTimestamp" : "true"
}

3. 元テンプレートをカスタマイズ

templateで、自分の使う言語のものをローカルにコピー, カスタマイズしましょう。

4. 生成しましょう

swagger-codegen generate \
  -l php \
  -i ./your-swagger-file.yml \
  -o ./ \
  -c ./config.json \
  -t ./templates

生成されたコードのライセンスについて

Generated code is intentionally not subject to the parent project license
When code is generated from this project, it shall be considered AS IS and owned by the user of the software.

とあるので、生成した本人のものになると理解しています。