diff --git a/lib/executor.js b/lib/executor.js index 428d3de..a3f13ec 100644 --- a/lib/executor.js +++ b/lib/executor.js @@ -322,7 +322,6 @@ function setupMiddleware(app, instructions) { } function getUpdatedConfigObject(app, config) { - var DYNAMIC_CONFIG_PARAM = /\$\{(\w+)\}$/; function getConfigVariable(param) { @@ -341,11 +340,24 @@ function getUpdatedConfigObject(app, config) { } function interpolateVariables(config) { + // config is a string and contains a config variable ('${var}') + if (typeof config === 'string') + return getConfigVariable(config); + + // anything but an array or object + if (typeof config !== 'object' || config == null) + return config; + + // recurse into array elements + if (Array.isArray(config)) + return config.map(interpolateVariables); + + // recurse into object props var interpolated = {}; Object.keys(config).forEach(function(configKey) { var value = config[configKey]; if (Array.isArray(value)) { - interpolated[configKey] = value.map(getConfigVariable); + interpolated[configKey] = value.map(interpolateVariables); } else if (typeof value === 'string') { interpolated[configKey] = getConfigVariable(value); } else if (typeof value === 'object' && Object.keys(value).length) { diff --git a/test/executor.test.js b/test/executor.test.js index 5ecb977..f826a1f 100644 --- a/test/executor.test.js +++ b/test/executor.test.js @@ -1,3 +1,4 @@ +var async = require('async'); var boot = require('../'); var path = require('path'); var loopback = require('loopback'); @@ -426,7 +427,6 @@ describe('executor', function() { }); describe('with middleware.json', function() { - it('should parse a simple config variable', function(done) { boot.execute(app, simpleMiddlewareConfig('routes', { path: '${restApiRoot}' } @@ -494,6 +494,37 @@ describe('executor', function() { }); }); + it('should not parse invalid config variables', function(done) { + var invalidDataTypes = [undefined, function() {}]; + async.each(invalidDataTypes, function(invalidDataType, cb) { + var config = simpleMiddlewareConfig('routes', { + path: invalidDataType + }); + boot.execute(app, config); + + supertest(app) + .get('/') + .end(function(err, res) { + expect(err).to.be.null(); + expect(res.body.path).to.be.undefined(); + cb(); + }); + }, done); + }); + + it('should parse valid config variables', function(done) { + var config = simpleMiddlewareConfig('routes', { + props: ['a', '${vVar}', 1, true, function() {}, {x:1, y: '${y}'}] + }); + boot.execute(app, config); + + supertest(app) + .get('/') + .end(function(err, res) { + expect(err).to.be.null(); + done(); + }); + }); }); describe('with component-config.json', function() {