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

主页 > InterSystems 最佳实践系列之--从持久类和序列类生成 Swagger 规范

文章
jieliang liu · 一月 7, 2021 阅读大约需 2 分钟

InterSystems 最佳实践系列之--从持久类和序列类生成 Swagger 规范

最近,我需要从持久类和序列类生成一个 Swagger 规范,所以我发布了我的代码(它并不完整 - 你仍然需要处理应用程序的细节,但这是一个开始)。 代码在这里。

假设你有下面的类:

类

Class REST.Test.Person Extends %Persistent
{

/// 人的名字。
Property Name As %String [ Required ];

/// 人的社会安全号。 这通过模式匹配进行验证。
Property SSN As %String [ Required ];

/// 人的出生日期。
Property DOB As %Date;

/// 人的家庭地址。 这里使用一个嵌入对象。
Property Home As Address;

/// 人的办公室地址。 这里使用一个嵌入对象。
Property Office As Address;

/// 人的配偶。 这是对另一个持久对象的引用。
Property Spouse As Person;

/// 代表人喜欢的颜色的字符串集合。
Property FavoriteColors As list Of %String;

/// 代表人喜欢的颜色的字符串集合。
Property FavoriteNumbers As array Of %Integer;

/// 人的年龄。<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://cn.community.intersystems.com/post/intersystems-%E6%9C%80%E4%BD%B3%E5%AE%9E%E8%B7%B5%E7%B3%BB%E5%88%97%E4%B9%8B-%E4%BB%8E%E6%8C%81%E4%B9%85%E7%B1%BB%E5%92%8C%E5%BA%8F%E5%88%97%E7%B1%BB%E7%94%9F%E6%88%90-swagger-%E8%A7%84%E8%8C%83