postmanlabs / postman-collection

Javascript module that allows a developer to work with Postman Collections
https://www.postmanlabs.com/postman-collection/
Apache License 2.0
457 stars 217 forks source link

Writing back a collection to json after modification does not save collection in correct format #569

Closed aksbenz closed 5 years ago

aksbenz commented 6 years ago

I load a collection into collection object using:

myCollection = new Collection(JSON.parse(fs.readFileSync('sample-collection.json').toString()));

// Do some manipulations and export

fs.writeFileSync('myCollection.postman_collection', JSON.stringify(myCollection, null, 2));

The original request has some disabled parameters, but in the exported collection, the URL has all the parameters. Even without exporting when using this Collection object as a parameter to newman api (options.collection), all the requests made by newman include disabled parameters. Due to this I am unable to use newman api with Collection object, since I need to modify collections before running them.

If I do this with newman, it executes the request with disabled parameters:

newman.run({
    collection: myCollection,
    reporters: 'cli'
}, (err, summary) => {
    console.log('DONE');
});

Original Collection:

{
    "info": {
        "name": "Temp",
        "_postman_id": "734b99af-72b1-a510-dc4c-10214c91aeee",
        "description": "",
        "schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"
    },
    "item": [
        {
            "name": "GET Request",
            "event": [
                {
                    "listen": "test",
                    "script": {
                        "type": "text/javascript",
                        "exec": [
                            "var responseJSON;",
                            "",
                            "try { ",
                            "    responseJSON = JSON.parse(responseBody); ",
                            "    tests['response is valid JSON'] = true;",
                            "}",
                            "catch (e) { ",
                            "    responseJSON = {}; ",
                            "    tests['response is valid JSON'] = false;",
                            "}",
                            "",
                            "tests['response json contains headers'] = _.has(responseJSON, 'headers');",
                            "tests['response json contains args'] = _.has(responseJSON, 'args');",
                            "tests['response json contains url'] = _.has(responseJSON, 'url');",
                            "",
                            "tests['args key contains argument passed as url parameter'] = ('test' in responseJSON.args);",
                            "tests['args passed via request url params has value \"123\"'] = (_.get(responseJSON, 'args.test') === \"123\");"
                        ]
                    }
                }
            ],
            "request": {
                "method": "GET",
                "header": [],
                "body": {},
                "url": {
                    "raw": "https://postman-echo.com/get?test=123&value1=test1",
                    "protocol": "https",
                    "host": [
                        "postman-echo",
                        "com"
                    ],
                    "path": [
                        "get"
                    ],
                    "query": [
                        {
                            "key": "test",
                            "value": "123",
                            "equals": true
                        },
                        {
                            "key": "value1",
                            "value": "test1",
                            "equals": true
                        },
                        {
                            "key": "value2",
                            "value": "test2",
                            "equals": true,
                            "disabled": true
                        },
                        {
                            "key": "value3",
                            "value": "test3",
                            "equals": true,
                            "disabled": true
                        }
                    ]
                },
                "description": "The HTTP `GET` request method is meant to retrieve data from a server. The data\nis identified by a unique URI (Uniform Resource Identifier). \n\nA `GET` request can pass parameters to the server using \"Query String \nParameters\". For example, in the following request,\n\n> http://example.com/hi/there?hand=wave\n\nThe parameter \"hand\" has the value \"wave\".\n\nThis endpoint echoes the HTTP headers, request parameters and the complete\nURI requested."
            },
            "response": []
        }
    ]
}

Exported Collection:

{
  "info": {
    "id": "734b99af-72b1-a510-dc4c-10214c91aeee",
    "name": "Temp",
    "schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"
  },
  "event": [],
  "variable": [],
  "item": [
    {
      "id": "6a4bba75-d29c-4505-91b2-7fca425cb853",
      "name": "GET Request",
      "request": {
        "url": "https://postman-echo.com/get?test=123&value1=test1&value2=test2&value3=test3",
        "method": "GET",
        "body": {},
        "description": {
          "content": "The HTTP `GET` request method is meant to retrieve data from a server. The data\nis identified by a unique URI (Uniform Resource Identifier). \n\nA `GET` request can pass parameters to the server using \"Query String \nParameters\". For example, in the following request,\n\n> http://example.com/hi/there?hand=wave\n\nThe parameter \"hand\" has the value \"wave\".\n\nThis endpoint echoes the HTTP headers, request parameters and the complete\nURI requested.",
          "type": "text/plain"
        }
      },
      "response": [],
      "event": [
        {
          "listen": "test",
          "script": {
            "type": "text/javascript",
            "exec": [
              "var responseJSON;",
              "",
              "try { ",
              "    responseJSON = JSON.parse(responseBody); ",
              "    tests['response is valid JSON'] = true;",
              "}",
              "catch (e) { ",
              "    responseJSON = {}; ",
              "    tests['response is valid JSON'] = false;",
              "}",
              "",
              "tests['response json contains headers'] = _.has(responseJSON, 'headers');",
              "tests['response json contains args'] = _.has(responseJSON, 'args');",
              "tests['response json contains url'] = _.has(responseJSON, 'url');",
              "",
              "tests['args key contains argument passed as url parameter'] = ('test' in responseJSON.args);",
              "tests['args passed via request url params has value \"123\"'] = (_.get(responseJSON, 'args.test') === \"123\");"
            ]
          }
        }
      ]
    }
  ]
}
kbuzby commented 6 years ago

I'm not exactly sure, but my guess is that you'd want to use the toJSON() method on the collection object and then call JSON.stringify on the result of that.

shamasis commented 6 years ago

@kamalaknn runtime should now do disabled support check within itself and property list could help in filtering that out. Maybe schedule in next release.

shamasis commented 6 years ago

This is planned for runtime v8.

shamasis commented 5 years ago

@codenirvana - is the initiative around handling disabled properties taking care of this?

codenirvana commented 5 years ago

@aksbenz can you confirm if this still persists with the latest version of the postman-collection? Also, try using JSON.stringify(myCollection.toJSON(), null, 2);

codenirvana commented 5 years ago

Closing because of inactivity.