bicep icon indicating copy to clipboard operation
bicep copied to clipboard

Using a nullable string output from a module as input for another resource gives an error

Open AlexanderDerck opened this issue 2 years ago • 3 comments

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

  1. Create a module which has a string? output that actually outputs a null value (in my case integrateVnet == false). image

  2. Use the output as input for a new resource. image

  3. I get an obscure error about a property value that doesn't exist, although my property is called appServicesSubnetId. I assume the message is about the value property of the nullable type (like in C#)? image

Additional context Now I use the following workaround using an empty string which does work.

  1. The module image

  2. The resource using the module image

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.

AlexanderDerck avatar Jan 31 '24 10:01 AlexanderDerck

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.

jeskew avatar Jan 31 '24 14:01 jeskew

Thanks for the quick feedback, I tried the solution you provided but I still get the same error as in my opening post: image

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:

image

AlexanderDerck avatar Jan 31 '24 15:01 AlexanderDerck

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)?

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.

jeskew avatar Jan 31 '24 15:01 jeskew