Skip to content

Commit 9f164bc

Browse files
Merge pull request #23 from balena-io-modules/coerce-null-env-vars
Coerce env var values to empty string
2 parents cdff612 + 1aad1a2 commit 9f164bc

3 files changed

Lines changed: 49 additions & 0 deletions

File tree

lib/compose.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,18 @@ function normalizeService(
417417
// Delete env_file, as compose-go adds env_file vars to service.environment
418418
delete service.env_file;
419419

420+
// Coerce null/undefined env var values to empty string.
421+
// `KEY: null` is valid in the Compose spec, but the API
422+
// rejects null values for env vars, so we coerce to '' to match
423+
// @balena/compose parser behavior prior to integrating compose-parser.
424+
if (service.environment) {
425+
for (const [key, value] of Object.entries(service.environment)) {
426+
if (value == null) {
427+
service.environment[key] = '';
428+
}
429+
}
430+
}
431+
420432
// Delete label_file, as compose-go adds label_file labels to service.labels
421433
delete service.label_file;
422434

test/compose.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,34 @@ describe('compose-go parsing & validation', () => {
458458
});
459459
});
460460

461+
it('should coerce null/undefined environment values to empty string', async () => {
462+
const composition = await parse(
463+
'test/fixtures/compose/services/null_env.yml',
464+
);
465+
expect(composition).to.deep.equal({
466+
services: {
467+
main: {
468+
image: 'alpine:latest',
469+
command: ['sh', '-c', 'sleep infinity'],
470+
environment: {
471+
EXPLICIT_VALUE: 'hello',
472+
EXPLICIT_EMPTY: '',
473+
NULL_DICT_FORM: '',
474+
UNDEFINED_FORM: '',
475+
},
476+
networks: {
477+
default: null,
478+
},
479+
},
480+
},
481+
networks: {
482+
default: {
483+
ipam: {},
484+
},
485+
},
486+
});
487+
});
488+
461489
it('should merge services from extends config', async () => {
462490
const composition = await parse(
463491
'test/fixtures/compose/services/extends.yml',
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
services:
2+
main:
3+
image: alpine:latest
4+
command: sh -c "sleep infinity"
5+
environment:
6+
EXPLICIT_VALUE: hello
7+
EXPLICIT_EMPTY: ""
8+
NULL_DICT_FORM: null
9+
UNDEFINED_FORM:

0 commit comments

Comments
 (0)