Skip to content

Commit bdbf420

Browse files
Use correct JSON serializer settings for all templates-related deserialization (#24282)
* Use correct JSON serializer settings for templates-related deserialization * Add unhandled exception debug logging * Add repro for #24286 * Update ChangeLog.md --------- Co-authored-by: Vincent Dai <[email protected]>
1 parent cd16fce commit bdbf420

File tree

15 files changed

+730
-37
lines changed

15 files changed

+730
-37
lines changed

src/Resources/ResourceManager/Extensions/JsonExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public static string ToJson(this object obj)
101101
/// <param name="obj">The object.</param>
102102
public static string ToFormattedJson(this object obj)
103103
{
104-
return JsonConvert.SerializeObject(obj, Formatting.Indented);
104+
return JsonConvert.SerializeObject(obj, Formatting.Indented, JsonExtensions.ObjectSerializationSettings);
105105
}
106106

107107
/// <summary>

src/Resources/ResourceManager/Implementation/CmdletBase/DeploymentCmdletBase.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
using System.Linq;
2626
using System.Management.Automation;
2727
using ProjectResources = Microsoft.Azure.Commands.ResourceManager.Cmdlets.Properties.Resources;
28+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
2829

2930
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
3031
{
@@ -403,7 +404,7 @@ protected void BuildAndUseBicepParameters(bool emitWarnings)
403404

404405
if (!string.IsNullOrEmpty(output.templateJson))
405406
{
406-
TemplateObject = JsonConvert.DeserializeObject<Hashtable>(output.templateJson);
407+
TemplateObject = output.templateJson.FromJson<Hashtable>();
407408
}
408409
else if (!string.IsNullOrEmpty(output.templateSpecId))
409410
{
@@ -425,7 +426,7 @@ protected void BuildAndUseBicepParameters(bool emitWarnings)
425426
protected void BuildAndUseBicepTemplate()
426427
{
427428
var templateJson = BicepUtility.Create().BuildBicepFile(this.ResolvePath(TemplateFile), this.WriteVerbose, this.WriteWarning);
428-
TemplateObject = JsonConvert.DeserializeObject<Hashtable>(templateJson);
429+
TemplateObject = templateJson.FromJson<Hashtable>();
429430
TemplateFile = null;
430431
}
431432
}

src/Resources/ResourceManager/Implementation/CmdletBase/ResourceManagerCmdletBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ public sealed override void ExecuteCmdlet()
181181
throw;
182182
}
183183

184+
this.WriteDebugWithTimestamp($"[{nameof(ResourceManagerCmdletBase)}.{nameof(ExecuteCmdlet)}] Caught unhandled exception: {ex}");
185+
184186
var capturedException = ExceptionDispatchInfo.Capture(ex);
185187
this.HandleException(capturedException: capturedException);
186188
}

src/Resources/ResourceManager/SdkExtensions/NewResourcesExtensions.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
//This class is split from the ResourcesExtensions class. Since this module has both cmdlets that use the old sdk version and cmdlets that use the new one, we needed client extension classes for both and with this split
1717
//this class acts as the client for the cmdlets using the newer bits while the ResourcesExtensions class acts as the client for the cmdlets using old sdk
1818

19+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
1920
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels;
2021
using Microsoft.Azure.Commands.ResourceManager.Common.Tags;
2122
using Microsoft.Azure.Management.Resources.Models;
@@ -321,25 +322,25 @@ private static void SetDeploymentProperties(PSDeploymentObject deploymentObject,
321322

322323
if (properties.Outputs != null)
323324
{
324-
Dictionary<string, DeploymentVariable> outputs = JsonConvert.DeserializeObject<Dictionary<string, DeploymentVariable>>(properties.Outputs.ToString());
325+
var outputs = properties.Outputs.ToString().FromJson<Dictionary<string, DeploymentVariable>>();
325326
// Continue deserialize if the type of Value in DeploymentVariable is array
326327
outputs?.Values.ForEach(dv => {
327328
if ("Array".Equals(dv?.Type) && dv?.Value != null)
328329
{
329-
dv.Value = JsonConvert.DeserializeObject<object[]>(dv.Value.ToString());
330+
dv.Value = dv.Value.ToString().FromJson<object[]>();
330331
}
331332
});
332333
deploymentObject.Outputs = outputs;
333334
}
334335

335336
if (properties.Parameters != null)
336337
{
337-
Dictionary<string, DeploymentVariable> parameters = JsonConvert.DeserializeObject<Dictionary<string, DeploymentVariable>>(properties.Parameters.ToString());
338+
var parameters = properties.Parameters.ToString().FromJson<Dictionary<string, DeploymentVariable>>();
338339
// Continue deserialize if the type of Value in DeploymentVariable is array
339340
parameters?.Values.ForEach(dv => {
340341
if ("Array".Equals(dv?.Type) && dv?.Value != null)
341342
{
342-
dv.Value = JsonConvert.DeserializeObject<object[]>(dv.Value.ToString());
343+
dv.Value = dv.Value.ToString().FromJson<object[]>();
343344
}
344345
});
345346
deploymentObject.Parameters = parameters;

src/Resources/ResourceManager/SdkModels/DeploymentStacks/PSDeploymentStack.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
1516
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkExtensions;
1617
using Microsoft.Azure.Management.Resources.Models;
1718
using Microsoft.WindowsAzure.Commands.Utilities.Common;
@@ -173,7 +174,7 @@ internal static Dictionary<string, DeploymentVariable> FormatMappedObject(object
173174
{
174175
if ("Array".Equals(dv?.Type) && dv?.Value != null)
175176
{
176-
dv.Value = JsonConvert.DeserializeObject<object[]>(dv.Value.ToString());
177+
dv.Value = dv.Value.ToString().FromJson<object[]>();
177178
}
178179
});
179180

@@ -183,7 +184,7 @@ internal static Dictionary<string, DeploymentVariable> FormatMappedObject(object
183184
internal static DeploymentVariable ExtractDeploymentVariable(JObject jObject)
184185
{
185186
// Attempt to desialize the DeploymentVariable.
186-
DeploymentVariable dVar = JsonConvert.DeserializeObject<DeploymentVariable>(jObject.ToString());
187+
var dVar = jObject.ToString().FromJson<DeploymentVariable>();
187188

188189
// If the type is null, attempt to infer the type.
189190
if (dVar.Type is null)

src/Resources/ResourceManager/Utilities/BicepUtility.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Utilities
2424
using System.IO;
2525
using System.Text.RegularExpressions;
2626
using Microsoft.Azure.Commands.Common.Authentication;
27+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
2728

2829
public class BicepBuildParamsStdout
2930
{
@@ -137,7 +138,7 @@ public BicepBuildParamsStdout BuildBicepParamFile(string bicepParamFilePath, IRe
137138
writeVerbose: writeVerbose,
138139
writeWarning: writeWarning);
139140

140-
return JsonConvert.DeserializeObject<BicepBuildParamsStdout>(stdout);
141+
return stdout.FromJson<BicepBuildParamsStdout>();
141142
}
142143

143144
public void PublishFile(string bicepFilePath, string target, string documentationUri = null, bool withSource = false, bool force = false, OutputCallback writeVerbose = null, OutputCallback writeWarning = null)

src/Resources/ResourceManager/Utilities/TemplateUtility.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,12 @@ public static Dictionary<string, TemplateParameterFileParameter> ParseTemplatePa
221221
{
222222
try
223223
{
224-
parameters = JsonConvert.DeserializeObject<Dictionary<string, TemplateParameterFileParameter>>(templateParameterContent);
224+
parameters = templateParameterContent.FromJson<Dictionary<string, TemplateParameterFileParameter>>();
225225
}
226226
catch (JsonSerializationException)
227227
{
228228
parameters = new Dictionary<string, TemplateParameterFileParameter>(
229-
JsonConvert.DeserializeObject<TemplateParameterFile>(templateParameterContent).Parameters);
229+
templateParameterContent.FromJson<TemplateParameterFile>().Parameters);
230230
}
231231
}
232232

src/Resources/Resources.Test/ResourceGroupDeployments/NewAzureResourceGroupDeploymentCommandTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
1516
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation;
1617
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkClient;
1718
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels;
@@ -321,7 +322,7 @@ public void ResolvesDynamicParametersWithCrossSubTemplateSpec()
321322
: templateFile; // Windows based (already valid)
322323

323324
var templateContentForTest = File.ReadAllText(normalizedTemplateFilePath);
324-
var template = JsonConvert.DeserializeObject<TemplateFile>(templateContentForTest);
325+
var template = templateContentForTest.FromJson<TemplateFile>();
325326

326327
templateSpecsVersionOperationsMock.Setup(f => f.GetWithHttpMessagesAsync(
327328
templateSpecRGName,
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
param probes probeType
2+
3+
type probeType = {
4+
name: string
5+
host: string?
6+
}[]?
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using 'main.bicep'
2+
3+
param probes = [
4+
{
5+
name: 'healthprobe-default'
6+
}
7+
]

0 commit comments

Comments
 (0)