コンテンツにスキップ

Swagger Codegen オンラインジェネレーター

swagger-generator モジュールは、独自の swagger-js ベースの Web UI を持ち、利用可能な Docker イメージ swaggerapi/swagger-generator-v3 を提供する Web サービスとして codegen を公開しています。

この Web サービスは https://generator3.swagger.io/ui にデプロイされているか、Docker コンテナとして簡単にデプロイできます。

ジェネレーターサービス API の OpenAPI 仕様は、Web サービスによって公開されている UI(例:https://generator3.swagger.io/ui)、公開されている YAML(https://generator3.swagger.io/openapi.json)、またはソースコードリポジトリ(https://github.com/swagger-api/swagger-codegen/blob/3.0.0/modules/swagger-generator/src/main/resources/openapi.yaml)で利用できます。

V2(v2 仕様用)と V3 ジェネレーター(v3 および生成中に変換された v2 仕様用)の両方が、プロパティ codegenVersion(例:"codegenVersion" : "v3")を提供することでサポートされていることに注意してください。

たとえば、Java API クライアントを生成するには、curl を使用して次の HTTP リクエストを送信するだけです。

ターミナルウィンドウ
1
curl -X POST \
2
https://generator3.swagger.io/api/generate \
3
-H 'content-type: application/json' \
4
-d '{
5
"specURL" : "https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore.yaml",
6
"lang" : "java",
7
"type" : "CLIENT",
8
"codegenVersion" : "V3"
9
}'

応答には、生成されたコードを含む圧縮ファイルが含まれます。

SDK をカスタマイズするには、次の HTTP ボディで言語固有のオプションを指定できます。

1
{
2
"specURL" : "https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore.yaml",
3
"lang" : "java",
4
"options" : {
5
"additionalProperties" : {
6
"useRuntimeException": true,
7
"useRxJava" : true
8
}
9
},
10
"type" : "CLIENT",
11
"codegenVersion" : "V3"
12
}

ここで、言語の options additionalProperties は、GET リクエストを https://generator3.swagger.io/api/options?language={language}&version={codegenVersion} に送信することで取得できます。

たとえば、curl https://generator3.swagger.io/api/options?language=java&version=V3 は(出力は省略されています)以下を返します。

1
{
2
"sortParamsByRequiredFlag": {
3
"opt": "sortParamsByRequiredFlag",
4
"description": "Sort method arguments to place required parameters before optional parameters.",
5
"type": "boolean",
6
"default": "true"
7
},
8
"ensureUniqueParams": {
9
"opt": "ensureUniqueParams",
10
"description": "Whether to ensure parameter names are unique in an operation (rename parameters that are not).",
11
"type": "boolean",
12
"default": "true"
13
},
14
"allowUnicodeIdentifiers": {
15
"opt": "allowUnicodeIdentifiers",
16
"description": "boolean, toggles whether unicode identifiers are allowed in names or not, default is false",
17
"type": "boolean",
18
"default": "false"
19
},
20
"modelPackage": {
21
"opt": "modelPackage",
22
"description": "package for generated models",
23
"type": "string"
24
},
25
...

OpenAPI/Swagger 仕様への URL を持つ specURL を使用する代わりに、JSON ペイロードに spec を含めることができます。例:

1
{
2
"options": {},
3
"spec": {
4
"swagger": "2.0",
5
"info": {
6
"version": "1.0.0",
7
"title": "Test API"
8
},
9
...
10
}
11
}