Skip to content

Commit

Permalink
[.NET] Add warning for max actions exceeded (microsoft#2919)
Browse files Browse the repository at this point in the history
* Add warning for max actions exceeded

* Fix HTML renderer
  • Loading branch information
almedina-ms committed May 15, 2019
1 parent ee55bc3 commit ab3f96a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,13 @@ protected static void AddActions(HtmlTag uiContainer, IList<AdaptiveAction> acti
}
}

// If the number of actions is bigger than maxActions, then log warning for it
if (actions.Count > actionsConfig.MaxActions)
{
context.Warnings.Add(new AdaptiveWarning((int)AdaptiveWarning.WarningStatusCode.MaxActionsExceeded,
"Some actions were not rendered due to exceeding the maximum number of actions allowed"));
}

var maxActions = Math.Min(actionsConfig.MaxActions, actions.Count);
// See if all actions have icons, otherwise force the icon placement to the left
var oldConfigIconPlacement = actionsConfig.IconPlacement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static void AddActions(Grid uiContainer, IList<AdaptiveAction> actions, A

var actionsConfig = context.Config.Actions;
var maxActions = actionsConfig.MaxActions;
var actionsToProcess = GetActionsToProcess(actions, maxActions);
var actionsToProcess = GetActionsToProcess(actions, maxActions, context);

if (actionsToProcess.Any())
{
Expand Down Expand Up @@ -138,8 +138,14 @@ public static void AddActions(Grid uiContainer, IList<AdaptiveAction> actions, A
}
}

private static List<AdaptiveAction> GetActionsToProcess(IList<AdaptiveAction> actions, int maxActions)
private static List<AdaptiveAction> GetActionsToProcess(IList<AdaptiveAction> actions, int maxActions, AdaptiveRenderContext context)
{
// If the number of actions is bigger than maxActions, then log warning for it
if (actions.Count > maxActions)
{
context.Warnings.Add(new AdaptiveWarning((int)AdaptiveWarning.WarningStatusCode.MaxActionsExceeded, "Some actions were not rendered due to exceeding the maximum number of actions allowed"));
}

// only consider known actions for ActionsToProcess
var actionsToProcess = actions.Where(obj => obj.GetType() != typeof(AdaptiveUnknownAction)).ToList();
return actionsToProcess.Take(maxActions).ToList();
Expand Down
9 changes: 3 additions & 6 deletions source/dotnet/Library/AdaptiveCards/AdaptiveCardConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ public class AdaptiveCardConverter : JsonConverter, ILogWarnings
{
public List<AdaptiveWarning> Warnings { get; set; } = new List<AdaptiveWarning>();

// TODO #2749: temporary warning code for fallback card. Remove when common set of error codes created and integrated.
private enum WarningStatusCode { UnsupportedSchemaVersion = 7, InvalidLanguage = 12 };

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
Expand Down Expand Up @@ -73,12 +70,12 @@ private string ValidateLang(string val)
}
else
{
Warnings.Add(new AdaptiveWarning((int)WarningStatusCode.InvalidLanguage, "Invalid language identifier: " + val));
Warnings.Add(new AdaptiveWarning((int)AdaptiveWarning.WarningStatusCode.InvalidLanguage, "Invalid language identifier: " + val));
}
}
catch (CultureNotFoundException)
{
Warnings.Add(new AdaptiveWarning((int)WarningStatusCode.InvalidLanguage, "Invalid language identifier: " + val));
Warnings.Add(new AdaptiveWarning((int)AdaptiveWarning.WarningStatusCode.InvalidLanguage, "Invalid language identifier: " + val));
}
}
return val;
Expand Down Expand Up @@ -122,7 +119,7 @@ private AdaptiveCard MakeFallbackTextCard(JObject jObject)
});

// Add relevant warning
Warnings.Add(new AdaptiveWarning((int) WarningStatusCode.UnsupportedSchemaVersion, "Schema version is not supported"));
Warnings.Add(new AdaptiveWarning((int)AdaptiveWarning.WarningStatusCode.UnsupportedSchemaVersion, "Schema version is not supported"));

return fallbackCard;
}
Expand Down
3 changes: 3 additions & 0 deletions source/dotnet/Library/AdaptiveCards/AdaptiveWarning.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ namespace AdaptiveCards
// TODO: decide if standard error codes are useful or if this should just be string
public class AdaptiveWarning
{
// TODO #2749: temporary warning code for fallback card. Remove when common set of error codes created and integrated.
public enum WarningStatusCode { UnsupportedSchemaVersion = 7, InvalidLanguage = 12, MaxActionsExceeded = 13 };

public AdaptiveWarning(int code, string message)
{
Code = code;
Expand Down

0 comments on commit ab3f96a

Please sign in to comment.