Skip to content

Commit

Permalink
Apply sealed modifier to types loaded from ARM JSON only if `additi…
Browse files Browse the repository at this point in the history
…onalProperties` was set to `false` in the source
  • Loading branch information
jeskew committed Mar 28, 2024
1 parent 855485d commit f480a5b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 8 deletions.
54 changes: 54 additions & 0 deletions src/Bicep.Core.IntegrationTests/CompileTimeImportTests.cs
Expand Up @@ -2258,4 +2258,58 @@ public void Copy_index_argument_in_copy_variables_imported_from_json_is_replaced
}
"""));
}

[TestMethod]
public void Tuple_imported_from_json_is_recompiled_to_a_valid_schema()
{
var typesBicep = """
@export()
type t = {
p: {
a: [
{
b: string
c: string
}
]
}
}
""";

var expectedCompilationOfTupleA = """
{
"type": "array",
"prefixItems": [
{
"type": "object",
"properties": {
"b": {
"type": "string"
},
"c": {
"type": "string"
}
}
}
],
"items": false
}
""";

var resultFromBicep = CompilationHelper.Compile(
("types.bicep", typesBicep),
("main.bicep", "import {t} from 'types.bicep'"));

resultFromBicep.Should().NotHaveAnyCompilationBlockingDiagnostics();
resultFromBicep.Template.Should().NotBeNull();
resultFromBicep.Template.Should().HaveJsonAtPath("definitions.t.properties.p.properties.a", expectedCompilationOfTupleA);

var resultFromJson = CompilationHelper.Compile(
("types.json", CompilationHelper.Compile(typesBicep).Template!.ToString()),
("main.bicep", "import {t} from 'types.json'"));

resultFromJson.Should().NotHaveAnyCompilationBlockingDiagnostics();
resultFromJson.Template.Should().NotBeNull();
resultFromJson.Template.Should().HaveJsonAtPath("definitions.t.properties.p.properties.a", expectedCompilationOfTupleA);
}
}
Expand Up @@ -136,28 +136,28 @@ private record TypeModifiers(Expression? Description,
Expression? Sealed);

private TypeModifiers GetTypeModifiers(ITemplateSchemaNode schemaNode) => new(
GetDescription(schemaNode) is string description ? ExpressionFactory.CreateStringLiteral(description, sourceSyntax) : null,
schemaNode.Metadata?.Value is JToken md && ConvertToExpression(ImmutableDictionary<JToken, LanguageExpression>.Empty, md) is ObjectExpression @object
Description: GetDescription(schemaNode) is string description ? ExpressionFactory.CreateStringLiteral(description, sourceSyntax) : null,
Metadata: schemaNode.Metadata?.Value is JToken md && ConvertToExpression(ImmutableDictionary<JToken, LanguageExpression>.Empty, md) is ObjectExpression @object
? ExcludingPropertiesNamed(@object, LanguageConstants.MetadataDescriptionPropertyName, LanguageConstants.MetadataExportedPropertyName)
: null,
schemaNode.Type?.Value switch
Secure: schemaNode.Type?.Value switch
{
TemplateParameterType.SecureString or TemplateParameterType.SecureObject => ExpressionFactory.CreateBooleanLiteral(true, sourceSyntax),
_ => null,
},
schemaNode.MinLength?.Value is long minLength
MinLength: schemaNode.MinLength?.Value is long minLength
? ExpressionFactory.CreateIntegerLiteral(minLength, sourceSyntax)
: null,
schemaNode.MaxLength?.Value is long maxLength
MaxLength: schemaNode.MaxLength?.Value is long maxLength
? ExpressionFactory.CreateIntegerLiteral(maxLength, sourceSyntax)
: null,
schemaNode.MinValue?.Value is long minValue
MinValue: schemaNode.MinValue?.Value is long minValue
? ExpressionFactory.CreateIntegerLiteral(minValue, sourceSyntax)
: null,
schemaNode.MaxValue?.Value is long maxValue
MaxValue: schemaNode.MaxValue?.Value is long maxValue
? ExpressionFactory.CreateIntegerLiteral(maxValue, sourceSyntax)
: null,
schemaNode.AdditionalProperties?.BooleanValue is false || schemaNode.Items?.BooleanValue is false
Sealed: schemaNode.AdditionalProperties?.BooleanValue is false
? ExpressionFactory.CreateBooleanLiteral(true, sourceSyntax)
: null);

Expand Down

0 comments on commit f480a5b

Please sign in to comment.