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

Amazon.CDK.AWS.CloudWatch: MathExpression throws Newtonsoft.Json.JsonSerializationException: #3882

Closed
haassto opened this issue Nov 7, 2022 · 3 comments
Assignees
Labels
bug This issue is a bug. effort/medium Medium work item – a couple days of effort jsii p1

Comments

@haassto
Copy link

haassto commented Nov 7, 2022

Describe the bug

I'm not able to create a MathExpression, instead 'cdk diff' throws an exception.

My code looks something like this:

        Queue[] queues;

        var props = new MathExpressionProps {
            Expression = "SUM(METRICS())",
            UsingMetrics = new Dictionary<string, IMetric>
            {
                {"1", queues[0].MetricApproximateNumberOfMessagesVisible()},
                {"2", queues[1].MetricApproximateNumberOfMessagesVisible()},
            }
        };
        var combinedMetrics = new MathExpression(props);

I'm not able to find some problem in my code. I also used a different expression, but the result is the same (analog to Docu [1])

[1] https://docs.aws.amazon.com/cdk/api/v2/dotnet/api/Amazon.CDK.AWS.CloudWatch.MathExpression.html#Amazon_CDK_AWS_CloudWatch_MathExpression_Expression

Expected Behavior

No exception

Current Behavior

This is the exception I get

Unhandled exception. Newtonsoft.Json.JsonSerializationException: Error converting value "Error" to type 'Amazon.JSII.JsonModel.Api.Response.ErrorResponseName'. Path 'name'. ---> System.ArgumentException: Requested value 'Error' was not found. at Newtonsoft.Json.Utilities.EnumUtils.ParseEnum(Type enumType, NamingStrategy namingStrategy, String value, Boolean disallowNumber) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType) --- End of inner exception stack trace --- at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMemb er, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ResolvePropertyAndCreatorValues(JsonObjectContract contract, JsonProperty containerProperty, JsonReader reader, Type objectType) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObjectUsingCreatorWithParameters(JsonReader reader, JsonObjectContract contract, JsonProperty containerProperty, ObjectConstructor1 creator, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Obj
ect existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMemb
er, 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.Linq.JToken.ToObject(Type objectType, JsonSerializer jsonSerializer)
at Newtonsoft.Json.Linq.JToken.ToObject(Type objectType)
at Newtonsoft.Json.Linq.JToken.ToObjectT
at Amazon.JSII.Runtime.Services.Client.TryDeserialize[TResponse](String responseJson)
at Amazon.JSII.Runtime.Services.Client.ReceiveResponseTResponse
at Amazon.JSII.Runtime.Services.Client.Send[TRequest,TResponse](TRequest requestObject)
at Amazon.JSII.Runtime.Services.Client.Create(CreateRequest request)
at Amazon.JSII.Runtime.Services.Client.Create(String fullyQualifiedName, Object[] arguments, Override[] overrides, String[] interfaces)
at Amazon.JSII.Runtime.Deputy.DeputyBase..ctor(DeputyProps props)
at Amazon.CDK.AWS.CloudWatch.MathExpression..ctor(IMathExpressionProps props)
`

Reproduction Steps

        Queue[] queues;

        var props = new MathExpressionProps {
            Expression = "SUM(METRICS())",
            UsingMetrics = new Dictionary<string, IMetric>
            {
                {"1", queues[0].MetricApproximateNumberOfMessagesVisible()},
                {"2", queues[1].MetricApproximateNumberOfMessagesVisible()},
            }
        };
        var combinedMetrics = new MathExpression(props);

Possible Solution

No response

Additional Information/Context

I changed to Version 2.50 and 2.41 with the same result.

CDK CLI Version

2.46.0

Framework Version

No response

Node.js Version

v18.12.0

OS

Windows 10/Linux (both)

Language

.NET

Language Version

dotnet 6

Other information

No response

@haassto haassto added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Nov 7, 2022
@peterwoodworth
Copy link
Contributor

Thanks for reporting this issue, I was able to reproduce the same issue in .NET on the latest version of CDK. Here's my full stack code:

using Amazon.CDK;
using Constructs;
using Amazon.CDK.AWS.CloudWatch;
using Amazon.CDK.AWS.SQS;

namespace Dotnet
{
    public class DotnetStack : Stack
    {
        internal DotnetStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props)
        {
            var queue1 = new Queue(this, "Queue1");
            var queue2 = new Queue(this, "Queue2");
            var sprops = new MathExpressionProps {
                Expression = "SUM(METRICS())",
                UsingMetrics = new System.Collections.Generic.Dictionary<string, IMetric>
                {
                    {"1", queue1.MetricApproximateNumberOfMessagesVisible()},
                    {"2", queue2.MetricApproximateNumberOfMessagesVisible()},
                }
            };
            var combinedMetrics = new MathExpression(sprops);
            }
    }
}

I was not able to reproduce the same issue in TypeScript, which indicates to me that this is a JSII issue when translating between .NET and JavaScript. So, I'm going to move this issue to the JSII repo. Let's see what they say 🙂

@peterwoodworth peterwoodworth added p2 and removed needs-triage This issue or PR still needs to be triaged. labels Dec 13, 2022
@peterwoodworth peterwoodworth transferred this issue from aws/aws-cdk Dec 13, 2022
@pahud pahud added jsii effort/medium Medium work item – a couple days of effort labels Mar 12, 2024
@rix0rrr rix0rrr added p1 and removed p2 labels Apr 10, 2024
@kaizencc
Copy link
Contributor

kaizencc commented Jun 4, 2024

I am able to reproduce this in CDK v2.46.0. The error message is very opaque. However in later versions (at least in CDK v2.78.0+) we produce a different error that is more illuminating:

Unhandled exception. System.Exception: Invalid variable names in expression: 1,2. Must start with lowercase letter and only contain alphanumerics.

The problem here is that you can't name your variables 1 and 2. They must start with a-z. I can confirm that after updating to get your variables in line with the requirements the code deploys successfully.

Hope this helps, and sorry about the opaque error message!

@kaizencc kaizencc closed this as completed Jun 4, 2024
Copy link
Contributor

github-actions bot commented Jun 4, 2024

This issue is now closed. Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. effort/medium Medium work item – a couple days of effort jsii p1
Projects
None yet
Development

No branches or pull requests

6 participants