【问题标题】:Django REST Swagger: How to use security section in Swagger settings?Django REST Swagger:如何在 Swagger 设置中使用安全部分?
【发布时间】:2017-01-20 09:45:02
【问题描述】:

我正在尝试为 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 文件的过程以及它是如何工作的吗?

【问题讨论】:

  • 能否把openapi.json文件内容贴出来方便调试
  • 我刚刚更新了问题!
  • 为什么要在列表中构造字典来定义安全性?用普通的 dict 试试。
  • 当你想拥有OAuth 2.0 - Password时,这是标准的Swagger模板......另外,我用普通的dict尝试过,我得到了相同的结果。
  • 是否尝试在swagger.io/irc 中提问?

标签: python django swagger django-swagger


【解决方案1】:

如有疑问,请检查代码。可以看OpenAPIRenderer的定义here

class OpenAPIRenderer(BaseRenderer):
    media_type = 'application/openapi+json'
    charset = None
    format = 'openapi'

    def render(self, data, accepted_media_type=None, renderer_context=None):
        if renderer_context['response'].status_code != status.HTTP_200_OK:
            return JSONRenderer().render(data)
        extra = self.get_customizations()

        return OpenAPICodec().encode(data, extra=extra)

    def get_customizations(self):
        """
        Adds settings, overrides, etc. to the specification.
        """
        data = {}
        if swagger_settings.SECURITY_DEFINITIONS:
            data['securityDefinitions'] = swagger_settings.SECURITY_DEFINITIONS

        return data

所以一种方法是子类化,例如:

class MyOpenAPIRenderer(OpenAPIRenderer):
    def get_customizations(self):
        data = super().get_customizations()

        # your customizations
        data["security"] = swagger_settings.SECURITY

        return data

然后,您可以将这个渲染器类用于您的视图。希望对您有所帮助!

【讨论】:

  • 不幸的是,这不起作用,因为字段 SECURITY 未在默认值中定义,因此我们无法定义 swagger_settings.SECURITY。我认为这应该在图书馆更新。 Github 上的问题:github.com/marcgibbons/django-rest-swagger/issues/628
  • @physicalattraction 啊,我明白了。请记住,您也在使用 Django,它具有非常灵活的 settings 模块,因此您可以使用:from django.conf import settings 解决上述问题,然后将有问题的行替换为:data["security"] = settings.SWAGGER_SETTINGS["SECURITY"]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-07
  • 2021-10-30
  • 1970-01-01
  • 2022-08-03
  • 1970-01-01
  • 1970-01-01
  • 2016-12-06
相关资源
最近更新 更多