在我的“簡(jiǎn)化” API中,所有響應(yīng)均從基本“響應(yīng)”類(lèi)派生(繼承)。響應(yīng)類(lèi)由填充有元數(shù)據(jù)的頭和包含用戶(hù)所請(qǐng)求的核心數(shù)據(jù)的主體組成。布置響應(yīng)(以JSON格式),使得所有元數(shù)據(jù)都位于第一個(gè)“層”上,并且body是這樣一個(gè)稱(chēng)為“ body”的單個(gè)屬性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來(lái)定義這種關(guān)系:{ ... "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" } }}然后,我嘗試通過(guò)創(chuàng)建從body / header繼承的各種body / header類(lèi)來(lái)創(chuàng)建不同的響應(yīng),然后創(chuàng)建由相關(guān)的header / body類(lèi)組成的子響應(yīng)類(lèi)(在底部的源代碼中顯示)。但是,我確信這是做事的錯(cuò)誤方法,或者我的實(shí)現(xiàn)是不正確的。我無(wú)法在swagger 2.0規(guī)范中找到繼承的示例(如下所示),但是找到了composition的示例。我可以肯定的是,這個(gè)“判別器”在很大程度上發(fā)揮了作用,但不確定我需要做什么。
3 回答

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