Azure / Azure/bicep

`loadYamlContent(..)` converts unset properties (should be: `null`) to empty strings (`''`)

Open
#14,143 5 comments 0 reactions 0 assignees View on GitHub
Needs: Upvote
Dominant language
Bicep
Stars
3.6k
Forks
830
Avg merge
1d 2h
Merged PRs (30d)
79

Description

**Bicep version**
`Bicep CLI version 0.27.1 (4b41cb6d4b)`

**Describe the bug**
While using a combination of `loadYamlContent()` in conjunction with `union()` and `string()` to construct an OpenTelemetry Collector configuration file (don't ask...!) I stumbled upon a rather interesting bug:

*yamlTest.bicep:*
```bicep
targetScope = 'subscription'

// expected: `{"toplevel":{"nested1":null,"nested2":null,"nested3":""}}`
// got: `{"toplevel":{"nested1":"","nested2":null,"nested3":""}}`
var yamlAsString = string(loadYamlContent('./example.yaml'))
```

*example.yaml:*
```yaml
toplevel:
nested1:
nested2: null
nested3: ''
```

**To Reproduce**
Steps to reproduce the behavior:
Create the two files as described above and use bicep to build the ARM template, e.g. by using the following shell script:

```sh
cat < ./example.yaml
toplevel:
nested1:
nested2: null
nested3: ''
EOF

cat < ./yamlTest.bicep
targetScope = 'subscription'

// expected: '{"toplevel":{"nested1":null,"nested2":null,"nested3":""}}'
// got: '{"toplevel":{"nested1":"","nested2":null,"nested3":""}}'
#disable-next-line no-unused-vars
var yamlAsString = string(loadYamlContent('./example.yaml'))
EOF

az bicep build --file ./yamlTest.bicep --outfile ./yamlTest.out.json
jq -r '.variables' ./yamlTest.out.json

# Now: just compare the contents of the variable with the given YAML input
```

**Additional context**
Mitigation in my case was to explicitly set the properties to `null`, but it was kind of counter-intuitive and took me some time to figure out that Bicep's `loadYamlContent()` was doing something unexpected.

Contributor guide

Open the contributing guide

Research direction

Reproduce the behavior with example.yaml and yamlTest.bicep using the provided az bicep build command, then inspect how loadYamlContent() handles unset YAML properties. Compare the generated variable with the expected JSON; done means an unset property remains null rather than becoming an empty string.

Written by the indexing model from the issue text.

Assessment

Tech stack
shell, yaml
Domain
cli, compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.