Django REST Swagger:如何在Swagger设置中使用安全部分?

时间:2022-10-29 17:48:06

I'm trying to build the Swagger settings for SecurityDefinition in order to get the following result in openapi.json:

我正在尝试为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"]
}]

In my settings.py I have addded the following swagger settings:

在我的settings.py中,我添加了以下swagger设置:

# 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"]
  }]
}

The issue is that in the openapi.json which generated by Swagger there is not the security dict and I have no clue how it is generated.

问题是在Swagger生成的openapi.json中没有安全字典,我也不知道它是如何生成的。

Below, presented the generated openapi.json:

下面,介绍了生成的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": {...}
}

Is there any better way to describe this concept in my Swagger settings? Or can you describe me which is the process and how it is working in order to generate the openapi.json file?

有没有更好的方法在我的Swagger设置中描述这个概念?或者你能描述一下这个过程以及它如何工作以生成openapi.json文件吗?

1 个解决方案

#1


4  

When in doubt, check the code. You can see the definition of OpenAPIRenderer here:

如有疑问,请检查代码。你可以在这里看到OpenAPIRenderer的定义:

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

So one way to do this is to subclass, for example:

所以这样做的一种方法是子类化,例如:

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

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

        return data

Then you can use this renderer class for your view. Hope it helps!

然后,您可以将此渲染器类用于视图。希望能帮助到你!

#1


4  

When in doubt, check the code. You can see the definition of OpenAPIRenderer here:

如有疑问,请检查代码。你可以在这里看到OpenAPIRenderer的定义:

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

So one way to do this is to subclass, for example:

所以这样做的一种方法是子类化,例如:

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

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

        return data

Then you can use this renderer class for your view. Hope it helps!

然后,您可以将此渲染器类用于视图。希望能帮助到你!