Published on InterSystems Developer Community (https://community.intersystems.com)

ホーム > PersistentクラスやSerialクラスからSwaggerクラスを生成する

記事
Toshihiko Minamoto · 2021年4月22日 3m read

PersistentクラスやSerialクラスからSwaggerクラスを生成する

先日、永続クラスとシリアルクラスからSwagger仕様を生成する必要がありました。そこで、その時のコードを公開することにします(完全なコードではないため、アプリケーション固有の部分を変更する必要がありますが、まずは出発点として使用できます)。 こちらからご利用ください。

次のクラスがあるとしましょう。

クラス

Class REST.Test.Person Extends %Persistent
{

/// Personの名前。
Property Name As %String [ Required ];

/// Personの社会保障番号。 これは、パターンマッチを使って検証されます。
Property SSN As %String [ Required ];

/// Personの生年月日。
Property DOB As %Date;

/// Personの自宅住所。 これには埋め込みオブジェクトを使用します。
Property Home As Address;

/// Personの勤務先住所 これには埋め込みオブジェクトを使用します。
Property Office As Address;

/// Personの配偶者 これは、別の永続オブジェクトへの参照です。
Property Spouse As Person;

/// Personの好きな色を表す文字列のコレクション。
Property FavoriteColors As list Of %String;

/// Personの好きな色を表す文字列のコレクション。
Property FavoriteNumbers As array Of %Integer;

/// Personの年齢。<br>
/// これは計算フィールドで、値は<property>DOB</property>から取得されます。
Property Age As %Integer;

}

Class REST.Test.Address Extends %SerialObject
{

/// 住所。
Property Street As %String(MAXLEN = 80);

/// 都市名。
Property City As %String(MAXLEN = 80);

/// 2文字の省略形の州名。
Property State As %String(MAXLEN = 2);

/// 5桁の米国 ゾーン改善計画(ZIP)コード。
Property Zip As %String(MAXLEN = 5);
}

このSwagger定義を自動的に生成できます。

 REST.Test.Person:
   type: "object"
   properties:
     Age:
       type: "integer"
     DOB:
       type: "string"
     FavoriteColors:
       type: "array"
       items:
         type: "string"
     FavoriteNumbers:
       type: "object"
     Home:
       $ref: "#/definitions/REST.Test.Address"
     Name:
       type: "string"
     Office:
       $ref: "#/definitions/REST.Test.Address"
     SSN:
       type: "string"
     Spouse:
       $ref: "#/definitions/REST.Test.Person"
 REST.Test.Address:
   type: "object"
   properties:
     City:
       type: "string"
     State:
       type: "string"
     Street:
       type: "string"
     Zip:
       type: "string"

メインのメソッド: Utils.YAML:GenerateClasses

テスト実行: do ##class(Utils.YAML).Test()

#API #Code Snippet #REST API #InterSystems IRIS

ソースURL:https://jp.community.intersystems.com/post/persistent%E3%82%AF%E3%83%A9%E3%82%B9%E3%82%84serial%E3%82%AF%E3%83%A9%E3%82%B9%E3%81%8B%E3%82%89swagger%E3%82%AF%E3%83%A9%E3%82%B9%E3%82%92%E7%94%9F%E6%88%90%E3%81%99%E3%82%8B