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

[BUG] Fails to pass some templates when evaluating parameters #361

Open
tonybaloney opened this issue May 3, 2024 · 4 comments · May be fixed by #362
Open

[BUG] Fails to pass some templates when evaluating parameters #361

tonybaloney opened this issue May 3, 2024 · 4 comments · May be fixed by #362
Labels
bug Something isn't working 🔍 needs-triage

Comments

@tonybaloney
Copy link

Describe the bug

I get the following when running this tool across many templates, using the parameters TemplateAnalyzer analyze-template infra/main.bicep --report-format Console

Error: An exception occurred while analyzing template /Users/anthonyshaw/projects/APICenter-Analyzer/infra/main.bicep
Exception details:
Microsoft.Azure.Templates.Analyzer.Core.TemplateAnalyzerException: Error while evaluating rules.
 ---> Microsoft.Azure.Templates.Analyzer.Core.TemplateAnalyzerException: Error while processing template.
 ---> Newtonsoft.Json.JsonSerializationException: Deserialized JSON type 'Newtonsoft.Json.Linq.JValue' is not compatible with expected type 'Newtonsoft.Json.Linq.JObject'. Path 'parameters.applicationInsightsName', line 1, position 3165.
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateJToken(JsonReader reader, JsonContract contract)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)
   at Newtonsoft.Json.Linq.JToken.ToObject(Type objectType, JsonSerializer jsonSerializer)
   at Newtonsoft.Json.Linq.JToken.ToObject(Type objectType)
   at Newtonsoft.Json.Linq.JToken.ToObject[T]()
   at Microsoft.Azure.Templates.Analyzer.TemplateProcessor.ArmTemplateProcessor.PopulateParameters(String parameters)
   at Microsoft.Azure.Templates.Analyzer.TemplateProcessor.ArmTemplateProcessor.ProcessTemplate(String parameters, String metadata)
   at Microsoft.Azure.Templates.Analyzer.TemplateProcessor.ArmTemplateProcessor.ProcessTemplate(String parameters)
   at Microsoft.Azure.Templates.Analyzer.Core.TemplateAnalyzer.AnalyzeAllIncludedTemplates(String populatedTemplate, String parameters, String templateFilePath, TemplateContext parentContext, String pathPrefix)
   --- End of inner exception stack trace ---
   at Microsoft.Azure.Templates.Analyzer.Core.TemplateAnalyzer.AnalyzeAllIncludedTemplates(String populatedTemplate, String parameters, String templateFilePath, TemplateContext parentContext, String pathPrefix)
   at Microsoft.Azure.Templates.Analyzer.Core.TemplateAnalyzer.AnalyzeAllIncludedTemplates(String populatedTemplate, String parameters, String templateFilePath, TemplateContext parentContext, String pathPrefix)
   --- End of inner exception stack trace ---
   at Microsoft.Azure.Templates.Analyzer.Core.TemplateAnalyzer.AnalyzeAllIncludedTemplates(String populatedTemplate, String parameters, String templateFilePath, TemplateContext parentContext, String pathPrefix)
   at Microsoft.Azure.Templates.Analyzer.Core.TemplateAnalyzer.AnalyzeTemplate(String template, String templateFilePath, String parameters)
   at Microsoft.Azure.Templates.Analyzer.Cli.CommandLineParser.AnalyzeTemplate(TemplateAndParams templateAndParameters)

Execution summary:

	Summary of the warnings:
		1 instance of: An exception occurred while evaluating the properties of the resource named functionapp
		1 instance of: An exception occurred while evaluating the properties of the resource named apicenteraccess
		1 instance of: An exception occurred while evaluating the properties of the resource named apicenteraccessexist
		1 instance of: The parsing of the template output named AZURE_FUNCTION_NAME failed
		1 instance of: The parsing of the template output named primaryEndpoints failed

	Summary of the errors:
		1 instance of: An exception occurred while analyzing template /Users/anthonyshaw/projects/APICenter-Analyzer/infra/main.bicep

	5 Warnings
	1 Error

It stops analysing the template at that stage.

I put a breakpoint in Visual Studio with this project in debug mode to better understand it, the parameters it was evaluating look like this:

{
	"name": "[if(not(empty(parameters('appServicePlanName'))), createObject('value', parameters('appServicePlanName')), createObject('value', format('{0}{1}', variables('abbrs').webServerFarms, variables('resourceToken'))))]",
	"location": {
		"value": "[parameters('location')]"
	},
	"tags": {
		"value": "[variables('tags')]"
	},
	"sku": {
		"value": {
			"name": "Y1"
		}
	},
	"kind": {
		"value": "linux"
	},
	"parameters": {
		"name": "[if(not(empty(parameters('appServicePlanName'))), createObject('value', parameters('appServicePlanName')), createObject('value', format('{0}{1}', variables('abbrs').webServerFarms, variables('resourceToken'))))]",
		"location": {
			"value": "[parameters('location')]"
		},
		"tags": {
			"value": "[variables('tags')]"
		},
		"sku": {
			"value": {
				"name": "Y1"
			}
		},
		"kind": {
			"value": "linux"
		}
	},
	"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
	"contentVersion": "1.0.0.0"
}

It crashes because this line https://github.com/Azure/template-analyzer/blob/main/src/Analyzer.TemplateProcessor/ArmTemplateProcessor.cs#L549 assumes that the parameter is an object, but the name parameter is a string with an expression inside it.

Expected behavior

see detail

Reproduction Steps

see detail

Environment

No response

@tonybaloney tonybaloney added bug Something isn't working 🔍 needs-triage labels May 3, 2024
@tonybaloney
Copy link
Author

I have a fix for this, will submit a PR

@tonybaloney tonybaloney linked a pull request May 3, 2024 that will close this issue
@JohnathonMohr
Copy link
Contributor

Hi @tonybaloney. I'm a bit confused at the example parameters you've provided that produce the error. That JSON does not match the schema for parameters files. Can you explain your scenario a bit more?

@tonybaloney
Copy link
Author

I cloned this repo and ran the tool against this bicep file https://github.com/Azure/APICenter-Analyzer/blob/preview/infra/main.bicep

TemplateAnalyzer analyze-template infra/main.bicep --report-format Console

Giving the stack trace shown above. The parameter file in this repo is valid.

The JSON I gave in the issue was captured by putting a breakpoint in this project and catching the exception then looking at the JSON it was trying to deserialize.

@JohnathonMohr
Copy link
Contributor

Thanks @tonybaloney for the additional clarification. Properly resolving this will require a bit more investigation into how the parameters end up in this format during execution, so we can fully understand how to process it correctly. Since the scanned template includes Bicep transformations and potentially multiple modules, it requires more careful consideration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working 🔍 needs-triage
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants