postmanlabs / openapi-to-postman

Plugin for converting OpenAPI 3.0 specs to the Postman Collection (v2) format
Apache License 2.0
932 stars 201 forks source link

How to add or convert a Collection Pre-Request Script? #158

Open skplunkerin opened 4 years ago

skplunkerin commented 4 years ago

How can I include a Collection Pre-Request Script?

I have a Collection Pre-Request Script that will setup a header for all requests made by my collection, but I can't find how to add this script into OpenAPI format to be converted correctly? OR is there a way the package can insert this script instead of adding it in OpenAPI?

Here's the script I'm wanting to include after running openapi-to-postmanv2:

pm.request.headers.add({key: 'X-Project-Client', value: "{{X-Project-Client}}" })

When I manually add this to my collection and export it, it will appear like this in the export json (edited for brevity):

{
    "info": {
        "_postman_id": "12345",
        "name": "Collection Name",
        "description": "My collection description",
        "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
    },
    "item": [...]
    "event": [
        {
            "listen": "prerequest",
            "script": {
                "id": "12234",
                "type": "text/javascript",
                "exec": [
                    "pm.request.headers.add({key: 'X-Project-Client', value: \"{{X-Project-Client}}\" })"
                ]
            }
        },
        {
            "listen": "test",
            "script": {
                "id": "1234",
                "type": "text/javascript",
                "exec": [
                    ""
                ]
            }
        }
    ],
    "variable": [...],
}
abhijitkane commented 4 years ago

@skplunkerin Is there a similar workflow in the other tools that you use that supports something similar?

We could explore Postman vendor-extensions in OAS that allow you to specify a X-Postman-PreRequestScript in your OpenAPI document, that's included as a pre-request script in the generated collection.

skplunkerin commented 4 years ago

@abhijitkane I was actually thinking the same thing about using OAS vendor extensions for this, and other Postman specific things.

Right now I'm currently breaking OAS validation by adding a non-supported component (not related to PreRequest Scripts) that I need for Postman, but OAS doesn't recognize. It would be really nice to have multiple Postman-specific features that I can add directly to my OAS file using vendor extensions, so both OAS and Postman are happy.

kepikoi commented 3 years ago

I agree - it would be nice to automate certain post api collection generation steps via a custom oas3 definition

Right now it's pretty burdensome to reapply all custom Postman features to an oas3-generated collection on each release

thim81 commented 3 years ago

The openapi-to-postman package focusses on transforming a OpenAPI document to a Postman collection.

To make the transformation more dynamic, we have created a seperate package Portman that uses "openapi-to-postman" under the hood to transform OpenAPI document to a Postman collection, but while doing so, it offers options to generate automat tests, insert postman variables, prefill Params / Headers / Body values with environment variables, add pre-request scripts, ...

We initially created a PR (#225) on "openapi-to-postman", but it didnt feel right to mix functionality and that is why we created the Portman package, next to openapi-to-postman, leveraging openapi-to-postman but with automation, API testing & making Postman more dynamic in mind.

scottpopplewell commented 3 years ago

Wow, I just sat down to finish a PR for this feature. I'll be checking out portman, thanks @thim81!