Skip to content

nullable parameters in user defined functions #20105

Description

@cheng93

Bicep version
Bicep CLI version 0.45.15 (6a4a640)

Describe the bug
Can't pass null as a parameter into a user defined function

InvalidTemplate - Deployment template validation failed: 'The template resource 'rg' at line '1' and column '1851' is not valid: The provided value for the 'environment' parameter of the user-defined function '__bicep.rgName' is not valid. The value '[null()]' is not part of the allowed value(s): 'dev,hub,live,prelive,uat'.. Please see https://aka.ms/arm-functions for usage details.'.

To Reproduce

// main.bicep
import { locationTypes, locationName, rgName } from '../exports.bicep'

param location locationTypes

var workload = 'test'

resource rg 'Microsoft.Resources/resourceGroups@2025-04-01' = {
  name: rgName(null, location, workload)
  location: locationName(location)
}

// exports.bicep
@export()
type environmentType = 'dev' | 'uat' | 'prelive' | 'live' | 'hub'

@export()
type locationTypes = 'frc' | 'ne' | 'uks' | 'ukw'

var locationMap = {
  frc: 'francecentral'
  ne: 'northeurope'
  uks: 'uksouth'
  ukw: 'ukwest'
}

@export()
func locationName(location locationTypes) string => locationMap[location]

@export()
func namePart(part string?) string => empty(part) ? '' : '-${part}'

@export()
func rgName(environment environmentType?, location locationTypes, workload string?) string =>
  'rg-emp${namePart(environment)}-${location}${namePart(workload)}'

Additional context
Generated ARM

{
  "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
  "languageVersion": "2.0",
  "contentVersion": "1.0.0.0",
  "metadata": {
    "_generator": {
      "name": "bicep",
      "version": "0.45.15.27210",
      "templateHash": "5649992331914242084"
    }
  },
  "definitions": {
    "_1.environmentType": {
      "type": "string",
      "allowedValues": [
        "dev",
        "hub",
        "live",
        "prelive",
        "uat"
      ],
      "metadata": {
        "__bicep_imported_from!": {
          "sourceTemplate": "../exports.bicep"
        }
      }
    },
    "locationTypes": {
      "type": "string",
      "allowedValues": [
        "frc",
        "ne",
        "uks",
        "ukw"
      ],
      "metadata": {
        "__bicep_imported_from!": {
          "sourceTemplate": "../exports.bicep"
        }
      }
    }
  },
  "functions": [
    {
      "namespace": "__bicep",
      "members": {
        "locationName": {
          "parameters": [
            {
              "$ref": "#/definitions/locationTypes",
              "name": "location"
            }
          ],
          "output": {
            "type": "string",
            "value": "[variables('_1.locationMap')[parameters('location')]]"
          },
          "metadata": {
            "__bicep_imported_from!": {
              "sourceTemplate": "../exports.bicep"
            }
          }
        },
        "rgName": {
          "parameters": [
            {
              "$ref": "#/definitions/_1.environmentType",
              "nullable": true,
              "name": "environment"
            },
            {
              "$ref": "#/definitions/locationTypes",
              "name": "location"
            },
            {
              "type": "string",
              "nullable": true,
              "name": "workload"
            }
          ],
          "output": {
            "type": "string",
            "value": "[format('rg-emp{0}-{1}{2}', _1.namePart(parameters('environment')), parameters('location'), _1.namePart(parameters('workload')))]"
          },
          "metadata": {
            "__bicep_imported_from!": {
              "sourceTemplate": "../exports.bicep"
            }
          }
        }
      }
    },
    {
      "namespace": "_1",
      "members": {
        "namePart": {
          "parameters": [
            {
              "type": "string",
              "nullable": true,
              "name": "part"
            }
          ],
          "output": {
            "type": "string",
            "value": "[if(empty(parameters('part')), '', format('-{0}', parameters('part')))]"
          },
          "metadata": {
            "__bicep_imported_from!": {
              "sourceTemplate": "../exports.bicep"
            }
          }
        }
      }
    }
  ],
  "parameters": {
    "location": {
      "$ref": "#/definitions/locationTypes"
    }
  },
  "variables": {
    "workload": "test",
    "_1.locationMap": {
      "frc": "francecentral",
      "ne": "northeurope",
      "uks": "uksouth",
      "ukw": "ukwest"
    }
  },
  "resources": {
    "rg": {
      "type": "Microsoft.Resources/resourceGroups",
      "apiVersion": "2025-04-01",
      "name": "[__bicep.rgName(null(), parameters('location'), variables('workload'))]",
      "location": "[__bicep.locationName(parameters('location'))]"
    }
  }
}
{
    "experimentalFeaturesEnabled": {
        "waitAndRetry": true
    },
    "extensions": {
        "microsoftGraphV1": "br:mcr.microsoft.com/bicep/extensions/microsoftgraph/v1.0:1.0.0"
    }
}

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

Status
Todo

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions