2016-06-22 36 views
2

我有文档,并尝试将新的API文档添加到该文档。如何在raml资源中包含常见的响应

我经历了基本的RAML文档。

我有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<<:都不行。

+0

性状适合我! – Sachin

+0

@Sachin你可以给你的“特质”例子吗? – Nilesh

回答

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"} 

你可以用同样的方法d efine特征为您的答复。

+0

我从来没有尝试'特征',但会试着选择这一个,如果它的工作。 – Nilesh

+0

谢谢!很高兴我可以帮助即使经过近一年:) – flogy