Skip to content

Fix the dictionary indexer issue #50265

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

ArcturusZhang
Copy link
Member

@ArcturusZhang ArcturusZhang commented May 26, 2025

Fixes #48491

I think the dictionary we have two different usages.

  1. The dictionary is storing the values, and we should write them out when building the bicep expressions.
  2. The dictionary is only symbolic, we do not even know what value is in it - but when everything executes, there should be some values in it therefore we just split out an indexer expression.

I think our BicepDictionary<T> should support both the purposes.
In the issue, that property is an output property. In this issue, we could imply that this dictionary only has the second purpose, and we should split out an indexer expression.

Contributing to the Azure SDK

Please see our CONTRIBUTING.md if you are not familiar with contributing to this repository or have questions.

For specific information about pull request etiquette and best practices, see this section.

@ArcturusZhang ArcturusZhang marked this pull request as ready for review May 27, 2025 03:50
@Copilot Copilot AI review requested due to automatic review settings May 27, 2025 03:50
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for symbolic output indexers on Bicep lists and dictionaries, implements the corresponding logic in BicepListOfT and BicepDictionaryOfT, and introduces new tests to verify indexing behavior.

  • Implemented _isOutput branches in this[int] and this[string] indexers for lists and dictionaries.
  • Added unit tests for output array and dictionary indexing.
  • Updated test files with necessary imports and scaffolding.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
sdk/provisioning/Azure.Provisioning/tests/Expressions/ArrayExpressionTests.cs Added missing using for Azure.Provisioning.Primitives.
sdk/provisioning/Azure.Provisioning/tests/BicepValues/BicepValueTests.cs Added tests for output array/dictionary indexing and test scaffolding.
sdk/provisioning/Azure.Provisioning/src/BicepListOfT.cs Added _isOutput branch in list indexer to emit Bicep syntax.
sdk/provisioning/Azure.Provisioning/src/BicepDictionaryOfT.cs Refactored dictionary indexer get/set to handle output expressions and prevent invalid assigns.
Comments suppressed due to low confidence (1)

sdk/provisioning/Azure.Provisioning/tests/BicepValues/BicepValueTests.cs:38

  • Add a [Test] attribute above the ValidateLiteralBicepValue method to ensure it is recognized and executed by NUnit.
public void ValidateLiteralBicepValue()

@ArcturusZhang ArcturusZhang requested a review from Copilot May 27, 2025 04:55
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request fixes the dictionary indexer issue by distinguishing between output and input usages for Bicep dictionaries and lists. Key changes include:

  • Adding new tests for both output and input scenarios in arrays and dictionaries.
  • Modifying BicepListOfT.cs to handle output index access using the resource’s reference.
  • Updating BicepDictionaryOfT.cs to conditionally return an indexer expression for output properties and disallow assignments.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
sdk/provisioning/Azure.Provisioning/tests/Expressions/ArrayExpressionTests.cs Added necessary using directive for primitives.
sdk/provisioning/Azure.Provisioning/tests/BicepValues/BicepValueTests.cs Introduced several tests covering output and input scenarios for arrays and dictionaries.
sdk/provisioning/Azure.Provisioning/src/BicepListOfT.cs Modified indexer to return an expression when in output mode.
sdk/provisioning/Azure.Provisioning/src/BicepDictionaryOfT.cs Updated indexer logic for output properties and ensured assignments are disallowed for expressions and outputs.

@@ -89,6 +89,10 @@ public BicepValue<T> this[int index]
// can reference their members.
return _referenceFactory!(BicepSyntax.Index(_expression!, BicepSyntax.Value(index)));
}
else if (_isOutput)
Copy link
Preview

Copilot AI May 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that _self is never null when _isOutput is true to prevent a potential NullReferenceException; consider adding documentation or an explicit null check.

Copilot uses AI. Check for mistakes.


set
{
if (_kind == BicepValueKind.Expression || _isOutput)
Copy link
Preview

Copilot AI May 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clarify the behavior in the set accessor by documenting that assignment is disallowed for output properties and when the value kind is an expression, which would help maintain the intended immutability.

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Indexing a BicepDictionary as an output throws KeyNotFoundException
2 participants