Using a nullable string output from a module as input for another resource gives an error
Bicep version 0.24.24
Describe the bug
When I use a string? output that has the value null as the input for another resource it gives me an error.
To Reproduce
-
Create a module which has a
string?output that actually outputs anullvalue (in my caseintegrateVnet == false). -
Use the output as input for a new resource.
-
I get an obscure error about a property
valuethat doesn't exist, although my property is calledappServicesSubnetId. I assume the message is about thevalueproperty of the nullable type (like in C#)?
Additional context Now I use the following workaround using an empty string which does work.
-
The module
-
The resource using the module
Also it has nothing to do with the existing resource or ternary operator in the module. I tested assigning null directly to the output parameter from the module without using any resources and it still gave me the error.
The error you're seeing is by design in ARM. The easiest way to deal with an output that may or may not be present on a completed module is to use the safe dereference (.?) operator:
resource appService ... = {
...
properties: {
virtualNetworkSubnetId: virtualNetwork.outputs.?appServicesSubnetId
...
}
}
The type checker will suggest an automatic fix injecting a .? into expressions where the left hand side of the . may be null, but it doesn't look like there's a diagnostic when the right hand side of the . may be missing. That should get added to catch issues like the one you ran into before deployment.
Thanks for the quick feedback, I tried the solution you provided but I still get the same error as in my opening post:
As far as I read the documentation about the dereference operator this solution shouldn't work, as outputs isn't null, but the value of my property is. So this is the 4th bullet point of this piece of documentation I believe:
- If a is non-null and has a property named x, the result of a.?x is the same as the result of a.x.
a (outputs) is not null, so the result should be the value of x (my property, which is null)?
Also I tested if perhaps the outputs property could be null, as my vnet module is a module with only one output (I simplified the example to post here). However, when I try it with another module sslConfiguration which has 2 other non-null outputs I get the same error when I try to access thumbprint, another string? whether I use the deference operator or not:
As far as I read the documentation about the dereference operator this solution shouldn't work, as
outputsisn't null, but the value of my property is. So this is the 4th bullet point of this piece of documentation I believe:
- If a is non-null and has a property named x, the result of a.?x is the same as the result of a.x.
a (outputs) is not null, so the result should be the value of x (my property, which is null)?
Outputs get special handling in the Bicep compiler because ARM returns each output as an object with two properties (type and value), so mod.outputs.?out in Bicep gets compiled to [tryGet(reference(resourceId('Microsoft.Resources/deployments', 'mod'), '2022-09-01').outputs, 'out', 'value')]. Looking at the compiled output, I can see that outputs.?<output> will handle conditional outputs but not nulled-out outputs (where it looks like the ARM engine will drop the value property rather than preserving "value": null in the completed resource representation.