2016-06-22 35 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