Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query parameters included on JSON.stringify even when omitted #936

Open
tklun opened this issue Aug 30, 2019 · 6 comments
Open

Query parameters included on JSON.stringify even when omitted #936

tklun opened this issue Aug 30, 2019 · 6 comments

Comments

@tklun
Copy link

tklun commented Aug 30, 2019

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?

@jbelford
Copy link

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.

@csk1827
Copy link

csk1827 commented Mar 7, 2020

@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()));

@StarpTech
Copy link

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

@StarpTech
Copy link

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(),
    });

@Fabiomad85
Copy link

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

@dsinghvi
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants