【发布时间】:2015-02-08 23:57:28
【问题描述】:
我喜欢 RAML 在声明 resourceType 时如何动态引用不同的模式,例如:
resourceTypes:
- collection:
get:
responses:
200:
body:
application/json:
schema: <<schema>>
post:
body:
application/json:
schema: <<schema>>Create
responses:
200:
body:
application/json:
schema: <<schema>>
在这里我可以像这样使用它
/users:
type: { collection: { schema: user } }
RAML 会给我来自 GET 和 POST 的 user 模式响应,并且还使用 userCreate 模式发送 POST 请求。凉爽的!现在我可以用大量不同的模式重用我的集合定义。
但现在我也想为所有内容提供示例 json,我希望以另一种方式利用 <<schema>> var 来利用“代码重用”。我希望能够做到
resourceTypes:
- collection:
get:
responses:
200:
body:
application/json:
schema: <<schema>>
example: examples/v1-<<schema>>.json
post:
body:
application/json:
schema: <<schema>>Create
example: examples/v1-<<schema>>-create.json
responses:
200:
body:
application/json:
schema: <<schema>>
example: examples/v1-<<schema>>.json
但不幸的是,这不起作用。我收到一个错误提示
error: File with path "/examples/v1-%3C%3Cschema%3E%3E.json" does not exist
所以现在我不得不手动将其添加到我的所有收藏中,上面的 /users 示例已成为
/users:
type: { collection: { schema: user } }
get:
responses:
200:
body:
application/json:
example: !include examples/v1-user.json
post:
body:
application/json:
example: !include examples/v1-user-create.json
responses:
200:
body:
application/json:
example: !include examples/v1-user.json
对我来说,这只是添加示例的大量开销。特别是当我想在许多资源上重复该模式时。
问题:有没有办法做到这一点?
【问题讨论】:
标签: raml