LoginSignup
10
10

More than 5 years have passed since last update.

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

Posted at

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.

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

10
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
10