0

Good day everyone! I am writing a game application using Django and Django rest framework. I have been working on swagger documentation and the main problem is, that autogenerated schema is without any params. Here are examples of my realization:

Tempaltes.html

  {% load static %}
<!DOCTYPE html>
<html>
  <head>
    <title>School Service Documentation</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" href="//unpkg.com/swagger-ui-dist@3/swagger-ui.css" />
  </head>
  <body>
    <div id="swagger-ui"></div>
    <script src="//unpkg.com/swagger-ui-dist@3/swagger-ui-bundle.js"></script>
    <script>
    const ui = SwaggerUIBundle({
        url: "{% url schema_url %}",
        dom_id: '#swagger-ui',
        presets: [
          SwaggerUIBundle.presets.apis,
          SwaggerUIBundle.SwaggerUIStandalonePreset
        ],
        layout: "BaseLayout"
      })
</script>

Urls.py

urlpatterns = [

    path('api_schema/', get_schema_view(
        title='API Schema',
        version="3.0.0",
        description='Guide for the REST API',
    ),
         name='api_schema'),
    path('docs/', TemplateView.as_view(
        template_name='index.html',
        extra_context={'schema_url': 'api_schema'}
    ), name='swagger-ui'),
    path('admin/', admin.site.urls),
    path('game/', include('GameWorld.urls'))
]

Views.py

class EventDetailsView(GenericAPIView):
    serializer_class = serializers.EventDetailsSerializer
    """
    hard_token
    event_type
    event_id =
    """
    # schema = ManualSchema(fields=[
    #     coreapi.Field(
    #         "hard_token",
    #         required=True,
    #         location="query",
    #         schema=coreschema.String()
    #     ),
    #     coreapi.Field(
    #         "event_type",
    #         required=True,
    #         location="query",
    #         schema=coreschema.String()
    #     ),
    #     coreapi.Field(
    #         "event_id",
    #         required=True,
    #         location="query",
    #         schema=coreschema.Integer()
    #     ),
    # ])

    @auth
    def post(self, request, *args, **kwargs):
        event_id = request.data.get('event_id')
        for_user_events = engine_models.Event.objects.filter(owner__pk=request.user.pk)
        event = for_user_events.get(pk=event_id)
        return Response(event.data)

As you can see in the post. I also tried to rewrite it manually using Manual schema according to the docs but the result was not successful.

Image: SwaggerUI

Please, can you help me with this task. Appreciated, have a great week!

  • This may help: https://stackoverflow.com/questions/26832510/django-rest-swagger-how-can-i-specify-the-parameter-type-in-the-docstring – bhansa Mar 09 '22 at 11:08

1 Answers1

0

You can use these:

import coreapi

    schema = AutoSchema(
            manual_fields=[
            coreapi.Field("my", required=True,location='query',description='City name or airport code.'),
            coreapi.Field("my1", required=True,location='query',description='City name or airport code.'),
            coreapi.Field("my2", required=True,location='query',description='City name or airport code.'),
        ])
4b0
  • 21,981
  • 30
  • 95
  • 142