在我的“簡化” API中,所有響應均從基本“響應”類派生(繼承)。響應類由填充有元數據的頭和包含用戶所請求的核心數據的主體組成。布置響應(以JSON格式),使得所有元數據都位于第一個“層”上,并且body是這樣一個稱為“ body”的單個屬性response|--metadata attribute 1 (string/int/object)|--metadata attribute 2 (string/int/object)|--body (object) |--body attribute 1 (string/int/object) |--body attribute 2 (string/int/object)我嘗試使用以下JSON來定義這種關系:{ ... "definitions": { "response": { "allOf": [ { "$ref": "#/definitions/response_header" }, { "properties": { "body": { "description": "The body of the response (not metadata)", "schema": { "$ref": "#/definitions/response_body" } } } } ] }, "response_header": { "type": "object", "required": [ "result" ], "properties": { "result": { "type": "string", "description": "value of 'success', for a successful response, or 'error' if there is an error", "enum": [ "error", "success" ] }, "message": { "type": "string", "description": "A suitable error message if something went wrong." } } }, "response_body": { "type": "object" } }}然后,我嘗試通過創建從body / header繼承的各種body / header類來創建不同的響應,然后創建由相關的header / body類組成的子響應類(在底部的源代碼中顯示)。但是,我確信這是做事的錯誤方法,或者我的實現是不正確的。我無法在swagger 2.0規范中找到繼承的示例(如下所示),但是找到了composition的示例。我可以肯定的是,這個“判別器”在很大程度上發揮了作用,但不確定我需要做什么。
3 回答

慕仙森
TA貢獻1827條經驗 獲得超8個贊
這里的所有答案都已經很好了,但是我只想對構成與繼承作一點說明。根據Swagger / OpenAPI Spec,要實現組合,使用該allOf屬性就足夠了,正如@oblalex正確指出的那樣。然而,為了實現繼承,你需要使用allOf與discriminator,如通過@TomaszS?tkowski例子。
另外,我在API Handyman上找到了更多有關組合和繼承的 Swagger示例。它們是Arnaud Lauret 出色的Swagger / OpenAPI教程系列的一部分,我認為每個人都應該簽出。
添加回答
舉報
0/150
提交
取消