Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ The validate function is synchronous and returns a status object which conforms

## Command Line Interface

The converter can be used as a CLI tool as well. The following [command line options](#options) are available.
The converter can be used as a CLI tool as well. The following [command line options](#options) are available. See [this page](./cli-config.md) for options supported using the config file.

`openapi2postmanv2 [options]`

Expand All @@ -169,6 +169,9 @@ The converter can be used as a CLI tool as well. The following [command line opt
- `-p`, `--pretty`
Used to pretty print the collection object while writing to a file

- `-c`, `--config`
Used to supply options to the converter

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

Expand Down
45 changes: 37 additions & 8 deletions bin/openapi2postmanv2.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@ var program = require('commander'),
inputFile,
outputFile,
prettyPrintFlag,
configFile,
testFlag,
sourceMapFile,
swaggerInput,
swaggerData;
swaggerData,
sourceMapData;

program
.version(require('../package.json').version, '-v, --version')
.option('-s, --spec <spec>', 'Convert given OPENAPI 3.0.0 spec to Postman Collection v2.0')
.option('-o, --output <output>', 'Write the collection to an output file')
.option('-m, --sourceMap <source-map>', 'Source map to use for operation to request mapping')
.option('-t, --test', 'Test the OPENAPI converter')
.option('-p, --pretty', 'Pretty print the JSON file');
.option('-p, --pretty', 'Pretty print the JSON file')
.option('-c, --config <config>', 'JSON file containing Converter options');


program.on('--help', function() {
Expand All @@ -41,6 +46,8 @@ inputFile = program.spec;
outputFile = program.output || false;
testFlag = program.test || false;
prettyPrintFlag = program.pretty || false;
configFile = program.config || false;
sourceMapFile = program.sourceMap;
swaggerInput;
swaggerData;

Expand All @@ -52,7 +59,7 @@ swaggerData;
* @param {Object} collection - POSTMAN collection object
* @returns {void}
*/
function writetoFile(prettyPrintFlag, file, collection) {
function writetoFile(prettyPrintFlag, file, collection, sourceMapFile, sourceMap) {
if (prettyPrintFlag) {
fs.writeFile(file, JSON.stringify(collection, null, 4), (err) => {
if (err) { console.log('Could not write to file', err); }
Expand All @@ -65,18 +72,33 @@ function writetoFile(prettyPrintFlag, file, collection) {
console.log('Conversion successful', 'Collection written to file');
});
}

if (sourceMapFile) {
fs.writeFile(sourceMapFile, JSON.stringify(sourceMap), (err) => {
if (err) { console.log('Could not write to source map file', err); }
console.log('Source Map written to file');
});
}
}

/**
* Helper function for the CLI to convert swagger data input
* @param {String} swaggerData - swagger data used for conversion input
* @returns {void}
*/
function convert(swaggerData) {
function convert(swaggerData, sourceMapData) {
let options = {};
if (configFile) {
configFile = path.resolve(configFile);
console.log("Config file: ", configFile);
options = JSON.parse(fs.readFileSync(configFile, "utf8"));
}

Converter.convert({
type: 'string',
data: swaggerData
}, {}, (err, status) => {
data: swaggerData,
sourceMap: sourceMapData ? JSON.parse(sourceMapData) : ''
}, options, (err, status) => {
if (err) {
return console.error(err);
}
Expand All @@ -87,7 +109,7 @@ function convert(swaggerData) {
else if (outputFile) {
let file = path.resolve(outputFile);
console.log('Writing to file: ', prettyPrintFlag, file, status); // eslint-disable-line no-console
writetoFile(prettyPrintFlag, file, status.output[0].data);
writetoFile(prettyPrintFlag, file, status.output[0].data, sourceMapFile, status.sourceMap);
}
else {
console.log(status.output[0].data); // eslint-disable-line no-console
Expand All @@ -107,7 +129,14 @@ else if (inputFile) {
// this will fix https://github.com/postmanlabs/openapi-to-postman/issues/4
// inputFile should be read from the cwd, not the path of the executable
swaggerData = fs.readFileSync(inputFile, 'utf8');
convert(swaggerData);
if (sourceMapFile) {
try {
sourceMapData = fs.readFileSync(sourceMapFile, 'utf8');
} catch (_e) {
sourceMapData = '{}';
}
}
convert(swaggerData, sourceMapData);
}
else {
program.emit('--help');
Expand Down
17 changes: 17 additions & 0 deletions cli-config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## CLI Configuration options

These options are supported using the config file.

| name | type | available options | description |
| ---------------------------- | ------- | ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| requestNameSource | enum | **Fallback**, URL | Determines how the requests inside the generated collection will be named. If “Fallback” is selected, the request will be named after one of the following schema values: `description`, `operationid`, `url`. |
| indentCharacter | enum | **Space**, Tab | Option for setting indentation character |
| collapseFolders | boolean | **true**, false | Importing will collapse all folders that have only one child element and lack persistent folder-level data. |
| requestParametersResolution | enum | **Schema**, Example | Select whether to generate the request parameters based on the [schema](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#schemaObject) or the [example](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#exampleObject) in the schema. |
| exampleParametersResolution | enum | Schema, **Example** | Select whether to generate the response parameters based on the [schema](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#schemaObject) or the [example](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#exampleObject) in the schema. |
| folderStrategy | enum | **Paths**, Tags | Select whether to create folders according to the spec’s paths or tags. |
| schemaFaker | boolean | **true**, false | Whether or not schemas should be faked. |
| shortValidationErrors | boolean | true, **false** | Whether detailed error messages are required for request <> schema validation operations. |
| validationPropertiesToIgnore | array | | Specific properties (parts of a request/response pair) to ignore during validation. Must be sent as an array of strings. Valid inputs in the array: PATHVARIABLE, QUERYPARAM, HEADER, BODY, RESPONSE_HEADER, RESPONSE_BODY |
| showMissingInSchemaErrors | boolean | true, **false** | MISSING_IN_SCHEMA indicates that an extra parameter was included in the request. For most use cases, this need not be considered an error. |
| detailedBlobValidation | boolean | true, **false** | Determines whether to show detailed mismatch information for application/json content in the request/response body. |
13 changes: 13 additions & 0 deletions examples/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"requestNameSource": "Fallback",
"indentCharacter": "Space",
"collapseFolders": true,
"requestParametersResolution": "Schema",
"exampleParametersResolution": "Example",
"folderStrategy": "Paths",
"schemaFaker": true,
"shortValidationErrors": false,
"validationPropertiesToIgnore": [],
"showMissingInSchemaErrors": true,
"detailedBlobValidation": false
}
Loading