postman-to-openapi icon indicating copy to clipboard operation
postman-to-openapi copied to clipboard

Custom JSON injection

Open Kinjalrk2k opened this issue 3 years ago • 3 comments

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

Related Issues

  • #78

Checklist

  • [x] Add library code (This PR)
  • [x] Add support from CLI (Using the config options parameter)
  • [x] New Unit Test case
  • [x] Example
  • [ ] Documentation?

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);
+          });

Kinjalrk2k avatar Nov 11 '22 13:11 Kinjalrk2k

@Kinjalrk2k you have a merge conflict

amowogbaje avatar Aug 30 '24 08:08 amowogbaje

@Kinjalrk2k you have a merge conflict

Yes! This PR is around 2 years old and bound to have merge conflicts. If the owners/maintainers plan on merging this as a feature, I can work towards resolving the conflicts!

Kinjalrk2k avatar Aug 30 '24 09:08 Kinjalrk2k

ooooooh

Gideon Amowogbaje [image: https://]about.me/amowogbaje https://about.me/amowogbaje?promo=email_sig&utm_source=product&utm_medium=email_sig&utm_campaign=edit_panel&utm_content=plaintext

On Fri, Aug 30, 2024 at 10:02 AM Kinjal Raykarmakar < @.***> wrote:

@Kinjalrk2k https://github.com/Kinjalrk2k you have a merge conflict

Yes! This PR is around 2 years old and bound to have merge conflicts. If the owners/maintainers plan on merging this as a feature, I can work towards resolving the conflicts!

— Reply to this email directly, view it on GitHub https://github.com/joolfe/postman-to-openapi/pull/244#issuecomment-2320565240, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACC5L4WPLCYN6NNPW2EHGATZUAYKTAVCNFSM6AAAAABNL67AF6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMRQGU3DKMRUGA . You are receiving this because you commented.Message ID: @.***>

amowogbaje avatar Aug 30 '24 09:08 amowogbaje

close because of archive, read the README for more info about that decision.

joolfe avatar Dec 27 '24 11:12 joolfe