如何使用 drf-yasg 自动生成的 swagger 页面配置“HTTPS”方案?

2024-04-22

我知道在传统的 swagger YAML 文件中,我们可以使用以下方式定义方案:

schemes:
  - http
  - https

//OR

schemes: [http, https]

但是,我如何使用自动生成的 swagger 页面做同样的事情drf-yasg图书馆?

现在,生成的swagger页面只包含HTTP计划,但是HTTPS不见了。我试过设置DEFAULT_API_URL in setting.py to https://mybaseurl.com,但似乎不起作用。


两者同时使用http and https你可以扩展的招摇计划OpenAPISchemaGenerator from drf_yasg.generators.

class BothHttpAndHttpsSchemaGenerator(OpenAPISchemaGenerator):
    def get_schema(self, request=None, public=False):
        schema = super().get_schema(request, public)
        schema.schemes = ["http", "https"]
        return schema

所以现在你可以用它作为generator_class for get_schema_view()

schema_view = get_schema_view(
    openapi.Info( ... ),
    public=True,
    generator_class=BothHttpAndHttpsSchemaGenerator, # Here
    permission_classes=(AllowAny,)
)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 drf-yasg 自动生成的 swagger 页面配置“HTTPS”方案? 的相关文章

随机推荐