Skip to content

Commit

Permalink
Don't collapse scope references or namespaces into objects (#15570)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeskew authored Nov 12, 2024
1 parent 4a382ed commit d2ccb35
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Bicep.Core.IntegrationTests/ScenarioTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6322,4 +6322,20 @@ public void Test_Issue13596(bool enableSymbolicNameCodegen)
result.Template.Should().HaveJsonAtPath("$.resources[?(@.name=='vault/secret')].dependsOn", """["[resourceId('Microsoft.Resources/deployments', 'mod')]"]""");
}
}

[TestMethod]
// https://github.com/azure/bicep/issues/15517
public void Test_Issue15517()
{
var result = CompilationHelper.Compile("""
param condition bool
resource sa 'Microsoft.Storage/storageAccounts@2023-05-01' existing = {
name: 'account'
scope: condition ? resourceGroup('a') : resourceGroup('b')
}
""");

result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();
}
}
70 changes: 70 additions & 0 deletions src/Bicep.Core.UnitTests/TypeSystem/TypeHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
// Licensed under the MIT License.
using System.Collections.Immutable;
using Bicep.Core.Extensions;
using Bicep.Core.Semantics;
using Bicep.Core.Semantics.Namespaces;
using Bicep.Core.Syntax;
using Bicep.Core.TypeSystem;
using Bicep.Core.TypeSystem.Providers;
using Bicep.Core.TypeSystem.Providers.Az;
using Bicep.Core.TypeSystem.Types;
using Bicep.Core.UnitTests.Assertions;
using FluentAssertions;
Expand Down Expand Up @@ -403,4 +408,69 @@ public void Object_collapse_should_incorporate_additionalProperties_types()
collapsed.AdditionalPropertiesType!.Type.Name.Should().Be("int");
collapsed.AdditionalPropertiesFlags.HasFlag(TypePropertyFlags.FallbackProperty).Should().BeTrue();
}

[TestMethod]
public void Scope_reference_objects_should_not_be_collapsed()
{
var scopeRef = new ResourceGroupScopeType(
ImmutableArray<FunctionArgumentSyntax>.Empty,
ImmutableArray<TypeProperty>.Empty);

var objects = new ObjectType[]
{
new("{foo: string}",
default,
new TypeProperty[]
{
new("foo", LanguageConstants.String, TypePropertyFlags.Required),
},
null),
new("{bar: string}",
default,
new TypeProperty[]
{
new("bar", LanguageConstants.String, TypePropertyFlags.Required),
},
LanguageConstants.Int),
};

TypeHelper.TryCollapseTypes(scopeRef.AsEnumerable()).Should().BeSameAs(scopeRef);
TypeHelper.TryCollapseTypes(objects).Should().BeAssignableTo<ObjectType>();
TypeHelper.TryCollapseTypes(objects.Append(scopeRef)).Should().BeNull();
}

[TestMethod]
public void Namespace_objects_should_not_be_collapsed()
{
var @namespace = new NamespaceType(
"alias",
new(true, "bicepExtensionName", null, "templateExtensionName", "1.0"),
ImmutableArray<TypeProperty>.Empty,
ImmutableArray<FunctionOverload>.Empty,
ImmutableArray<BannedFunction>.Empty,
ImmutableArray<Decorator>.Empty,
new EmptyResourceTypeProvider());

var objects = new ObjectType[]
{
new("{foo: string}",
default,
new TypeProperty[]
{
new("foo", LanguageConstants.String, TypePropertyFlags.Required),
},
null),
new("{bar: string}",
default,
new TypeProperty[]
{
new("bar", LanguageConstants.String, TypePropertyFlags.Required),
},
LanguageConstants.Int),
};

TypeHelper.TryCollapseTypes(@namespace.AsEnumerable()).Should().BeSameAs(@namespace);
TypeHelper.TryCollapseTypes(objects).Should().BeAssignableTo<ObjectType>();
TypeHelper.TryCollapseTypes(objects.Append(@namespace)).Should().BeNull();
}
}
4 changes: 4 additions & 0 deletions src/Bicep.Core/TypeSystem/TypeCollapser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,10 @@ public UnionCollapseState Push(ITypeReference memberType)
{
switch (memberType.Type)
{
// scope references and namespaces aren't used as values and therefore cannot be collapsed
case IScopeReference:
case NamespaceType:
return Uncollapsable.Instance;
case ObjectType @object:
flags |= @object.ValidationFlags;
discriminatedObjectTypeBuilder.TryInclude(@object);
Expand Down

0 comments on commit d2ccb35

Please sign in to comment.