OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

drf_yasg: Not able to generate Swagger UI from YAML file

  • Thread starter Thread starter Ankit Tiwari
  • Start date Start date
A

Ankit Tiwari

Guest
I am using the drf_yasg library in my Django REST Framework project to generate Swagger documentation. I have defined my API endpoints and schemas in a YAML file, but when I try to generate the Swagger UI, it is not rendering correctly. It's rendering UI using auto-discovery, which does not have information about functions, but I want to add more information to it.

Some key details:

  • I have installed drf_yasg and added it to my Django INSTALLED_APPS
  • I have defined my API endpoints and schemas in a swagger.yaml file
  • In my Django URL config, I have added the get_schema_view() from drf_yasg to generate the schema

This is my project urls.py

Code:
from django.contrib import admin
from django.urls import path, include
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
from rest_framework import permissions


schema_view = get_schema_view(
    openapi.Info(
        title="My Application",
        default_version="v1",
        description="A sample API for learning DRF",
        terms_of_service="https://www.google.com/policies/terms/",
        contact=openapi.Contact(email="[email protected]"),
        license=openapi.License(name="BSD License"),
    ),
    public=True,
    permission_classes=(permissions.AllowAny,),
)

urlpatterns = [
    path('admin/', admin.site.urls),
    path('swagger/', schema_view.with_ui(
        'swagger', cache_timeout=0), name='schema-swagger-ui'),
    path('redoc/', schema_view.with_ui(
        'redoc', cache_timeout=0), name='schema-redoc'),
]

this how my function looks:

Code:
@api_view(['POST'])
def searchData(request):
    name = request.data.get('name')
    vendor = request.data.get('vendor')
    s = AppSearch()
    result = s.search(name, vendor)
    result = json.loads(json_util.dumps(result))
    return Response(result, status=status.HTTP_200_OK)

By default, Swagger is showing me the function and displaying the return type, but it is not displaying the request details like name and vendor. To resolve this, I have implemented those details in my schema.yml file, which is located in the root directory of my project (where manage.py resides).
<p>I am using the <a href="https://drf-yasg.readthedocs.io/en/stable/readme.html#installation" rel="nofollow noreferrer">drf_yasg</a> library in my <strong>Django REST Framework</strong> project to generate <strong>Swagger</strong> documentation. I have defined my API endpoints and schemas in a <strong>YAML</strong> file, but when I try to generate the <strong>Swagger UI</strong>, it is not rendering correctly.
It's rendering UI using auto-discovery, which does not have information about functions, but I want to add more information to it.</p>
<p>Some key details:</p>
<ul>
<li>I have installed <code>drf_yasg</code> and added it to my <strong>Django INSTALLED_APPS</strong></li>
<li>I have defined my API endpoints and schemas in a <code>swagger.yaml</code> file</li>
<li>In my <strong>Django URL</strong> config, I have added the <code>get_schema_view()</code> from <code>drf_yasg</code> to generate the schema</li>
</ul>
<p>This is my project <code>urls.py</code></p>
<pre class="lang-py prettyprint-override"><code>from django.contrib import admin
from django.urls import path, include
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
from rest_framework import permissions


schema_view = get_schema_view(
openapi.Info(
title="My Application",
default_version="v1",
description="A sample API for learning DRF",
terms_of_service="https://www.google.com/policies/terms/",
contact=openapi.Contact(email="[email protected]"),
license=openapi.License(name="BSD License"),
),
public=True,
permission_classes=(permissions.AllowAny,),
)

urlpatterns = [
path('admin/', admin.site.urls),
path('swagger/', schema_view.with_ui(
'swagger', cache_timeout=0), name='schema-swagger-ui'),
path('redoc/', schema_view.with_ui(
'redoc', cache_timeout=0), name='schema-redoc'),
]
</code></pre>
<p>this how my function looks:</p>
<pre><code>@api_view(['POST'])
def searchData(request):
name = request.data.get('name')
vendor = request.data.get('vendor')
s = AppSearch()
result = s.search(name, vendor)
result = json.loads(json_util.dumps(result))
return Response(result, status=status.HTTP_200_OK)
</code></pre>
<p>By default, Swagger is showing me the function and displaying the return type, but it is not displaying the request details like <code>name</code> and <code>vendor</code>. To resolve this, I have implemented those details in my <code>schema.yml</code> file, which is located in the root directory of my project (where <code>manage.py</code> resides).</p>
 

Latest posts

B
Replies
0
Views
1
Blundering Ecologist
B
Top