joolfe / postman-to-openapi

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

Cannot parse request and response bodies having JSON with comments #188

Closed dhwaneetbhatt closed 2 years ago

dhwaneetbhatt commented 2 years ago

Postman allows savings requests and responses with JSON that has comments (although the strict JSON implementation does not allow it).

But the library fails to convert this to OpenAPI because of a parse failure.

% p2o with-comments.postman_collection.json -f api.yaml
Error: Error parsing response example "200 OK Response" parse error is: Unexpected token / in JSON at position 85

This is a package that can be used to parse JSON with comments: https://github.com/microsoft/node-jsonc-parser

Example collection:

{
    "info": {
        "_postman_id": "76e4aff3-329c-4629-b03f-f0362ccac378",
        "name": "Httpbin",
        "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
    },
    "item": [
        {
            "name": "Post",
            "event": [
                {
                    "listen": "prerequest",
                    "script": {
                        "exec": [
                            ""
                        ],
                        "type": "text/javascript"
                    }
                },
                {
                    "listen": "test",
                    "script": {
                        "exec": [
                            ""
                        ],
                        "type": "text/javascript"
                    }
                }
            ],
            "request": {
                "method": "POST",
                "header": [],
                "body": {
                    "mode": "raw",
                    "raw": "{\n    \"name\": \"Bruce\" // name of the person\n}",
                    "options": {
                        "raw": {
                            "language": "json"
                        }
                    }
                },
                "url": {
                    "raw": "{{url}}/post",
                    "host": [
                        "{{url}}"
                    ],
                    "path": [
                        "post"
                    ]
                }
            },
            "response": [
                {
                    "name": "200 OK Response",
                    "originalRequest": {
                        "method": "POST",
                        "header": [],
                        "body": {
                            "mode": "raw",
                            "raw": "{\n    \"name\": \"{{name}}\" // name of the person\n}",
                            "options": {
                                "raw": {
                                    "language": "json"
                                }
                            }
                        },
                        "url": {
                            "raw": "{{url}}/post",
                            "host": [
                                "{{url}}"
                            ],
                            "path": [
                                "post"
                            ]
                        }
                    },
                    "status": "OK",
                    "code": 200,
                    "_postman_previewlanguage": "json",
                    "header": [
                        {
                            "key": "Date",
                            "value": "Fri, 06 May 2022 04:48:56 GMT"
                        },
                        {
                            "key": "Content-Type",
                            "value": "application/json"
                        },
                        {
                            "key": "Content-Length",
                            "value": "590"
                        },
                        {
                            "key": "Connection",
                            "value": "keep-alive"
                        },
                        {
                            "key": "Server",
                            "value": "gunicorn/19.9.0"
                        },
                        {
                            "key": "Access-Control-Allow-Origin",
                            "value": "*"
                        },
                        {
                            "key": "Access-Control-Allow-Credentials",
                            "value": "true"
                        }
                    ],
                    "cookie": [],
                    "body": "{\n    \"args\": {},\n    \"data\": \"{\\n    \\\"name\\\": \\\"Bruce\\\" // name of the person\\n}\", // this is the name received back\n    \"files\": {},\n    \"form\": {},\n    \"headers\": {\n        \"Accept\": \"*/*\",\n        \"Accept-Encoding\": \"gzip, deflate, br\",\n        \"Cache-Control\": \"no-cache\",\n        \"Content-Length\": \"45\",\n        \"Content-Type\": \"application/json\",\n        \"Host\": \"httpbin.org\",\n        \"Postman-Token\": \"23b5cfa0-afbf-4594-895d-fe92f6a4cfd5\",\n        \"User-Agent\": \"PostmanRuntime/7.29.0\",\n        \"X-Amzn-Trace-Id\": \"Root=1-6274a8b8-49add5292fbb91771b126810\"\n    },\n    \"json\": null,\n    \"origin\": \"54.86.50.139\",\n    \"url\": \"https://httpbin.org/post\"\n}"
                }
            ]
        }
    ],
    "event": [
        {
            "listen": "prerequest",
            "script": {
                "type": "text/javascript",
                "exec": [
                    ""
                ]
            }
        },
        {
            "listen": "test",
            "script": {
                "type": "text/javascript",
                "exec": [
                    ""
                ]
            }
        }
    ],
    "variable": [
        {
            "key": "url",
            "value": "https://httpbin.org",
            "type": "string"
        },
        {
            "key": "name",
            "value": "",
            "type": "string"
        }
    ]
}
DefCon-007 commented 2 years ago

I will take a look at this, will try to come up with a PR by 15th.