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

[Bug] when importing Postman collection, requests with same name in the same folder are ignored #799

Open
grtea opened this issue Oct 26, 2023 · 11 comments

Comments

@grtea
Copy link

grtea commented Oct 26, 2023

Encountered when importing a folder with multiple requests of the same name. Postman allows same naming of requests.

When there are multiple requests with the same name, only one of the requests is importing, while the other are ignored. There is no notice about this.

@jwetzell
Copy link
Contributor

@helloanoop This looks to be a side effect of the Collection Item files being stored using the name of the request as the file name. The piece that handles the import-collection ipc call is just overwriting the "duplicate" item because the filePath will be computed as being the same.

I think a solution would be to use something like the item.uid as the file name (the request names will still come over as they are stored in the .bru file). This does mean the files are a little hard to sort through so maybe a combo of name_uid or something so the collision wouldn't happen.

I would say in general that collection items with the same name SHOULD be allowed even when creating using the UI "New Request" process.

@Its-treason
Copy link
Member

We had the same problem with the insomnia collection import, this was fixed in #264. We probably need the same fix here.

@jwetzell
Copy link
Contributor

While that would work. I think resolving the problem of collection items in the same folder not being allowed to have the same name would be more ideal and solve this issue across the board.

@jwetzell
Copy link
Contributor

jwetzell commented Nov 1, 2023

If it is going to be a "rule" of bru that requests in a collection cannot have the same name (because filename = requestname.bru then a consistent way of dealing with duplicate request names from parsers should be determined and I think ideally in a way that the parsers don't have to come up with individual "fixes". Leaving it to the code that saves the parsed collection to apply some sort of rule for duplicate request names.

@koriit-kontakt
Copy link

In the meantime I'm treating my Postman collections with this script before importing - prefixing all request names with HTTP method.

import argparse
import json

parser = argparse.ArgumentParser()
parser.add_argument('--file', dest='file', required=True, help="Postman collection JSON file to alter")
parser.add_argument('--in-place', action='store_true', dest='in_place', default=False, help="Whether to edit file in place")
args = parser.parse_args()

with open(args.file, "r") as file:
    collection = json.load(file)

# check if this is a collection file just for sanity sake
if not str(collection.get("info", {}).get("schema", "")).endswith("collection.json"):
    raise Exception("Could not verify that this is a Postman collection file")


def adjust_name(item):
    items = item.get("item")
    if items is not None:
        for nested_item in items:
            adjust_name(nested_item)
        return

    request = item.get("request")
    if request is None:
        raise Exception("Not a request: " + str(item))

    item["name"] = str(request["method"]).lower() + "-" + (item['name'] if item["name"][0].isalnum() else item["name"][1:])


adjust_name(collection)

if not args.in_place:
    print(json.dumps(collection, indent=4))
else:
    with open(args.file, "w") as file:
        json.dump(collection, file, indent=4)
$ python3 prefix_request_names.py --file DCC.postman_collection.json --in-place

@ljohnston
Copy link

While that would work. I think resolving the problem of collection items in the same folder not being allowed to have the same name would be more ideal and solve this issue across the board.

Amen! Name and Method should define uniqueness for a request.

@ljohnston
Copy link

Shouldn't this be a high priority? Basically Postman imports are broken due to this issue.

@cplaetzinger
Copy link

cplaetzinger commented Dec 11, 2023

In the meantime I'm treating my Postman collections with this script before importing - prefixing all request names with HTTP method.

Many thanks for sharing. Saved me a some work and I was able to import a large postman collection :-)

@toehser
Copy link

toehser commented Jun 26, 2024

Can't we just save what we are importing as we go and then append -2 -3 if we find more?
This is an especially irritating one. If I have "create auth" and x and y and z and "create auth" I end up with 1 "create auth" as the 4th request - 2 3 4 5 and no 1. Fine to rename the second "create auth" as "create auth bru-2" after all this would only affect IMPORT.

FWIW, when I import my Postman collection of 56 actions, I get 46 actions in Bruno!

@toehser
Copy link

toehser commented Jun 26, 2024

prefixing all request names with HTTP method.

Doesn't help for my case.

While that would work. I think resolving the problem of collection items in the same folder not being allowed to have the same name would be more ideal and solve this issue across the board.

Can't we just do a quick fix only to postman import? Changing the structure to not allow duplicate names, um, why not just have "seq"-name after all? Are duplicate seq allowed? "seq"-name would also be NICE for collating in a file explorer... ijs, you have this file-system-simplicity concept, which I like, but, seq is more meaningful than arbitrary alpha or date sort. Again, just fix the POSTMAN IMPORT to do seq-name or seq_name or name-1 name-2 or name name-2 FIRST, has to be more easy and isolated... Using UID for filename is a BAD IDEA.

Amen! Name and Method should define uniqueness for a request.

No. I have a Postman collection with 67 entries. It is structured like this:

"auth" post
"thing 1" get
"thing 2" post
"thing 3" patch
"thing 2" post
"thing 4" post
"thing 5" get
"auth" post
... more stuff

While in concept "auth" could be reused with seq 1 and seq 8 both being "auth", "thing 1" has differences in the test script, etc... This is a postman import problem for LOTS of existing postman collections.

Importing the above gives:

seq 2 thing1
seq 4 thing3
seq 5 thing2
seq 6 thing4
seq 7 thing5
seq 8 auth

Notice it totally breaks because it never authorized. The existence of auth again is because some are slow and the token times out.

When I import my 56 entry collection, it comes in as 46 requests! 21 out of 67 are just "silently dropped".

It isn't OK to say "name and method should define uniqueness" when postman doesn't :)

My suggestion is still: For now, make POSTMAN imports do names as "seq###-name" and long term make ALL filenames seq###-name...

@toehser
Copy link

toehser commented Jun 26, 2024

python to fix the postman json file prepending sequence numbers:

import json
seq = 1
with open('collection.json', 'r') as fIn:
  collection = json.load(fIn)
for n in collection['item']:
    n['name'] = str(seq).zfill(4) + "-" + n['name']
    seq+=1
with open('fixed.json', 'w') as fOt:
  json.dump(collection, fOt)

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

No branches or pull requests

8 participants