How can I generate a parameter using switch against a datatype choice parameter with dotnet new templates?

Aaron Chun 20 Reputation points
2025-03-06T22:00:22.3666667+00:00

While creating a template for dotnet new <template>, one of the required parameters is of datatype "choice". I am trying to then have a generated parameter with the generator of "switch" based on the choice parameter. When using the template, the string that is supposed to be replaced hasn't been replaced in any of the files. Considering the other parameters are successfully being replaced elsewhere, I'm assuming the issue comes from the actual switch conditionals.

The required choice parameter is being used for other generated and derived parameters so I've also tried to use those as the switch case conditionals and have not had any luck there either. Below is the basic setup I currently have (minus more parameters that aren't related to the issue).

{
    "$schema": "http://json.schemastore.org/template",
    .
    .
    .
    ,
    "symbols": {
        "ChoiceInput": {
            "type": "parameter",
            "isRequired": true,
            "datatype": "choice",
            "choices": [
                {
                    "choice": "Choice 1",
                    "description": "Choice 1"
                },
                {
                    "choice": "Choice 2",
                    "description": "Choice 2"
                },
                .
                .
                .
                ,
                {
                    "choice": "Choice 8",
                    "description": "Choice 8"
                },
                {
                    "choice": "Choice 9",
                    "description": "Choice 9"
                }
            ]
        },
        "ChoiceDerived": {
            "type": "derived",
            "valueSource": "ChoiceInput",
            "valueTransform": "Combine",
            "replaces": "{ChoiceDerivedParameter}"
        },
        "ChoiceDerivedLower": {
            "type": "generated",
            "generator": "casing",
            "parameters": {
              "source": "ChoiceDerived",
              "toLower": true
            },
            "replaces": "{choicederivedparameter}"
        },
        "ChoiceShortHand": {
            "type": "generated",
            "generator": "switch",
            "parameters": {
              "datatype": "string",
              "replaces": "{cshparameter}",
              "cases": [
                {
                    "condition": "(ChoiceInput== 'Choice 1')",
                    "value": "c1"
                },
                {
                    "condition": "(ChoiceInput== 'Choice 2')",
                    "value": "c2"
                },
                .
                .
                .
                ,
                {
                    "condition": "(ChoiceInput== 'Choice 8')",
                    "value": "c8"
                },
                {
                    "condition": "(ChoiceInput== 'Choice 9')",
                    "value": "c9"
                }
              ]
            }
        }
    },
    "forms": {
        "lc": {
            "identifier": "lowerCase"
        },
        "Combine": {
            "identifier": "replace",
            "pattern": " |-",
            "replacement": ""
        }
    }
}
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,198 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 33,701 Reputation points Microsoft External Staff
    2025-03-07T14:45:11.49+00:00

    Hi @Aaron Chun ,

    The problem could be a type mismatch.

    Try replace the "datatype": "string", to "datatype": "choice", in ChoiceShortHand.

    Best Regards.

    Jiachen Li


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.