[英]How to generate swagger specification file (json) from python using annotations in code?
[英]Generate Swagger specification from Python code without annotations
我正在尋找一種通過Python服務的定義生成Swagger規范(JSON和Swagger-UI)的方法。 我發現了很多選項(通常基於Flask),但是它們都使用了批注,我無法從代碼中正確處理這些批注,例如,在循環中使用不同的路由定義10個等效的服務(在此示例中,可能會產生處理程序,首先調用函數以獲取它)。
有沒有注釋的方法嗎? 我想通過調用具有與該API方法的實現和文檔相對應的值的函數(或方法,或類的構造函數)在API及其文檔中生成方法。
我建議調查apispec 。
apispec是可插入的API規范生成器。
當前支持OpenAPI 2.0規范(fka Swagger 2.0)
apispec
from apispec import APISpec
spec = APISpec(
title='Gisty',
version='1.0.0',
info=dict(description='A minimal gist API')
)
spec.definition('Gist', properties={
'id': {'type': 'integer', 'format': 'int64'},
'content': {'type': 'string'},
})
spec.add_path(
path='/gist/{gist_id}',
operations=dict(
get=dict(
responses={
'200': {
'schema': {'$ref': '#/definitions/Gist'}
}
}
)
)
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.