【问题标题】:RAML Trait wrapping a response object (for defining REST API)包装响应对象的 RAML Trait(用于定义 REST API)
【发布时间】:2019-04-10 21:44:32
【问题描述】:

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

    您可以使用类型为“”的变量:

    responseEnvelope:
            usage: This trait should be used to wrap any response object
            body:
              application/json:
                type: object
                properties:
                  metadata:
                  responseObjectToWrap:
                    type: <<typeName>>
                  message:
    

    然后在你定义资源的 trait 时传递该类型:

    /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立即删除,因为它是重复的

    【讨论】:

      猜你喜欢
      • 2020-11-14
      • 1970-01-01
      • 1970-01-01
      • 2018-11-08
      • 1970-01-01
      • 2015-01-03
      • 1970-01-01
      • 1970-01-01
      • 2016-10-24
      相关资源
      最近更新 更多