joolfe / postman-to-openapi

🛸 Convert postman collection to OpenAPI
MIT License
577 stars 99 forks source link

Custom JSON injection #244

Open Kinjalrk2k opened 1 year ago

Kinjalrk2k commented 1 year ago

Custom JSON can be grabbed from an external file and injected directly into the generated OpenAPI doc.

Related Issues

Checklist

Note for maintainers/developers

I was fiddling around with the code base and came up with this approach. If you suggest any improvements to my current approach and/or want to present with your own, I encourage you to use this PR as a brainstorming thread. Additionally, if this PR doesn't directly close #78, I would like to present this PR as a new feature

Example

A custom JSON file can be as follows:

{
  "info.x-summary": "This is a very basic example of an API Documentation",
  "paths./note.post.x-code-samples": [
    {
      "lang": "NodeJs",
      "label": "Axios",
      "source": "var axios = require('axios');\nvar data = 'This is an example Note';\n\naxios.post('https://api.io/note/', data).then(function (response) {\nconsole.log(JSON.stringify(response.data));\n})\n.catch(function (error) {\nconsole.log(error);\n});"
    }
  ]
}

Look carefully at how the fields in the JSON file are defined. They contain the direct path of the JSON where injection occurs

The above JSON injection on the Basic Postman collection PostmantoOpenAPI.json produces the following output:

openapi: 3.0.0
info:
  title: Postman to OpenAPI
  description: Mi super test collection from postman
  version: 1.1.0
+ x-summary: This is a very basic example of an API Documentation
servers:
  - url: https://api.io
paths:
  /users:
    post:
      tags:
        - default
      summary: Create new User
      description: Create a new user into your amazing API
      requestBody:
        content:
          application/json:
            schema:
              type: object
              example:
                example: field
                other:
                  data1: 'yes'
                  data2: 'no'
      responses:
        '200':
          description: Successful response
          content:
            application/json: {}
  /posts:
    post:
      tags:
        - default
      summary: Create a post
      requestBody:
        content:
          text/plain: {}
      responses:
        '200':
          description: Successful response
          content:
            application/json: {}
  /note:
    post:
      tags:
        - default
      summary: Create a note
      description: Just an example of text raw body
      requestBody:
        content:
          text/plain:
            schema:
              type: string
              example: This is an example Note
      responses:
        '200':
          description: Successful response
          content:
            application/json: {}
+     x-code-samples:
+      - lang: NodeJs
+         label: Axios
+         source: |-
+          var axios = require('axios');
+          var data = 'This is an example Note';

+          axios.post('https://api.io/note/', data).then(function (response) {
+          console.log(JSON.stringify(response.data));
+          })
+          .catch(function (error) {
+          console.log(error);
+          });