如何包含对raml资源的常见响应

时间:2016-06-22 16:46:04

标签: api inheritance raml api-doc

我有个文档,并尝试将新API的文档添加到该文档中。

我浏览了基本的RAML文档。

我有raml文件为。

#Filename: base.raml
title: Test RAML
documentation:
  - title: Test RAML docs first time :)
    content: This is RAML testing
baseUri: https://myportal.com/{version}/scriptmanagement
version: v1.0
mediaType: application/json
protocols: [ HTTPS ]

/test:
    !include raml/test.raml

实际的raml内容位于test.raml

#Filename: test.raml
displayName: Test RAML Inheritance
description: Testing for RAML inheritance for responses.

get:
    description: Get all TEST
    headers:
        name:
            description: name required in each request
            example: testname
            required: true
    responses:
        200:
            description: SUCCESS
            body:
                application/json:
                    example: |
                        {}
        400:
            description: BAD REQUEST
            body:
                application/json:
                    example: |
                        {"error": "Bad Request"}
        500:
            description: INTERNAL ERROR
            body:
                application/json:
                    example: |
                        {"error": "Internal Error"}

post:
    description: Get all TEST
    headers:
        name:
            description: name required in each request
            example: testname
            required: true
    responses:
        200:
            description: SUCCESS
            body:
                application/json:
                    example: |
                        {"message": "Created"}
        400:
            description: BAD REQUEST
            body:
                application/json:
                    example: |
                       {"error": "Bad Request"}
        500:
            description: INTERNAL ERROR
            body:
                application/json:
                    example: |
                        {"error": "Internal Error"}


/{test_id}:
    description: TEST DETAILS
    get:
        description: Retrieve resource own by x-user-name
        headers:
            name:
                description: name required in each request
                example: testname
                required: true
        responses:
            200:
                description: SUCCESS
                body:
                    application/json:
                        example: |
                            {"message": "Details"}
            400:
                description: BAD REQUEST
                body:
                    application/json:
                        example: |
                            {"error": "Bad Request"}
            500:
                description: INTERNAL ERROR
                body:
                    application/json:
                        example: |
                            {"error": "Internal Error"}

在上面的RAML中,400500响应很常见,name标头很常见。

如何写一次并添加到所有资源?我尝试traits<<:都不起作用。

1 个答案:

答案 0 :(得分:1)

特征是正确的解决方案。这是您的name标题方案的示例:

定义特征

traits:
  nameHeader:
    headers:
      name:
        description: name required in each request
        example: testname
        required: true

使用特征

要使用此特征,您必须在请求规范中明确提及:

/{test_id}:
  description: TEST DETAILS
  get:
    description: Retrieve resource own by x-user-name
    is: [nameHeader]
    responses:
      200:
        description: SUCCESS
        body:
          application/json:
            example: |
              {"message": "Details"}

您可以为答案定义特征。