R.y*_*yan 8 python python-3.x swagger django-rest-framework drf-yasg
我知道在传统的 swagger YAML 文件中,我们可以定义方案:
schemes:
- http
- https
//OR
schemes: [http, https]
Run Code Online (Sandbox Code Playgroud)
但是,如何使用drf-yasg库自动生成的 swagger 页面做同样的事情?
现在,生成的 swagger 页面仅包含HTTP方案,但HTTPS已丢失。我试过将DEFAULT_API_URLin设置setting.py为https://mybaseurl.com,但它似乎不起作用。
小智 21
要在 swagger 中同时使用http和https方案,您可以OpenAPISchemaGenerator从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
Run Code Online (Sandbox Code Playgroud)
generator_class所以现在你可以使用它get_schema_view()
schema_view = get_schema_view(
openapi.Info( ... ),
public=True,
generator_class=BothHttpAndHttpsSchemaGenerator, # Here
permission_classes=(AllowAny,)
)
Run Code Online (Sandbox Code Playgroud)
小智 9
有一个解决方案。
在 中定义 get_schema_view() 时urls.py,使用以下代码:
schema_view = get_schema_view(
openapi.Info( ... ),
url='https://example.net/api/v1/', # Important bit
public=True,
permission_classes=(permissions.AllowAny,)
)
Run Code Online (Sandbox Code Playgroud)
注意:您可以使用 https 或 http,因为这样可以更好地将此解决方案与不同设置的环境变量一起使用。
| 归档时间: |
|
| 查看次数: |
2956 次 |
| 最近记录: |