
OpenApiのYAMLを読み込んで、シミュレーターに自動設定させてみよう ①
今回の目的
せっかく完成したconB2ApiSimulatorですが、いざ設定となると、我ながら面倒くさい。
なので、OpenApiという、Apiの設定書(?)が世界標準みたいなので、それを自動で読み込もうかなぁと。
備忘録がてら、つらつらと手順を記述して行きます。
YAMLファイルの読み込みと展開
一番最初は、動的にYAMLの設定情報をフリーな感じで読み取って貼り付ければ良いや、な感じで進めたのですが、Nugetの読み込みユーティリティーが、柔軟すぎて全部のKPVをオブジェクトで扱うの orz
取得のたびに、キーをstringへ変換して、その配下を呼び出して、またキーを変換して・・・泥沼になりそうなので、クラス化をしました。
とりあえず、簡素化したYAMLファイルと、その取得POCOクラスを作成しました。
openapi: 3.0.0
info:
title: Simple Petstore API
description: A minimal version of the Swagger Petstore API
version: 1.0.0
servers:
- url: https://petstore.example.com/api
paths:
/pet:
post:
summary: Add a new pet
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
responses:
'200':
description: Pet added successfully
get:
summary: List all pets
responses:
'200':
description: A list of pets
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Pet'
/pet/{petId}:
get:
summary: Get pet by ID
parameters:
- name: petId
in: path
required: true
schema:
type: integer
responses:
'200':
description: Pet details
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
components:
schemas:
Pet:
type: object
required:
- id
- name
properties:
id:
type: integer
name:
type: string
status:
type: string
enum: [available, pending, sold]
using System.Collections.Generic;
namespace conB2WebApiSimulator.Models
{
public class OpenApiDocument
{
public string Openapi { get; set; } = "";
public Info Info { get; set; } = new();
public List<Server> Servers { get; set; } = new(); // 通常1件だがリスト構造
public Dictionary<string, PathItem> Paths { get; set; } = new();
public Components Components { get; set; } = new();
}
public class Info
{
public string Title { get; set; } = "";
public string Description { get; set; } = "";
public string Version { get; set; } = "";
}
public class Server
{
public string Url { get; set; } = "";
}
public class PathItem
{
public Dictionary<string, Operation> Operations { get; set; } = new();
}
public class Operation
{
public string Summary { get; set; } = "";
public List<Parameter>? Parameters { get; set; }
public RequestBody? RequestBody { get; set; }
public Dictionary<string, Response>? Responses { get; set; }
}
public class Parameter
{
public string Name { get; set; } = "";
public string In { get; set; } = ""; // "query", "path", etc.
public bool Required { get; set; }
public Schema Schema { get; set; } = new();
}
public class RequestBody
{
public Dictionary<string, MediaType> Content { get; set; } = new();
}
public class Response
{
public string Description { get; set; } = "";
public Dictionary<string, MediaType>? Content { get; set; }
}
public class MediaType
{
public Schema Schema { get; set; } = new();
}
public class Schema
{
public string? Type { get; set; }
public string? Ref { get; set; } // $ref
public Schema? Items { get; set; } // for arrays
public List<string>? Enum { get; set; }
public Dictionary<string, Schema>? Properties { get; set; }
public List<string>? Required { get; set; }
}
public class Components
{
public Dictionary<string, Schema> Schemas { get; set; } = new();
}
private OpenApiDocument ParseYaml(string yamlText)
{
var deserializer = new DeserializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.IgnoreUnmatchedProperties()
.Build();
var openApi = deserializer.Deserialize<OpenApiDocument>(yamlText);
return openApi;
}
}
とりあえず、Chat先生に頼んだら、出来ました。
YAMLのテキストを放り込むと、クラスになります。
まぁ、うちの定義と、OpenApiの定義が違うので、このままコンバート出来るかは、この先の作業です。
実データを都合よくコンバートする
URLはサクッと取れたので、プロトコルとドメイン名は取得できました。
APIのエントリーも取得できるなぁ、と思ったら、メソッドで処理を分けてるの。うちのは、そこは未対応なんだよね。とりあえず全部受け取って、DELETE以外、同一処理でレスポンス出しちゃうの。
シミュレーター側の仕様変更が発生したので、いったん打ち切りです。
残念。