Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

openapi-to-postman-core

postmanlabs8Apache-2.00.0.2

Convert a given OpenAPI specification to Postman Collection v2.0

openapi, postman, api, schema, swagger, oas

readme

postman icon

Supercharge your API workflow.
Modern software is built on APIs. Postman helps you develop APIs faster.

OpenAPI 3.0 to Postman Collection v2.1.0 Converter

Build Status

Contents

  1. Getting Started
  2. Using the converter as a NodeJS module
    1. Convert Function
    2. Options
    3. ConversionResult
    4. Sample usage
    5. Validate function
  3. Command Line Interface
    1. Options
    2. Usage
  4. Conversion Schema

Getting Started

To use the converter as a Node module, you need to have a copy of the NodeJS runtime. The easiest way to do this is through npm. If you have NodeJS installed you have npm installed as well.

$ npm install openapi-to-postmanv2

Using the converter as a NodeJS module

In order to use the convert in your node application, you need to import the package using require.

var Converter = require('openapi-to-postmanv2')

The converter provides the following functions:

Convert

The convert function takes in your OpenAPI specification ( YAML / JSON ) and converts it to a Postman collection.

Signature: convert (data, options, callback);

data:

{ type: 'file', data: 'filepath' }
OR
{ type: 'string', data: '<entire OpenAPI string - JSON or YAML>' }
OR
{ type: 'json', data: OpenAPI-JS-object }

options:

{
  schemaFaker: true,
  requestNameSource: 'fallback',
  indentCharacter: ' '
}
/*
All three properties are optional. Check the options section below for possible values for each option.
*/

callback:

function (err, result) {
  /*
  result = {
    result: true,
    output: [
      {
        type: 'collection',
        data: {..collection object..}
      }
    ]
  }
  */
}

Options:

  • 'schemaFaker'(boolean): whether to use json-schema-faker for schema conversion. Default: true
  • 'requestNameSource'(string): The strategy to use to generate request names. url: use the request's URL as the name, fallback: Use the summary/operationId/URL (in that order) Default: fallback
  • 'indentCharacter' (string): The character to use per level of indentation for JSON/XML data. Default: ' '(space)

ConversionResult

  • result - Flag responsible for providing a status whether the conversion was successful or not

  • reason - Provides the reason for an unsuccessful conversion, defined only if result: false

  • output - Contains an array of Postman objects, each one with a type and data. The only type currently supported is collection.

Sample Usage:

var fs = require('fs'),

Converter = require('openapi-to-postmanv2'),
openapiData = fs.readFileSync('sample-spec.yaml', {encoding: 'UTF8'});

Converter.convert({ type: 'string', data: openapiData },
  {}, (err, conversionResult) => {
    if (!conversionResult.result) {
      console.log('Could not convert', conversionResult.reason);
    }
    else {
      console.log('The collection object is: ', conversionResult.output[0].data);
    }
  }
);

Validate Function

The validate function is meant to ensure that the data that is being passed to the convert function is a valid JSON object or a valid (YAML/JSON) string.

The validate function is synchronous and returns a status object which conforms to the following schema

Validation object schema

{
  type: 'object',
  properties: {
    result: { type: 'boolean'},
    reason: { type: 'string' }
  },
  required: ['result']
}
Validation object explanation
  • result - true if the data looks like OpenAPI and can be passed to the convert function

  • reason - Provides a reason for an unsuccessful validation of the specification

Command Line Interface

The converter can be used as a CLI tool as well. The following command line options are available.

openapi2postmanv2 [options]

Options

  • -V, --version
    Specifies the version of the converter

  • -s <source>, --spec <source>
    Used to specify the OpenAPI specification (file path) which is to be converted

  • -o <destination>, --output <destination>
    Used to specify the destination file in which the collection is to be written

  • -t, --test
    Used to test the collection with an in-built sample specification

  • -p, --pretty
    Used to pretty print the collection object while writing to a file

  • -h, --help
    Specifies all the options along with a few usage examples on the terminal

Usage

Sample usage examples of the converter CLI

  • Takes a specification (spec.yaml) as an input and writes to a file (collection.json) with pretty printing

    $ openapi2postmanv2 -s spec.yaml -o collection.json -p
  • Testing the converter

    $ openapi2postmanv2 --test

Conversion Schema

postman openapi options examples
collectionName info.title -
description info.description + info.contact -
collectionVariables server.variables + pathVariables -
folderName paths.path -
requestName operationItem(method).operationId default(operationId)-(requestName)enum['operationId','summary','url']
request.method path.method -
request.headers parameter (in = header) - link
request.body operationItem(method).requestBody -
request.url.raw server.url (path level server >> openapi server) + path -
request.url.variables parameter (in = path) - link
request.url.params parameter (in = query) - {"key": param.name, "value": link}
api_key in (query or header) components.securitySchemes.api_key -

changelog

OpenAPI-Postman Changelog

v1.2.1 (June 18, 2020)

  • Added support for validating content-type header.

v1.2.0 (June 14, 2020)

  • Added support for stricter request matching via option for validation.
  • Added missing endpoints from collection info in result of validation.
  • Suggest fixes in collection for violated properties in validation.
  • Introduced option to validate metadata for validation.
  • Use faked value instead of fallback to schema for parameter resolution when set to example.
  • Use faked value instead of invalid schema defined example.
  • Introduced option to ignore unresolved postman variable mismatches.
  • Fixed invalid generated collection for body type formdata.

v1.1.19 (June 12, 2020)

  • Fix for #232 - Changes default auth of requests to null conforming to the JSON schema.

v1.1.18 (June 1, 2020)

  • Fix for #86 - Now global security schemes will be added at the collection level.

v1.1.17 (May 15, 2020)

  • Fix for #3 #57 - Introduced a new option folderStrategy, can choose between Tags or Path while creating folders in Postman Collection.
  • Fixed an issue where undefined was returned as error message while trying to import invalid format.
  • Use minItems and maxItems values if available in schema with type array.

v1.1.16 (May 4, 2020)

  • Change Url to URL in Naming Request option.

v1.1.15 (April 29, 2020)

  • Add missing yaml dependency.

v1.1.14 (April 28, 2020)

  • Added a new function to get meta data from a schema.
  • Fix for #169 - Removed unused folderStrategy option.

v1.1.13 (April 21, 2020)

  • Added support for detailed validation body mismatches with option detailedBlobValidation.
  • Fix for #8098 - Unable to validate schema with type array.
  • Fixed URIError for invalid URI in transaction.
  • Fix for #152 - Path references not resolved due to improver handling of special characters.
  • Fix for #160 - Added handling for variables in local servers not a part of a URL segment. All path servers to be added as collection variables.
  • Unresolved refs will not be stored in schemaResolutionCache.

v1.1.12 (Mar 26, 2020)

v1.1.11 (Mar 14, 2020)

  • Safely handling invalid reference schemas/properties

v1.1.10 (Mar 4, 2020)

  • Safely handling malformed URIs and schemas

v1.1.9 (Feb 17, 2020)

  • Support for multi-file schemas. Added a new input method 'folder'.

v1.1.8 (Feb 15, 2020)

v1.1.7 (Feb 05, 2020)

v1.1.6 (Jan 13, 2020)

  • Not throwing errors for missing schema path entries

v1.1.5 (Jan 10, 2020)

  • Not throwing errors for invalid schema references
  • Not throwing errors for invalid schema.path entries
  • Not throwing errors for unknown schema types

v1.1.4 (Jan 07, 2020)

  • More updated copy for mismatch reasons
  • Added missing dependency for async.js

v1.1.2/v1.1.3 (Jan 03, 2020)

  • Updated copy for mismatch reasons
  • Returning errors instead of exceptions for invalid schemas
  • Fix for duplicate mismatches

v1.1.1 (Jan 03, 2020)

  • Correct validation against JSON request/response bodies
  • Correct JSON paths in response mismatches

v1.1.0 (Jan 02, 2020)

  • Handling cases where API definition parameters have no schema
  • Validating input transaction schema before starting validation
  • Forcing path variable descriptions to be strings instead of objects

v1.0.2 (Jan 01, 2020)

  • Exposing option to hide MISSING_IN_SCHEMA mismatches, hiding them by default
  • Consistent response formats, more resilient against invalid schemas

v1.0.1 (Jan 01, 2020)

  • Deleting 'info.version' from generated collection JSON - it's not required and was causing versioning problems
  • Scope-related bugfixes in schema validation flows

v1.0.0 (Dec 31, 2019)

v0.2.0 (Nov 22, 2019)

  • Handled cases where the URL query has no description property
  • Fix for https://github.com/postmanlabs/openapi-to-postman/issues/117 - Maintaining descriptions for form-urlencoded body params
  • Added various options for converting OpenAPI specs into Postman Collections, including controlling how examples are generated
  • Request parameters now default to a schema-based value generation, response parameters default to example-based value generation.

v0.1.0 (Oct 1, 2019)

v0.0.17/v0.0.18 (Sep 3, 2019)

v0.0.16 (July 22, 2019)

v0.0.14 / v0.0.15 (June 5, 2019)

v0.0.13 (May 29, 2019)

v0.0.12 (Apr 17, 2019)

v0.0.11 (Apr 17, 2019)

v0.0.10 (Jan 31, 2019)

  • Safe property access to empty content/authHelper objects
  • Setting postman_previewLanguage while setting responses
  • Not overriding non-string variable types during schema faking
  • Not doubly-stringifying string headers

v0.0.9 (December 17, 2018)

v0.0.8 (December 12, 2018)

  • Refactoring, restucturing tests
  • Adding support for xml chemas
  • Enabling travis CI
  • Updating README, adding a license, moving to Github

v0.0.7 (December 7, 2018)

  • Converting all console.error to console.warn

v0.0.6 (December 5, 2018)

  • Handling schema.enum when no other schema is specified
  • Resolving parameter schemas while creating requests
  • Correctly setting path variable descriptions
  • Using a browserified json-schema-faker

v0.0.5 (November 30, 2018)

  • Populating the original request in generated example responses
  • Infer the response content-type header from the response body
  • Generating more human-readable folder/request names from snake_case/camelCase

v0.0.4 (November 29, 2018)

  • Handling nested schemas, correct handling for oneOf/anyOf

v0.0.3 (November 29, 2018)

  • Prefer examples to schemas while generating example response body
  • Correct handling for scheme variables in the URL
  • Ignoring schema errors for invalid references
  • Blocking schema nesting of >20 levels
  • Correctly handling empty security sets for requests
  • Removing the insecure node-uuid dependency

v0.0.2 (November 19, 2018)

  • Adding default URLs if "server" is absent
  • Better indication of lack-of-support for allOf schemas

v0.0.1 (October 23, 2018)

  • Base release