Azure / Azure/bicep

AllowedSet for UDT discriminator

Open
#14,879 0 comments 1 reaction 0 assignees View on GitHub
enhancement Needs: Upvote
Dominant language
Bicep
Stars
3.6k
Forks
830
Avg merge
1d 21m
Merged PRs (30d)
79

Description

**Is your feature request related to a problem? Please describe.**
While Bicep's the `discriminator('')` [feature](https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/user-defined-data-types#tagged-union-data-type) is an absolutely great feature, it seems to lack support for one scenario, that is, sharing the same UDT for one discriminator value.

The scenario would for example be that I have a properties block of a resources that needs one set of properties if a property 'type' is set to 'Type1' or 'Type2' and completely different other set of the type is set to 'TypeN',

Do do this with a discriminator today, I'd actually have to duplicate the UDT for 'Type1' & 'Type2' even though they have the exact same values, because the discriminator must be a single string, and may not be an allowed set of strings.

This would look something like this:
```bicep
param myProperties selectedType

@discriminator('type')
type selectedType = TypeA | TypeB | TypeN

type TypeA = {
type: 'Type1'
property1: string
property2: string
}

type TypeB = {
type: 'Type2'
property1: string
property2: string
}

type TypeN = {
type: 'TypeN'
somethingElse: int
}

resource myResource 'Microsoft.Cake/gladosSaysHi@2007-10-10' = {
name: 'myResource'
location: 'westus'
properties: selectedType
}
```

**Describe the solution you'd like**
A nice way to solve this would be if the discriminator could accept an allowedSet of values that share the same implementation. This would look like the following:
```bicep
param myProperties selectedType

@discriminator('type')
type selectedType = TypeA | TypeN

type TypeA = {
type: ('Type1' | 'Type2')
property1: string
property2: string
}

type TypeN = {
type: 'TypeN'
somethingElse: int
}

resource myResource 'Microsoft.Cake/gladosSaysHi@2007-10-10' = {
name: 'myResource'
location: 'westus'
properties: selectedType
}
```

While the above is a fairly simplified example, we had more complex cases in AVM and it would be much appreciated if we could avoid duplicating the code for discriminator-dependent types 😉

cc: @krbar, @arnoldna

Contributor guide

Open the contributing guide

Research direction

No files, tests, or entry points are named. Start by locating Bicep's discriminator and user-defined type validation, then determine how an allowed set of discriminator values could share one implementation. Done means the proposed syntax accepts multiple discriminator strings without duplicating the associated user-defined type and preserves validation for other union members.

Written by the indexing model from the issue text.

Assessment

Tech stack
azure
Domain
cloud, compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.