使用RAML,我试图创建一个信封,多个API可以用来包装响应。也就是说,通用响应包络了。
我想出了这个
title: Response Envelop
version: 1.0
uses:
another: ./another.raml
traits:
responseEnvelope:
usage: This trait should be used to wrap any response object
body:
application/json:
type: object
properties:
metadata:
type: another.AType
responseObjectToWrap:
type: any
message:
type: another.BType
我在定义上面的responseObjectToWrap
部分时遇到困难。这就是响应信封将包裹的类型。 type: any
是否有效?
此外,如何在API中使用此特征?例如,如果我想使用此信封包装类型NewType
。
答案 0 :(得分:1)
您可以为类型'<>'使用变量:
responseEnvelope:
usage: This trait should be used to wrap any response object
body:
application/json:
type: object
properties:
metadata:
responseObjectToWrap:
type: <<typeName>>
message:
然后在资源上定义特征时传递该类型:
/myResource:
post:
is: { responseEnvelope: { typeName : MyCustomType } }
完整示例:
#%RAML 1.0
title: Response Envelop
version: 1.0
traits:
responseEnvelope:
usage: This trait should be used to wrap any response object
body:
application/json:
type: object
properties:
metadata:
responseObjectToWrap:
type: <<typeName>>
message:
types:
NewType:
properties:
bla:
/myResource:
post:
is: { responseEnvelope: { typeName : NewType } }
已更新。最初是不小心将其发布到错误的问题上,并由于重复而被mods立即删除