Skip to content

Commit 2f3624a

Browse files
authored
Use integer and boolean literal types when deriving return type for loadJsonContent (#18466)
Resolves #18416
1 parent 438573a commit 2f3624a

File tree

5 files changed

+50
-18
lines changed

5 files changed

+50
-18
lines changed

src/Bicep.Core.IntegrationTests/ScenarioTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7508,4 +7508,33 @@ public void Test_Issue18217_module()
75087508

75097509
result.Should().NotHaveAnyDiagnostics();
75107510
}
7511+
7512+
[TestMethod]
7513+
public void Test_Issue18416()
7514+
{
7515+
var result = CompilationHelper.CompileParams(
7516+
("foo.json", """
7517+
{
7518+
"default": 5,
7519+
"boolLiteral": true
7520+
}
7521+
"""),
7522+
("main.bicep", """
7523+
type fooType = 5 | 10 | 15
7524+
7525+
param foo fooType
7526+
7527+
param bar true
7528+
"""),
7529+
("parameters.bicepparam", """
7530+
using 'main.bicep'
7531+
7532+
var fooVar = loadJsonContent('foo.json')
7533+
7534+
param foo = fooVar.default
7535+
param bar = fooVar.boolLiteral
7536+
"""));
7537+
7538+
result.Should().NotHaveAnyDiagnostics();
7539+
}
75117540
}

src/Bicep.Core.IntegrationTests/TypeSystem/TypeValidationTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ public void Json_function_can_obtain_types_for_string_literal_json_args()
366366
var floatJson = json('1234.1224')
367367
var stringJson = json('""hello!""')
368368
var nullJson = json('null')
369+
var boolJson = json('true')
369370
var jsonWithComments = json('''
370371
{
371372
//here's a comment!
@@ -385,10 +386,11 @@ public void Json_function_can_obtain_types_for_string_literal_json_args()
385386
GetTypeForNamedSymbol(model, "objectJson").Name.Should().Be("object");
386387
GetTypeForNamedSymbol(model, "propAccess").Name.Should().Be("'validValue'");
387388

388-
GetTypeForNamedSymbol(model, "intJson").Name.Should().Be("int");
389+
GetTypeForNamedSymbol(model, "intJson").Name.Should().Be("123");
389390
GetTypeForNamedSymbol(model, "floatJson").Name.Should().Be("any");
390391
GetTypeForNamedSymbol(model, "stringJson").Name.Should().Be("'hello!'");
391392
GetTypeForNamedSymbol(model, "nullJson").Name.Should().Be("null");
393+
GetTypeForNamedSymbol(model, "boolJson").Name.Should().Be("true");
392394
GetTypeForNamedSymbol(model, "commentsPropAccess").Name.Should().Be("'value'");
393395

394396
GetTypeForNamedSymbol(model, "invalidPropAccess").Name.Should().Be("error");

src/Bicep.Core.Samples/Files/baselines/LoadFunctions_CRLF/main.symbols.bicep

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ var testJson = json(loadTextContent('./Assets/test.json.txt'))
100100
var testJsonString = testJson.string
101101
//@[4:18) Variable testJsonString. Type: 'someVal'. Declaration start char: 0, length: 36
102102
var testJsonInt = testJson.int
103-
//@[4:15) Variable testJsonInt. Type: int. Declaration start char: 0, length: 30
103+
//@[4:15) Variable testJsonInt. Type: 123. Declaration start char: 0, length: 30
104104
var testJsonArrayVal = testJson.array[0]
105-
//@[4:20) Variable testJsonArrayVal. Type: int. Declaration start char: 0, length: 40
105+
//@[4:20) Variable testJsonArrayVal. Type: 1 | 2. Declaration start char: 0, length: 40
106106
var testJsonObject = testJson.object
107107
//@[4:18) Variable testJsonObject. Type: object. Declaration start char: 0, length: 36
108108
var testJsonNestedString = testJson.object.nestedString
@@ -115,13 +115,13 @@ var testJsonString2 = testJson.string
115115
var testJsonString2_1 = loadJsonContent('./Assets/test.json.txt', '.string')
116116
//@[4:21) Variable testJsonString2_1. Type: 'someVal'. Declaration start char: 0, length: 76
117117
var testJsonInt2 = testJson.int
118-
//@[4:16) Variable testJsonInt2. Type: int. Declaration start char: 0, length: 31
118+
//@[4:16) Variable testJsonInt2. Type: 123. Declaration start char: 0, length: 31
119119
var testJsonInt2_1 = loadJsonContent('./Assets/test.json.txt', '.int')
120-
//@[4:18) Variable testJsonInt2_1. Type: int. Declaration start char: 0, length: 70
120+
//@[4:18) Variable testJsonInt2_1. Type: 123. Declaration start char: 0, length: 70
121121
var testJsonArrayVal2 = testJson.array[0]
122-
//@[4:21) Variable testJsonArrayVal2. Type: int. Declaration start char: 0, length: 41
122+
//@[4:21) Variable testJsonArrayVal2. Type: 1 | 2. Declaration start char: 0, length: 41
123123
var testJsonArrayVal2_1 = loadJsonContent('./Assets/test.json.txt', '.array[0]')
124-
//@[4:23) Variable testJsonArrayVal2_1. Type: int. Declaration start char: 0, length: 80
124+
//@[4:23) Variable testJsonArrayVal2_1. Type: 1. Declaration start char: 0, length: 80
125125
var testJsonObject2 = testJson.object
126126
//@[4:19) Variable testJsonObject2. Type: object. Declaration start char: 0, length: 37
127127
var testJsonObject2_1 = loadJsonContent('./Assets/test.json.txt', '.object')
@@ -146,29 +146,29 @@ var testYaml = loadYamlContent('./Assets/test.yaml.txt')
146146
var testYamlString = testYaml.string
147147
//@[4:18) Variable testYamlString. Type: 'someVal'. Declaration start char: 0, length: 36
148148
var testYamlInt = testYaml.int
149-
//@[4:15) Variable testYamlInt. Type: int. Declaration start char: 0, length: 30
149+
//@[4:15) Variable testYamlInt. Type: 123. Declaration start char: 0, length: 30
150150
var testYamlBool = testYaml.bool
151-
//@[4:16) Variable testYamlBool. Type: bool. Declaration start char: 0, length: 32
151+
//@[4:16) Variable testYamlBool. Type: true. Declaration start char: 0, length: 32
152152
var testYamlArrayInt = testYaml.arrayInt
153-
//@[4:20) Variable testYamlArrayInt. Type: int[]. Declaration start char: 0, length: 40
153+
//@[4:20) Variable testYamlArrayInt. Type: (1 | 2)[]. Declaration start char: 0, length: 40
154154
var testYamlArrayIntVal = testYaml.arrayInt[0]
155-
//@[4:23) Variable testYamlArrayIntVal. Type: int. Declaration start char: 0, length: 46
155+
//@[4:23) Variable testYamlArrayIntVal. Type: 1 | 2. Declaration start char: 0, length: 46
156156
var testYamlArrayString = testYaml.arrayString
157157
//@[4:23) Variable testYamlArrayString. Type: ('someVal' | 'someVal2')[]. Declaration start char: 0, length: 46
158158
var testYamlArrayStringVal = testYaml.arrayString[0]
159159
//@[4:26) Variable testYamlArrayStringVal. Type: 'someVal' | 'someVal2'. Declaration start char: 0, length: 52
160160
var testYamlArrayBool = testYaml.arrayBool
161-
//@[4:21) Variable testYamlArrayBool. Type: bool[]. Declaration start char: 0, length: 42
161+
//@[4:21) Variable testYamlArrayBool. Type: true[]. Declaration start char: 0, length: 42
162162
var testYamlArrayBoolVal = testYaml.arrayBool[0]
163-
//@[4:24) Variable testYamlArrayBoolVal. Type: bool. Declaration start char: 0, length: 48
163+
//@[4:24) Variable testYamlArrayBoolVal. Type: true. Declaration start char: 0, length: 48
164164
var testYamlObject = testYaml.object
165165
//@[4:18) Variable testYamlObject. Type: object. Declaration start char: 0, length: 36
166166
var testYamlObjectNestedString = testYaml.object.nestedString
167167
//@[4:30) Variable testYamlObjectNestedString. Type: 'someVal'. Declaration start char: 0, length: 61
168168
var testYamlObjectNestedInt = testYaml.object.nestedInt
169-
//@[4:27) Variable testYamlObjectNestedInt. Type: int. Declaration start char: 0, length: 55
169+
//@[4:27) Variable testYamlObjectNestedInt. Type: 123. Declaration start char: 0, length: 55
170170
var testYamlObjectNestedBool = testYaml.object.nestedBool
171-
//@[4:28) Variable testYamlObjectNestedBool. Type: bool. Declaration start char: 0, length: 57
171+
//@[4:28) Variable testYamlObjectNestedBool. Type: true. Declaration start char: 0, length: 57
172172

173173
output testYamlString string = testYamlString
174174
//@[7:21) Output testYamlString. Type: string. Declaration start char: 0, length: 45

src/Bicep.Core.Samples/Files/baselines_bicepparam/Imports/parameters.symbols.bicepparam

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as bicepconfig from 'bicepconfig.bicep'
33
//@[12:23) ImportedNamespace bicepconfig. Type: bicepconfig. Declaration start char: 7, length: 16
44
// ok
55
param one = bicepconfig.directExport
6-
//@[06:09) ParameterAssignment one. Type: bool. Declaration start char: 0, length: 36
6+
//@[06:09) ParameterAssignment one. Type: true. Declaration start char: 0, length: 36
77
// Failed to evaluate parameter "two"
88
// Unhandled exception during evaluating template language function 'variables' is not handled.bicep(BCP338)
99
param two = bicepconfig.functionExport

src/Bicep.Core/Semantics/Namespaces/SystemNamespaceType.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Globalization;
66
using System.Numerics;
77
using System.Text;
8+
using Azure.Deployments.Expression.Extensions;
89
using Bicep.Core.Analyzers.Linter;
910
using Bicep.Core.Diagnostics;
1011
using Bicep.Core.Extensions;
@@ -1666,10 +1667,10 @@ public static TypeSymbol ConvertJsonToBicepType(JToken token)
16661667
JValue value => value.Type switch
16671668
{
16681669
JTokenType.String => TypeFactory.CreateStringLiteralType(value.ToString(CultureInfo.InvariantCulture)),
1669-
JTokenType.Integer => LanguageConstants.Int,
1670+
JTokenType.Integer => TypeFactory.CreateIntegerLiteralType(value.ToLong()),
16701671
// Floats are currently not supported in Bicep, so fall back to the default behavior of "any"
16711672
JTokenType.Float => LanguageConstants.Any,
1672-
JTokenType.Boolean => LanguageConstants.Bool,
1673+
JTokenType.Boolean => TypeFactory.CreateBooleanLiteralType(value.ToObject<bool>()),
16731674
JTokenType.Null => LanguageConstants.Null,
16741675
_ => LanguageConstants.Any,
16751676
},

0 commit comments

Comments
 (0)