Update postman collection from swagger specification file

Viewed 9869

I'm importing a swagger specification file into postman to create a collection, at this point, works as expected and the collection is generated with all requests & sub-folders, fine!!. But when the api is updated, I need update the postman to update all requests based on the new specification. I can't find a action like "update" or something else. I'm trying import the new specification into postman and he say:

A collection APIName already exists. What would you like to do?

Replace or Import as copy

a copy its a not feasible option, then I use replace and the existent collection is updated, but all tests, parameters, pre-req scripts are remove and I need reconfigure all again.

I'm missing something, exist a way to import & update a existent collection from a specification file, without losing existent tests & configuration?

thanks in advance!

4 Answers

Postman does not support this as of now. Link

Alternative I learned from this blog. In short:

  1. Update your OpenAPI YAML/JSON files.
  2. Import to Postman as a new collection.
  3. Export the new collection from Postman. As JSON in Collection v2.1 format (recommended).
  4. Using Postman API (Update Collection), update the existing collection with the JSON in step 3 as body. Make sure to update collection_uid accordingly.

Postman update collection API body sample:

{
  "collection": 
  <------- YOUR EXPORTED COLLECTION HERE --------->
}

There is no straightforward solution. There also probably won't be one for quite some time - the devs said it is hard to correlate old Postman requests in a collection with new requests generated from incoming Swagger file (or any other source of updates, for that matter).

You can do it outside of Postman, though. Postman collections are really just JSON data and can be manipulated as such.

  1. Transform your Swagger file to a Postman collection file. (You can just import it and export it again, or use tools like Swagger2Postman if you want to automate. Save it as collection 2.0 or newer, that format makes step 3 a lot easier.)
  2. Export your old collection in the same version
  3. Merge the two JSONs in your preferred scripting language
  4. Import the merged JSON back to Postman

I made myself a simple helper for step 3. It is fine if you are just adding more stuff to the collection (my case), but can't remove anything. If anyone has a more versatile solution, please, post it - I would gladly use it instead.

function execute() {
  collection = JSON.parse($(".collection").val());
  swagger = JSON.parse($(".swagger").val());
  result = JSON.stringify($.extend(true, {}, swagger, collection));
  $(".result").val(result);
}
<html><body>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <br>Collection: <br> <textarea class="collection"></textarea>
  <br>Swagger: <br> <textarea class="swagger"></textarea>
  <br>Result: <br> <textarea class="result"></textarea>
  <br>
  <button onClick="execute()">EXECUTE</button>
</body></html>

Related