Skip to content

Commit f65c7cf

Browse files
committed
update: coerce numbers to string, error on other non-string types for env values
1 parent c63fa18 commit f65c7cf

File tree

2 files changed

+76
-15
lines changed

2 files changed

+76
-15
lines changed

package/lib/compileFunctions.js

+18-7
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,10 @@ module.exports = {
4747
'nodejs8';
4848
funcTemplate.properties.timeout =
4949
_.get(funcObject, 'timeout') || _.get(this, 'serverless.service.provider.timeout') || '60s';
50-
funcTemplate.properties.environmentVariables = _.mapValues(
51-
_.merge(
52-
{},
53-
_.get(this, 'serverless.service.provider.environment'),
54-
funcObject.environment // eslint-disable-line comma-dangle
55-
),
56-
(value) => value.toString()
50+
funcTemplate.properties.environmentVariables = _.transform(
51+
_.merge({}, _.get(this, 'serverless.service.provider.environment'), funcObject.environment),
52+
(result, value, key) => coerceEnvOrError(result, key, value),
53+
{}
5754
);
5855
funcTemplate.accessControl.gcpIamPolicy.bindings = _.unionBy(
5956
_.get(funcObject, 'iam.bindings'),
@@ -201,6 +198,20 @@ const validateIamProperty = (funcObject, functionName) => {
201198
}
202199
};
203200

201+
const coerceEnvOrError = (result, key, value) => {
202+
if (typeof value === 'string') {
203+
result[key] = value;
204+
} else if (typeof value === 'number') {
205+
result[key] = value.toString();
206+
} else {
207+
const errorMessage = [
208+
`The value for environment variable ${key} is an unsupported type: ${typeof value}.`,
209+
' Values must either be strings or numbers (which are coerced into strings at package time).',
210+
].join('');
211+
throw new Error(errorMessage);
212+
}
213+
};
214+
204215
const getFunctionTemplate = (funcObject, projectName, region, sourceArchiveUrl) => {
205216
//eslint-disable-line
206217
return {

package/lib/compileFunctions.test.js

+58-8
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,64 @@ describe('CompileFunctions', () => {
370370
});
371371
});
372372

373+
it('should fail setting environment variable due to unsupported type (bool)', () => {
374+
googlePackage.serverless.service.functions = {
375+
func1: {
376+
handler: 'func1',
377+
environment: {
378+
TEST_VAR: true,
379+
},
380+
events: [{ http: 'foo' }],
381+
},
382+
};
383+
384+
expect(() => googlePackage.compileFunctions()).toThrow(Error);
385+
});
386+
387+
it('should fail setting environment variable due to unsupported type (null)', () => {
388+
googlePackage.serverless.service.functions = {
389+
func1: {
390+
handler: 'func1',
391+
environment: {
392+
TEST_VAR: null,
393+
},
394+
events: [{ http: 'foo' }],
395+
},
396+
};
397+
398+
expect(() => googlePackage.compileFunctions()).toThrow(Error);
399+
});
400+
401+
it('should fail setting environment variable due to unsupported type (object)', () => {
402+
googlePackage.serverless.service.functions = {
403+
func1: {
404+
handler: 'func1',
405+
environment: {
406+
dev: {
407+
TEST_VAR: 'test',
408+
},
409+
},
410+
events: [{ http: 'foo' }],
411+
},
412+
};
413+
414+
expect(() => googlePackage.compileFunctions()).toThrow(Error);
415+
});
416+
417+
it('should fail setting environment variable from provider due to unsupported type (bool)', () => {
418+
googlePackage.serverless.service.functions = {
419+
func1: {
420+
handler: 'func1',
421+
events: [{ http: 'foo' }],
422+
},
423+
};
424+
googlePackage.serverless.service.provider.environment = {
425+
TEST_VAR: true,
426+
};
427+
428+
expect(() => googlePackage.compileFunctions()).toThrow(Error);
429+
});
430+
373431
it('should set the environment variables based on the function configuration', () => {
374432
googlePackage.serverless.service.functions = {
375433
func1: {
@@ -378,7 +436,6 @@ describe('CompileFunctions', () => {
378436
TEST_VAR: 'test',
379437
INT_VAR: 1,
380438
FLOAT_VAR: 3.141,
381-
BOOL_VAR: true,
382439
},
383440
events: [{ http: 'foo' }],
384441
},
@@ -398,7 +455,6 @@ describe('CompileFunctions', () => {
398455
TEST_VAR: 'test',
399456
INT_VAR: '1',
400457
FLOAT_VAR: '3.141',
401-
BOOL_VAR: 'true',
402458
},
403459
timeout: '60s',
404460
sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip',
@@ -429,7 +485,6 @@ describe('CompileFunctions', () => {
429485
TEST_VAR: 'test',
430486
INT_VAR: 1,
431487
FLOAT_VAR: 3.141,
432-
BOOL_VAR: true,
433488
};
434489

435490
const compiledResources = [
@@ -446,7 +501,6 @@ describe('CompileFunctions', () => {
446501
TEST_VAR: 'test',
447502
INT_VAR: '1',
448503
FLOAT_VAR: '3.141',
449-
BOOL_VAR: 'true',
450504
},
451505
timeout: '60s',
452506
sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip',
@@ -473,15 +527,13 @@ describe('CompileFunctions', () => {
473527
environment: {
474528
TEST_VAR: 'test_var',
475529
TEST_VALUE: 'foobar',
476-
TEST_BOOL: true,
477530
},
478531
events: [{ http: 'foo' }],
479532
},
480533
};
481534
googlePackage.serverless.service.provider.environment = {
482535
TEST_VAR: 'test',
483536
TEST_FOO: 'foo',
484-
TEST_BOOL: false,
485537
};
486538

487539
const compiledResources = [
@@ -498,7 +550,6 @@ describe('CompileFunctions', () => {
498550
TEST_VAR: 'test_var',
499551
TEST_VALUE: 'foobar',
500552
TEST_FOO: 'foo',
501-
TEST_BOOL: 'true',
502553
},
503554
timeout: '60s',
504555
sourceArchiveUrl: 'gs://sls-my-service-dev-12345678/some-path/artifact.zip',
@@ -518,7 +569,6 @@ describe('CompileFunctions', () => {
518569
expect(googlePackage.serverless.service.provider.environment).toEqual({
519570
TEST_VAR: 'test',
520571
TEST_FOO: 'foo',
521-
TEST_BOOL: false,
522572
});
523573
});
524574
});

0 commit comments

Comments
 (0)