Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bicep build generates Object instead of Array for resources when using user-defined functions #15268

Closed
galiacheng opened this issue Oct 8, 2024 · 4 comments
Assignees
Labels
documentation Improvements or additions to documentation
Milestone

Comments

@galiacheng
Copy link

Bicep version

0.30.23

Describe the bug

I am using Bicep to develop and maintain the source code for the Azure WebLogic on AKS marketplace offer. The marketplace requires converting Bicep scripts to ARM templates, following this ARM structure, where the resources section must be an array.

However, when building with bicep build or az bicep build, I receive an object instead of an array for the resources section, leading to a Partner Center error: Package acceptance validation error: JsonSchemaValidationError The json file 'mainTemplate.json' is invalid for the provided schema. Line Number: '0', Error: 'Invalid type. Expected Array but got Object.'

I traced this issue to user-defined functions in the Bicep script. Without them, the resources section builds correctly as an array. You can view the built ARM template and error in this GitHub action.

To Reproduce

  1. Bicep script without user defined function. Run az bicep build -f main.bicep to build the following content.

    param location string
    
    resource stg 'Microsoft.Storage/storageAccounts@2021-04-01' = {
    name: 'hellotest20241008001'
    location: location
    sku: {
      name: 'Standard_LRS'
    }
    kind: 'StorageV2'
    properties: {
      supportsHttpsTrafficOnly: true
    }
    }
    
    output storageEndpoint object = stg.properties.primaryEndpoints
    

    Got ARM template as following, type of resources is Array.

    {
      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "metadata": {
        "_generator": {
          "name": "bicep",
          "version": "0.30.23.60470",
          "templateHash": "5107046056470376467"
        }
      },
      "parameters": {
        "location": {
          "type": "string"
        }
      },
      "resources": [
        {
          "type": "Microsoft.Storage/storageAccounts",
          "apiVersion": "2021-04-01",
          "name": "hellotest20241008001",
          "location": "[parameters('location')]",
          "sku": {
            "name": "Standard_LRS"
          },
          "kind": "StorageV2",
          "properties": {
            "supportsHttpsTrafficOnly": true
          }
        }
      ],
      "outputs": {
        "storageEndpoint": {
          "type": "object",
          "value": "[reference(resourceId('Microsoft.Storage/storageAccounts', 'hellotest20241008001'), '2021-04-01').primaryEndpoints]"
        }
      }
    }
  2. Bicep script with user defined function. Run az bicep build -f main.bicep to build the following content.

    param location string
    
    var uniqueStorageName = abc(resourceGroup().id)
    
    func abc(resourceId string) string => string(resourceId)
    
    resource stg 'Microsoft.Storage/storageAccounts@2021-04-01' = {
      name: uniqueStorageName
      location: location
      sku: {
        name: 'Standard_LRS'
      }
      kind: 'StorageV2'
      properties: {
        supportsHttpsTrafficOnly: true
      }
    }
    
    output storageEndpoint object = stg.properties.primaryEndpoints

    Got ARM template as following, type of resources is Object.

    {
      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
      "languageVersion": "2.0",
      "contentVersion": "1.0.0.0",
      "metadata": {
        "_generator": {
          "name": "bicep",
          "version": "0.30.23.60470",
          "templateHash": "8515690428638717928"
        }
      },
      "functions": [
        {
          "namespace": "__bicep",
          "members": {
            "abc": {
              "parameters": [
                {
                  "type": "string",
                  "name": "resourceId"
                }
              ],
              "output": {
                "type": "string",
                "value": "[string(parameters('resourceId'))]"
              }
            }
          }
        }
      ],
      "parameters": {
        "location": {
          "type": "string"
        }
      },
      "variables": {
        "uniqueStorageName": "[__bicep.abc(resourceGroup().id)]"
      },
      "resources": {
        "stg": {
          "type": "Microsoft.Storage/storageAccounts",
          "apiVersion": "2021-04-01",
          "name": "[variables('uniqueStorageName')]",
          "location": "[parameters('location')]",
          "sku": {
            "name": "Standard_LRS"
          },
          "kind": "StorageV2",
          "properties": {
            "supportsHttpsTrafficOnly": true
          }
        }
      },
      "outputs": {
        "storageEndpoint": {
          "type": "object",
          "value": "[reference('stg').primaryEndpoints]"
        }
      }
    }

Additional context

The expected outcome is that, in both cases, the resources section should be an array, consistent with the ARM template definition.

@github-project-automation github-project-automation bot moved this to Todo in Bicep Oct 8, 2024
@galiacheng galiacheng changed the title az bicep build builds inconsistent ARM template Bicep build generates Object instead of Array for resources when using user-defined functions Oct 8, 2024
@anthony-c-martin
Copy link
Member

The reason for this is that usage of user-defined functions depends on language version 2.0.

The change from array -> object is explained here.

@galiacheng
Copy link
Author

Thanks for your prompt response, @anthony-c-martin. Could we clarify this in the documentation?

I would expect there to be documentation specifying which features trigger a language version 2.0 template, as the Java EE on Azure team uses Bicep extensively to build marketplace offers.

@anthony-c-martin
Copy link
Member

Thanks for your prompt response, @anthony-c-martin. Could we clarify this in the documentation?

@stephaniezyen I see you started this under #14866. Please could we arrange for this to be moved to the official Microsoft docs?

@anthony-c-martin anthony-c-martin added the documentation Improvements or additions to documentation label Oct 16, 2024
@alex-frankel alex-frankel modified the milestones: Committed Backlog, v0.31 Oct 17, 2024
@stephaniezyen stephaniezyen modified the milestones: v0.31, v0.32 Nov 6, 2024
@github-project-automation github-project-automation bot moved this from Todo to Done in Bicep Nov 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
Archived in project
Development

No branches or pull requests

5 participants