【发布时间】:2019-10-14 21:58:27
【问题描述】:
在我的应用程序中,我需要为多个客户端提供多个带有分组端点的 Swagger 页面。
我的一个客户端(路径)提供移动应用 API,另一个提供 Web 客户端 API。 URL 模式相应地保存在 2 个不同的 urls.py 中。
我正在使用 drf-yasg 为我的 API 生成架构。
要为那些我为每个 urls.py 文件初始化 2 个单独的 schema_views 的人生成 swagger 规范,如下所示:
from api_mobile.urls import urlpatterns as mobile_patterns
from api_web.urls import urlpatterns as web_patterns
mobile_schema_view = get_schema_view(
openapi.Info(
title="Mobile API",
default_version='v3',
),
public=True,
permission_classes=(permissions.AllowAny,),
patterns=mobile_patterns,
)
web_schema_view = get_schema_view(
openapi.Info(
title="Web API",
default_version='v1',
),
public=True,
permission_classes=(permissions.AllowAny,),
patterns=web_patterns,
)
urlpatterns = [
path(
'api/mobile/docs',
mobile_schema_view.with_ui('swagger', cache_timeout=0),
name='mobile-schema-ui'
),
path(
'api/web/docs',
web_schema_view.with_ui('swagger', cache_timeout=0),
name='web-schema-ui'
),
path('api/mobile/v3/', include('api_mobile.urls'), name='mobile_urls'),
path('api/web/v1/', include('api_web.urls'), name='web_urls'),
...
]
其中mobile_patterns 和web_patterns 只是网址格式列表。
如果我打开 http://localhost:8000/api/mobile/docs 或 http://localhost:8000/api/web/docs 我确实看到了为两个模式列表正确生成的架构,但是如果我尝试直接从 swagger 规范页面发出请求,所有端点都会返回 404 错误——它们都尝试在不提供端点完整路径的情况下向不存在的 url 模式发出请求。
因此,如果我从 mobile 端点向任何视图发出请求,swagger 会尝试在
http://localhost:8000/some_mobile_url/ 而不是http://localhost:8000/api/mobile/v3/some_mobile_url/
另外一个模式的情况也一样,swagger 错误地请求http://localhost:8000/some_web_url/ 而不是使用完整路径
http://localhost:8000/api/web/v3/some_web_url/
显然,能够直接通过swagger 测试 API 非常重要,因此对我而言,规范本身是不够的。
这是我错误配置swagger itlesf 的问题,还是我应该以某种方式提供swagger 的路径,以便相应地为每个网址添加完整路径?
【问题讨论】:
标签: django django-rest-framework swagger openapi drf-yasg