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

Fix importing tuples from JSON #13736

Merged
merged 1 commit into from Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -134,28 +134,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