postmanlabs / openapi-to-postman

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

ConfigFile options are overwritten by CLI options #315

Closed thim81 closed 3 years ago

thim81 commented 3 years ago

The options from the ConfigFile are getting overwritten by the DefinedOptions, which should contain the CLI -O values but they contain all the CLI options and not the property

openapi2postmanv2 -s openapi.yaml -o postman.json -p -c postman/openapi-to-postman-config.json

openapi2postmanv2.js - Line 122

function convert(swaggerData) {
  let options = {};

  console.log('options', options)

// apply options from config file if present
  if (configFile) {
    configFile = path.resolve(configFile);
    console.log('Options Config file: ', configFile); // eslint-disable-line no-console
    options = JSON.parse(fs.readFileSync(configFile, 'utf8'));
  }

  console.log('options configFile', options)

  // override options provided via cli
  if (definedOptions) {
    options = definedOptions;
  }

  console.log('options configFile', options)

OUTPUT

options {}
Options Config file:  /Users/tim/Sites/DevPortal/api-smc/postman/openapi-to-postman-config.json
options configFile {
  requestNameSource: 'Fallback',
  indentCharacter: 'Space',
  collapseFolders: true,
  requestParametersResolution: 'Example',
  exampleParametersResolution: 'Example',
  folderStrategy: 'Tags',
  schemaFaker: true,
  includeAuthInfoInExample: true,
  shortValidationErrors: false,
  validationPropertiesToIgnore: [],
  showMissingInSchemaErrors: true,
  detailedBlobValidation: false
}
options definedOptions [
  Option {
    flags: '-v, --version',
    required: false,
    optional: false,
    bool: true,
    short: '-v',
    long: '--version',
    description: 'output the version number'
  },
  Option {
    flags: '-s, --spec <spec>',
    required: true,
    optional: false,
    bool: true,
    short: '-s',
    long: '--spec',
    description: 'Convert given OPENAPI 3.0.0 spec to Postman Collection v2.0'
  },
  Option {
    flags: '-o, --output <output>',
    required: true,
    optional: false,
    bool: true,
    short: '-o',
    long: '--output',
    description: 'Write the collection to an output file'
  },
  Option {
    flags: '-t, --test',
    required: false,
    optional: false,
    bool: true,
    short: '-t',
    long: '--test',
    description: 'Test the OPENAPI converter'
  },
  Option {
    flags: '-p, --pretty',
    required: false,
    optional: false,
    bool: true,
    short: '-p',
    long: '--pretty',
    description: 'Pretty print the JSON file'
  },
  Option {
    flags: '-c, --options-config <optionsConfig>',
    required: true,
    optional: false,
    bool: true,
    short: '-c',
    long: '--options-config',
    description: 'JSON file containing Converter options'
  },
  Option {
    flags: '-O, --options <options>',
    required: true,
    optional: false,
    bool: true,
    short: '-O',
    long: '--options',
    description: 'comma separated list of options'
  },
  Option {
    flags: '-g, --generate <generate>',
    required: true,
    optional: false,
    bool: true,
    short: '-g',
    long: '--generate',
    description: 'Generate postman tests given the JSON file with test options'
  }
]

If you use

openapi2postmanv2 -s openapi.yaml -o postman.json -p -c postman/openapi-to-postman-config.json -O folderStrategy=Tags

Then the OUTPUT is correct

options {}
Options Config file:  /Users/tim/Sites/DevPortal/api-smc/postman/openapi-to-postman-config.json
options configFile {
  requestNameSource: 'Fallback',
  indentCharacter: 'Space',
  collapseFolders: true,
  requestParametersResolution: 'Example',
  exampleParametersResolution: 'Example',
  folderStrategy: 'Tags',
  schemaFaker: true,
  includeAuthInfoInExample: true,
  shortValidationErrors: false,
  validationPropertiesToIgnore: [],
  showMissingInSchemaErrors: true,
  detailedBlobValidation: false
}
options definedOptions { folderStrategy: 'Tags' }
thim81 commented 3 years ago

Proposal to solve this:

openapi2postmanv2.js - Line 55

.option('-O, --options <options>', 'comma separated list of options', parseOptions)

Replace with

.option('-O, --options-cli <optionsCli>', 'comma separated list of options', parseOptions)

openapi2postmanv2.js Line 82

definedOptions = program.options || {};

Replace with

definedOptions = program.optionsCli || {};

VShingala commented 3 years ago

Thanks for raising issue and PR as well 😄. I have reviewed the PR and it looks good to me apart from the change suggested. Let me know when you address the change. I will approve and merge the PR.

thim81 commented 3 years ago

@VShingala I'll look into the suggested change of correct if possible

thim81 commented 3 years ago

PR is updated and should be ready to go

thim81 commented 3 years ago

Resolved as part of the 2.1.0 release