3

我有文档,并尝试将新的 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 中,400响应500很常见,name标头很常见。

我怎样才能写一次并添加到所有资源中?我试过了traits<<:但都不起作用。

4

1 回答 1

4

特质是这里的正确解决方案。这是您的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"}

与您可以为响应定义特征的方式相同。

于 2017-06-09T15:18:36.933 回答