2016-09-20 35 views
0

我有一個API,其中一個鍵的基本響應將有一個標識符數組。用戶可以傳遞一個額外的參數,這樣數組將轉向一個字符串數組中的對象數組(對於實際的細節而不必單獨調用)。如何指示哪個「oneOf」API響應將使用?

"children": { 
    "type": "array", 
    "items": { 
    "oneOf": [{ 
     "type": "string", 
     "description": "Identifier of child" 
    }, { 
     "type": "object", 
     "description": "Contains details about the child" 
    }] 
    } 
}, 

有沒有辦法表明第一種類型是通過默認方式來實現的,第二種方式是通過請求的參數來實現的?

+0

使用新屬性擴展oneOf模型並將其設置在服務器端? –

+0

我不明白?我引用http://json-schema.org/ –

回答

0

我不完全清楚你想要完成的區別。真的,這聽起來像文件;也許在每個oneOf子模板的description中詳細說明。

您可以在頂層(children的同級)添加一個額外的布爾型字段,以指示是否返回詳細響應併爲該字段提供默認值。下一步是將布爾值與數組項的類型耦合,我使用oneOf來完成。

我建議沿着線的東西:

{                    
    "children": {                 
    "type": "array",                
    "items": {                 
     "oneOf": [                 
     {                  
      "type": "string",              
      "description": "Identifier of child", 
      "pattern": "^([A-Z0-9]-?){4}$"         
     },                  
     {                  
      "type": "object",              
      "description": "Contains details about the child", 
      "properties": { 
      "age": { 
       "type": "number" 
      } 
      }      
     }                  
     ]                   
    }                   
    },                    
    "detailed": {                 
    "type": "boolean",               
    "description": "If true, children array contains extra details.",   
    "default": false                
    },                    
    "oneOf": [                  
    {                   
     "detailed": {                
     "enum": [                
      true                 
     ]                  
     },                   
     "children": {                
     "type": "array",               
     "items": {                
      "type": "object"              
     }                  
     }                   
    },                   
    {                   
     "detailed": {                
     "enum": [                
      false                 
     ]                  
     },                   
     "children": {                
     "type": "array",               
     "items": {                
      "type": "string"              
     }                  
     }                   
    }                   
    ]                    
} 

第二oneOf地方響應對象上的進一步的要求,當"detailed": true的「孩子們」的項目的數組必須是「對象」類型。這細化了描述「children」數組中對象模式的第一個oneOf限制。

+0

我看你在暗示'detailed'被顯示爲響應(除了僅僅是查詢參數外),但不一定實際返回並且僅用於doc這裏的目的。在詳細介紹之後,我會因爲需要額外的'oneOf'而迷失方向。看來你打算第一個'oneOf'依賴'$ ref',然後使用'definitions'而不是'oneOf' –

+0

你不會在'oneOf's之間共享任何東西;他們故意重疊,但他們說的是不同的東西。我更新了我的答案,試圖澄清。 – wachr