RAML Trait包装响应对象(用于定义REST API)

问题描述 投票:0回答:1

使用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

rest api api-design raml
1个回答
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即时删除作为重复

© www.soinside.com 2019 - 2024. All rights reserved.