我正在尝试为 SecurityDefinition 构建 Swagger 设置,以便在 openapi.json 中获得以下结果:
"securityDefinitions": {
"password": {
"type": "oauth2",
"tokenUrl": "http://example.com/oauth/token",
"flow": "password",
"scopes": {
"write": "allows modifying resources",
"read": "allows reading resources"
}
}
},
"security": [{
"password": ["read", "write"]
}]
在我的 settings.py 中,我添加了以下招摇设置:
# Swagger settings
SWAGGER_SETTINGS = {
"SECURITY_DEFINITIONS": {
"password": {
"type": "oauth2",
"tokenUrl": "http://example.com/oauth/token",
"flow": "password",
"scopes": {
"write": "allows modifying resources",
"read": "allows reading resources"
}
}
},
"SECURITY": [{
"password": ["read", "write"]
}]
}
问题是在 Swagger 生成的 openapi.json 中没有security
字典,我不知道它是如何生成的。
下面,展示了生成的 openapi.json:
{
"info": {
"title": "Example Service API",
"version": ""
},
"host": "http://example.com",
"swagger": "2.0",
"securityDefinitions": {
"password": {
"type": "oauth2",
"scopes": {
"write": "allows modifying resources",
"read": "allows reading resources"
},
"tokenUrl": "http://example.com/oauth/token",
"flow": "password"
}
},
"paths": {...}
}
在我的 Swagger 设置中是否有更好的方法来描述这个概念?或者您能描述一下生成 openapi.json 文件的过程以及它是如何工作的吗?