apigee-127 / swagger-node-runner

The heart of Swagger-Node
MIT License
102 stars 123 forks source link

Global config replacement (x-a127-config style) #84

Open jcferrer opened 7 years ago

jcferrer commented 7 years ago

Scott,

As I am going through migrating from the a127 to the swagger-node new framework, I ran into the issue of trying to use the global replacement capability using the x-a127-config section in the yaml file.

We rely on this capability to be able to set different options per environment specifically for the volos libraries.

It seems that this was left out by design, assuming the app could provide the swagger instance via the config object. This is fine, but it would require the app to do something like the doConfigReplacements function in the a127-magic module.

Is there any way that a global yaml config section can be added to provide this kind of functionality or do you know of any other way that this can be achieved?

Thanks in advance for the help.

JC

theganyo commented 7 years ago

Yes, it was left out as swagger-node doesn't have the capability to set the environment like a127 did.

I haven't looked at this, but you could possibly load the config module and do some setup prior to initializing the swagger-node-runner module. The config module allows cascading configuration, so optimally you'd be able to use that.

Obviously, though, a more flexible configuration system would be nice... I'm open for ideas.

jcferrer commented 7 years ago

That's what I had to do. Using the a127-magic loader.js module to do the config replacements and passing the config.swagger object to the runner. For sure this is a viable workaround. However it defeats the purpose of the node-runner already resolving the config and the yaml file.

My suggestion to you is to support a directive in the config object to allow for the doConfigReplacements (or similar function) to kick in. A directive like (please forgive me but I am terrible with property names :)) config.global = { enable: true, section: 'x-config' }, possibly having the section to default to 'x-a127-config'.

Again, this would require to add the a127-magic loader.js to the runner and call it around these lines (168) in the swagger-node-runner index.js:

  if (_.isObject(appJsConfig.swagger)) { // allow direct setting of swagger
    this.swagger = appJsConfig.swagger;
  } else {
    try {

      // add check for check appJsConfig.global settings and call loader.load

      var swaggerFile = appJsConfig.swaggerFile || this.resolveAppPath(appPaths.swaggerFile);
      var swaggerString = fs.readFileSync(swaggerFile, 'utf8');
      this.swagger = yaml.safeLoad(swaggerString);
    } catch (err) {
      return cb(err);
    }
  }

Thanks