Skip to content

Commit

Permalink
Handle cases where ExpressionEvaluationDetails#targetValue is not a…
Browse files Browse the repository at this point in the history
…n array

For single values the API might return with a single value instead of an array with one element. In that case the following error is produced:
```
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList<java.lang.Object>` out of VALUE_STRING token
 at [Source: (String)"{"policyDefinitionDisplayName":"Allowed resource types","policySetDefinitionDisplayName":"webapp","evaluationDetails":{"evaluatedExpressions":[{"result":"False","expression":"type","path":"type","expressionValue":"Microsoft.ApiManagement/service","targetValue":"Microsoft.Resources/resourceGroups","operator":"In"}]},"policyDefinitionId":"/providers/Microsoft.Authorization/policyDefinitions/a08ec900-254a-4555-9bf5-e42af04b5c5c","policySetDefinitionId":"/providers/Microsoft.Management/managementGro"[truncated 692 chars]; line: 1, column: 262] (through reference chain: com.microsoft.azure.CloudError["additionalInfo"]->java.util.ArrayList[1]->com.microsoft.azure.PolicyViolationErrorInfo["evaluationDetails"]->com.microsoft.azure.EvaluationDetails["evaluatedExpressions"]->java.util.ArrayList[0]->com.microsoft.azure.ExpressionEvaluationDetails["targetValue"])

	at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
	at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1445)
	at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1219)
	at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1129)
	at com.fasterxml.jackson.databind.deser.std.StringCollectionDeserializer.handleNonArray(StringCollectionDeserializer.java:274)
	at com.fasterxml.jackson.databind.deser.std.StringCollectionDeserializer.deserialize(StringCollectionDeserializer.java:183)
	at com.fasterxml.jackson.databind.deser.std.StringCollectionDeserializer.deserialize(StringCollectionDeserializer.java:173)
	at com.fasterxml.jackson.databind.deser.std.StringCollectionDeserializer.deserialize(StringCollectionDeserializer.java:21)
	at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:138)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:288)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
	at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:286)
	at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:245)
	at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:27)
	at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:138)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:288)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
	at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:138)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:288)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4218)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3214)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3182)
	at com.microsoft.azure.PolicyViolation.<init>(PolicyViolation.java:41)
```
eg: `"targetValue":"Microsoft.Resources/subscriptions/resourceGroups"` is received instead of `"targetValue":["Microsoft.Resources/subscriptions/resourceGroups"]`

In this commit:
- `@JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)` is added to `ExpressionEvaluationDetails#targetValue`
- verified by adjusting the unit test, which would fail without the fix
  • Loading branch information
lacikaaa committed Feb 21, 2022
1 parent a869a9a commit fcd2f69
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import java.util.List;

import com.fasterxml.jackson.annotation.JsonFormat;

public class ExpressionEvaluationDetails {

private String expression;
Expand All @@ -20,6 +22,7 @@ public class ExpressionEvaluationDetails {

private String result;

@JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
private List<String> targetValue;

public String getExpression() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,43 +261,7 @@ public void testEvaluationDetails() throws IOException {
+ " \"expression\": \"type\",\n"
+ " \"path\": \"type\",\n"
+ " \"expressionValue\": \"Microsoft.ApiManagement/service\",\n"
+ " \"targetValue\": [\n"
+ " \"Microsoft.Resources/resourceGroups\",\n"
+ " \"Microsoft.Storage/storageAccounts\",\n"
+ " \"Microsoft.Web/sites\",\n"
+ " \"Microsoft.Web/serverFarms\",\n"
+ " \"Microsoft.Web/functions\",\n"
+ " \"Microsoft.DocumentDB/databaseAccounts\",\n"
+ " \"microsoft.insights/components\",\n"
+ " \"Microsoft.KeyVault/vaults\",\n"
+ " \"Microsoft.Cache/Redis\",\n"
+ " \"Microsoft.ServiceBus/namespaces\",\n"
+ " \"Microsoft.ServiceBus/namespaces/authorizationrules\",\n"
+ " \"Microsoft.ServiceBus/namespaces/queues\",\n"
+ " \"Microsoft.ServiceBus/namespaces/queues/authorizationrules\",\n"
+ " \"Microsoft.ServiceBus/namespaces/topics\",\n"
+ " \"Microsoft.ServiceBus/namespaces/topics/authorizationrules\",\n"
+ " \"Microsoft.ServiceBus/namespaces/topics/subscriptions\",\n"
+ " \"Microsoft.ServiceBus/namespaces/topics/subscriptions/rules\",\n"
+ " \"Microsoft.CognitiveServices/accounts\",\n"
+ " \"Microsoft.Web/sites/slots\",\n"
+ " \"Microsoft.Web/sites/slots/instances\",\n"
+ " \"Microsoft.Web/sites/slots/metrics\",\n"
+ " \"Microsoft.Web/sites/metrics\",\n"
+ " \"Microsoft.Web/sites/instances\",\n"
+ " \"Microsoft.Web/certificates\",\n"
+ " \"Microsoft.Portal/dashboards\",\n"
+ " \"Microsoft.ContainerRegistry/registries\",\n"
+ " \"Microsoft.ContainerRegistry/registries/webhooks\",\n"
+ " \"Microsoft.Web/connections\",\n"
+ " \"Microsoft.Logic/workflows\",\n"
+ " \"Microsoft.Web/customApis\",\n"
+ " \"Microsoft.Search/searchServices\",\n"
+ " \"Microsoft.Network/trafficmanagerprofiles\",\n"
+ " \"Microsoft.Sql/servers\",\n"
+ " \"Microsoft.Sql/servers/databases\",\n"
+ " \"Microsoft.SignalRService/SignalR\"\n"
+ " ],\n"
+ " \"targetValue\": \"Microsoft.Resources/resourceGroups\",\n"
+ " \"operator\": \"In\"\n"
+ " }\n"
+ " ]\n"
Expand Down

0 comments on commit fcd2f69

Please sign in to comment.