Skip to content

Commit

Permalink
Make sure types from wildcard imports are assigned the correct type w…
Browse files Browse the repository at this point in the history
…hen pulling from ARM JSON models. (Azure#13255)

Resolves Azure#13248 
###### Microsoft Reviewers: [Open in
CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/Azure/bicep/pull/13255)
  • Loading branch information
jeskew committed Feb 9, 2024
1 parent 397977c commit 804565c
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
76 changes: 76 additions & 0 deletions src/Bicep.Core.IntegrationTests/CompileTimeImportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,47 @@ public void Type_symbols_imported_from_ARM_json_should_have_declarations_injecte
"""));
}

// https://github.com/Azure/bicep/issues/13248
[TestMethod]
public void Type_symbols_imported_from_ARM_json_via_wildcard_should_be_usable_as_types()
{
var result = CompilationHelper.Compile(
("main.bicep", """
import * as mod from 'mod.json'

param foo mod.foo
"""),
("mod.json", $$"""
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"languageVersion": "2.0",
"definitions": {
"foo": {
"metadata": {
"{{LanguageConstants.MetadataExportedPropertyName}}": true
},
"type": "array",
"items": {
"$ref": "#/definitions/bar"
}
},
"bar": {
"type": "string"
}
},
"resources": {}
}
"""));

result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();
result.Template.Should().HaveValueAtPath("parameters.foo", JToken.Parse("""
{
"$ref": "#/definitions/_1.foo"
}
"""));
}

[TestMethod]
public void Variable_symbols_imported_from_ARM_json_should_have_declarations_injected_into_compiled_template()
{
Expand Down Expand Up @@ -388,6 +429,41 @@ public void Variable_symbols_imported_from_ARM_json_should_have_declarations_inj
"""));
}

[TestMethod]
public void Variable_symbols_imported_from_ARM_json_with_wildcard_syntax_should_be_usable_as_values()
{
var result = CompilationHelper.Compile(
("main.bicep", """
import * as mod from 'mod.json'

output bar string = mod.foo.property
"""),
("mod.json", $$"""
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"metadata": {
"{{LanguageConstants.TemplateMetadataExportedVariablesName}}": [
{
"name": "foo",
"description": "A lengthy, florid description"
}
]
},
"variables": {
"foo": {
"property": "[variables('bar')]"
},
"bar": "barValue"
},
"resources": []
}
"""));

result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();
result.Template.Should().HaveValueAtPath("outputs.bar.value", "[variables('_1.foo').property]");
}

[TestMethod]
public void Function_symbols_imported_from_ARM_json_should_have_declarations_injected_into_compiled_template()
{
Expand Down
14 changes: 13 additions & 1 deletion src/Bicep.Core/Semantics/ArmTemplateSemanticModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ private ITypeReference GetType(TemplateOutputParameter output)
if (template.Definitions is { } typeDefinitions)
{
exports.AddRange(typeDefinitions.Where(kvp => IsExported(kvp.Value))
.Select(kvp => new ExportedTypeMetadata(kvp.Key, GetType(kvp.Value), GetMostSpecificDescription(kvp.Value))));
.Select(kvp => new ExportedTypeMetadata(kvp.Key, AsTypeType(GetType(kvp.Value)), GetMostSpecificDescription(kvp.Value))));
}

if (template.Functions is { } userDefinedFunctions)
Expand Down Expand Up @@ -280,6 +280,18 @@ private ITypeReference GetType(TemplateOutputParameter output)
return exportsBuilder.ToImmutable();
}

private static ITypeReference AsTypeType(ITypeReference @ref) => @ref switch
{
DeferredTypeReference => new DeferredTypeReference(() => AsTypeType(@ref.Type)),
_ => AsTypeType(@ref.Type),
};

private static TypeSymbol AsTypeType(TypeSymbol type) => type switch
{
TypeType or ErrorType => type,
_ => new TypeType(type),
};

private static bool TryCreateUnboundResourceTypeParameter(JToken? metadataToken, [NotNullWhen(true)] out TypeSymbol? type)
{
if (metadataToken is JObject metadata &&
Expand Down

0 comments on commit 804565c

Please sign in to comment.