Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use custom YAML file as API documentation in Django REST Framework?

I need to add API documentation to my project. I wrote my custom schema using swagger editor and now I have a YAML file as follows:

swagger: "2.0"
info:
  description: "This is the documentation of Orion Protocol API"
  version: "1.0.0"
  title: "Orion Protocol API"
host: "127.0.0.1:8000"
basePath: "/api/"
paths:
  /api/decode:
    post:
      tags:
      - "pet"
      summary: "Decode the payload"
      consumes:
      - "application/json"
      produces:
      - "application/json"
      parameters:
      - in: "body"
        name: "body"
        description: "Packet data"
        required: true
        schema:
          $ref: "#/definitions/PacketData"
      responses:
        "405":
          description: "Invalid input"

  /api/encode:
   post:
     description: "Encoding configuration parameters for the devices"
     produces:
     - "string"
     parameters:
     - in: "body"
       name: "body"
       description: "Addresses and values of configuration parameters"
      required: true
       schema:
         $ref: "#/definitions/ConfigPayload"
     responses:
       "405":
         description: "Invalid input"


  definitions:
    PacketData:
      type: "object"
      required:
      - "payload"
      properties:
     payload:
       type: "string"
       description: "Packet string starting with 78"
       example: "78010013518BB325140400000500000AAA0000002A6E0000004AC05D00006A00000000"
    ConfigPayload:
        type: "object"
       properties:
          Addresses of the configuration parameter:
            type: "string"
            description: "According to the documentation of configuration protocol"
            example: "542"

Now how can I add this to the project? Where it should locate in the project? May the views render this file? I need to have the following path:

urlpatterns = [
     path('documentation/', some-view-that-will-render-yaml)
]
like image 427
Aigerim Sadir Avatar asked Jun 10 '20 05:06

Aigerim Sadir


People also ask

What is Yaml in REST API?

YAML is an attempt to create a more human-readable data exchange format. It's similar to JSON (which is actually a subset of YAML) but uses spaces, colons, and hyphens to indicate the structure. Many computers ingest data in a YAML or JSON format.

How do I upload an Excel file in Django REST framework?

So you have two choices: let ModelViewSet and ModelSerializer handle the job and send the request using content-type=multipart/form-data; set the field in ModelSerializer as Base64ImageField (or) Base64FileField and tell your client to encode the file to Base64 and set the content-type=application/json.


1 Answers

I found a solution and it was pretty easy. We need to convert yaml to json and save it in static folder. Then: urls.py

urlpatterns =  [
    ...
    path('swagger-ui/', TemplateView.as_view(
        template_name='swagger-ui.html',
        extra_context={'schema_url': 'openapi-schema'}
    ), name='swagger-ui'),
]

templates/swagger-ui.html:

{% load static %}
<html>
  <head>
    <title>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: "{% static 'swagger.json' %}" ,
    dom_id: '#swagger-ui',
    presets: [
      SwaggerUIBundle.presets.apis,
      SwaggerUIBundle.SwaggerUIStandalonePreset
    ]
  })
    </script>
  </body>
</html>

and save your custom schema as json in static folder. Works!

like image 126
Aigerim Sadir Avatar answered Oct 17 '22 20:10

Aigerim Sadir