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

Query parameters included on JSON.stringify even when omitted

Open tklun opened this issue 5 years ago • 6 comments

Currently, when I construct a Url without any query parameters, the instance of the url works as expected in isolation. Both toString() and toJSON() leave out any query string.

However, when I add the same url to a collection, the query value is filled with a lot of extra fields that persist when imported back into Postman.

The code:

var fs = require("fs"),
  {
    Collection,
    Item,
    ItemGroup,
    Request,
    Url
  } = require("postman-collection");

var url = new Url("https://www.example.com");

var item = new Item({
  name: "Test",
  request: new Request({
    url
  }),
});

var itemGroup = new ItemGroup();

itemGroup.items.add(item);

var collection = new Collection({
  item: itemGroup
});

console.log("Collection Stringify: ", JSON.stringify(collection, null, 2));

outputs:

Collection Stringify:  {
  "item": [
    {
      "id": "9f670340-f4c4-4379-8fbc-be33d445c20d",
      "item": [
        {
          "id": "57e49c56-9f7e-471a-b23e-f9f5fbf243c0",
          "name": "Test",
          "request": {
            "url": {
              "protocol": "https",
              "host": [
                "www",
                "example",
                "com"
              ],
              "query": [
                {
                  "key": "members",
                  "value": [
                    {
                      "key": "members",
                      "value": []
                    },
                    {
                      "key": "reference",
                      "value": {}
                    },
                    {
                      "key": "Type",
                      "value": {
                        "_postman_propertyName": "QueryParam",
                        "_postman_propertyIndexKey": "key",
                        "_postman_propertyAllowsMultipleValues": true
                      }
                    },
                    {
                      "key": "_postman_listIndexKey",
                      "value": "key"
                    },
                    {
                      "key": "_postman_listAllowsMultipleValues",
                      "value": true
                    }
                  ]
                },
                {
                  "key": "reference",
                  "value": {
                    "members": {
                      "key": "members",
                      "value": []
                    },
                    "reference": {
                      "key": "reference",
                      "value": {}
                    },
                    "Type": {
                      "key": "Type",
                      "value": {
                        "_postman_propertyName": "QueryParam",
                        "_postman_propertyIndexKey": "key",
                        "_postman_propertyAllowsMultipleValues": true
                      }
                    },
                    "_postman_listIndexKey": {
                      "key": "_postman_listIndexKey",
                      "value": "key"
                    },
                    "_postman_listAllowsMultipleValues": {
                      "key": "_postman_listAllowsMultipleValues",
                      "value": true
                    }
                  }
                },
                {
                  "key": "Type",
                  "value": {
                    "_postman_propertyName": "QueryParam",
                    "_postman_propertyIndexKey": "key",
                    "_postman_propertyAllowsMultipleValues": true
                  }
                },
                {
                  "key": "_postman_listIndexKey",
                  "value": "key"
                },
                {
                  "key": "_postman_listAllowsMultipleValues",
                  "value": true
                }
              ],
              "variable": []
            },
            "method": "GET"
          },
          "response": [],
          "event": []
        }
      ],
      "event": []
    }
  ],
  "event": [],
  "variable": [],
  "info": {
    "_postman_id": "5ad37d6a-5bef-4cba-9821-036b34b287c8",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  }
}

If I'm doing something incorrectly, would someone be able to point me in the right direction?

tklun avatar Aug 30 '19 20:08 tklun

I have ran into this same issue and even if I add query parameters this is all I get. The same occurs for urlencoded body.

jbelford avatar Sep 24 '19 16:09 jbelford

@tklun @jbelford you are directly trying to stringify the complete Collection object by doing

console.log("Collection Stringify: ", JSON.stringify(collection, null, 2));

The query parameters will be correctly parsed if you first convert the collection object to JSON Object(by doing collection.toJSON()) and then stringify. The right way of stringifying the collection object is as follows:

console.log("Collection Stringify: ", JSON.stringify(collection.toJSON()));

csk1827 avatar Mar 07 '20 16:03 csk1827

Did you really test it? I still can reproduce it in both examples.

StarpTech avatar Sep 25 '21 22:09 StarpTech

I found it out. You really need to call toJSON on all objects Request, Collection, ...

Invalid

collectionDefinition.item?.push({
      name: op.Name,
      request: new Request(request),
    });

valid

collectionDefinition.item?.push({
      name: op.Name,
      request: new Request(request).toJSON(),
    });

StarpTech avatar Sep 25 '21 23:09 StarpTech

Hi Everybody! @StarpTech I tried your solution but I still get the postman_listIndex polluted query params

Fabiomad85 avatar Jun 19 '22 15:06 Fabiomad85

Couldn't get it to work either :/ at this point im just writing my own types

dsinghvi avatar Jul 21 '22 23:07 dsinghvi