亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

昂首闊步的繼承與組成

昂首闊步的繼承與組成

慕容3067478 2019-11-28 10:42:42
在我的“簡化” 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教程系列的一部分,我認為每個人都應該簽出。


查看完整回答
反對 回復 2019-11-28
  • 3 回答
  • 0 關注
  • 708 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號