rksoftware

Visual Studio とか C# とかが好きです

AutoRest で OpenAPI 3 からクライアントを生成

OpenAPI / Swagger なんもわかりません。

以前に AutoRest を使てみた時とても苦労したのです (おそらく私がなんもわかっていないから) が、今やったら申し越し簡単にできたので情報アップデートです。

以前に試した記事

今回参考にした情報


--v3 オプションをどこで見たかは忘れてしまいました。

■ autorest-beta のインストール

npm でインストールしました。

npm install -g @autorest/autorest

かんたんですね。

■ autorest--beta で C# コードを生成

autorest-beta --v3 --input-file=swagger.json --csharp --output-folder=CSharp_Swagger --namespace=swagger

autorest-beta コマンドと --v3 オプションがポイントです。

かんたんですね。

■ やはりエラーは出る

以前と同じ json ファイルで試してみました。
以前に試した時にもエラーで作れませんでしたが、今回もエラーは出るには出ます。
しかし今回出たエラーは前回 5 つ出たうちの最後の一つだけです。

FATAL: OperationId is required for all operations. Please add it for 'get' operation of '/WeatherForecast' path. 
FATAL: AutoRest.Core.Logging.CodeGenerationException: OperationId is required for all operations. Please add it for 'get' operation of '/WeatherForecast' path. 

これは対処もかんたんですね。

■ json ファイルを書き換える

エラーは

  • すべてのオペレーションに OperationId が必要
  • '/WeatherForecast' パスの 'get' オペレーションにそれ (OperationId) を足してください

ということなので軽く json ファイルを編集しました。編集箇所は

{}
  "paths": {
    "/WeatherForecast": {
      "get":

get にプロパティ "operationId": "getWeatherForecast" を追加しました。
こんな感じ

    "paths": {
      "/WeatherForecast": {
        "get": {
          "operationId": "getWeatherForecast",

かんたんですね。

■ json 変更前後

変更前

{
  "openapi": "3.0.1",
  "info": {
    "title": "My API",
    "version": "v1"
  },
  "paths": {
    "/WeatherForecast": {
      "get": {
        "tags": [
          "WeatherForecast"
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "text/plain": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/WeatherForecast"
                  },
                  "nullable": true
                }
              },
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/WeatherForecast"
                  },
                  "nullable": true
                }
              },
              "text/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/WeatherForecast"
                  },
                  "nullable": true
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "WeatherForecast": {
        "type": "object",
        "properties": {
          "date": {
            "type": "string",
            "format": "date-time"
          },
          "temperatureC": {
            "type": "integer",
            "format": "int32"
          },
          "temperatureF": {
            "type": "integer",
            "format": "int32",
            "readOnly": true
          },
          "summary": {
            "type": "string",
            "nullable": true
          }
        },
        "nullable": true
      }
    }
  }
}

変更後

{
  "openapi": "3.0.1",
  "info": {
    "title": "My API",
    "version": "v1"
  },
  "paths": {
    "/WeatherForecast": {
      "get": {
        "tags": [
          "WeatherForecast"
        ],
        "operationId": "getWeatherForecast",
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "text/plain": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/WeatherForecast"
                  },
                  "nullable": true
                }
              },
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/WeatherForecast"
                  },
                  "nullable": true
                }
              },
              "text/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/WeatherForecast"
                  },
                  "nullable": true
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "WeatherForecast": {
        "type": "object",
        "properties": {
          "date": {
            "type": "string",
            "format": "date-time"
          },
          "temperatureC": {
            "type": "integer",
            "format": "int32"
          },
          "temperatureF": {
            "type": "integer",
            "format": "int32",
            "readOnly": true
          },
          "summary": {
            "type": "string",
            "nullable": true
          }
        },
        "nullable": true
      }
    }
  }
}

かんたんですね。

かんたんですね

以前よりより簡単にクライアントが生成できるようになりました。
色々なサービスの API 研究がより捗ります。