pulumi / pulumi-dotnet

.NET support for Pulumi
Apache License 2.0
28 stars 25 forks source link

The JSON value could not be converted to System.String #100

Open lee-butn opened 1 year ago

lee-butn commented 1 year ago

What happened?

Try to read an integer number from pulumi config and getting follow error:

at object RuntimeMethodHandle.InvokeMethod(object target, in Span<object> arguments, Signature sig, bool constructor, bool wrapExceptions)
08:06:43   Info     |       at object System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture)
08:06:43   Info     |       at object Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
08:06:43   Info     |       at TResult Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor<TArgument, TResult>.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
08:06:43   Info     |       at object Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context)
08:06:43   Info     |       at TResult Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor<TArgument, TResult>.VisitCallSite(ServiceCallSite callSite, TArgument argument)
08:06:43   Info     |       at object Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
08:06:43   Info     |       at Func<ServiceProviderEngineScope, object> Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(Type serviceType)
08:06:43   Info     |       at TValue System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue>.GetOrAdd(TKey key, Func<TKey, TValue> valueFactory)
08:06:43   Info     |       at object Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope) x 2
08:06:43   Info     |       at T Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService<T>(IServiceProvider provider)
08:06:43   Info     |       at new ButnUp.Platform.Ram.Lambda.Infra.MainStack() in /_/src/ButnUp.Platform.Ram.Lambda.Infra/MainStack.cs:line 24
08:06:43   Info     |       at object RuntimeType.CreateInstanceOfT() ---> System.Text.Json.JsonException: The JSON value could not be converted to System.String. Path: $.PlainVariables[9].Value | LineNumber: 0 | BytePositionInLine: 773.
08:06:43   Info     |       at void System.Text.Json.ThrowHelper.ReThrowWithPath(ref ReadStack state, in Utf8JsonReader reader, Exception ex)
08:06:43   Info     |       at T System.Text.Json.Serialization.JsonConverter<T>.ReadCore(ref Utf8JsonReader reader, JsonSerializerOptions options, ref ReadStack state)
08:06:43   Info     |       at TValue System.Text.Json.JsonSerializer.ReadFromSpan<TValue>(ReadOnlySpan<byte> utf8Json, JsonTypeInfo jsonTypeInfo, int? actualByteCount) x 2
08:06:43   Info     |       at TValue System.Text.Json.JsonSerializer.Deserialize<TValue>(string json, JsonSerializerOptions options)
08:06:43   Info     |       at T Pulumi.Config.GetObjectImpl<T>(string key, string use, string insteadOf) ---> System.InvalidOperationException: Cannot get the value of a token type 'Number' as a string.
08:06:43   Info     |       at void System.Text.Json.ThrowHelper.ThrowInvalidOperationException_ExpectedString(JsonTokenType tokenType)
08:06:43   Info     |       at string System.Text.Json.Utf8JsonReader.GetString()
08:06:43   Info     |       at string System.Text.Json.Serialization.Converters.StringConverter.Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
08:06:43   Info     |       at bool System.Text.Json.Serialization.Metadata.JsonPropertyInfo<T>.ReadJsonAndSetMember(object obj, ref ReadStack state, ref Utf8JsonReader reader)
08:06:43   Info     |       at bool System.Text.Json.Serialization.Converters.ObjectDefaultConverter<T>.OnTryRead(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options, ref ReadStack state, out T value)
08:06:43   Info     |       at bool System.Text.Json.Serialization.JsonConverter<T>.TryRead(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options, ref ReadStack state, out T value)
08:06:43   Info     |       at bool System.Text.Json.Serialization.JsonCollectionConverter<TCollection, TElement>.OnTryRead(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options, ref ReadStack state, out TCollection value)
08:06:43   Info     |       at bool System.Text.Json.Serialization.JsonConverter<T>.TryRead(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options, ref ReadStack state, out T value)
08:06:43   Info     |       at bool System.Text.Json.Serialization.Metadata.JsonPropertyInfo<T>.ReadJsonAndSetMember(object obj, ref ReadStack state, ref Utf8JsonReader reader)
08:06:43   Info     |       at bool System.Text.Json.Serialization.Converters.ObjectDefaultConverter<T>.OnTryRead(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options, ref ReadStack state, out T value)
08:06:43   Info     |       at bool System.Text.Json.Serialization.JsonConverter<T>.TryRead(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options, ref ReadStack state, out T value)
08:06:43   Info     |       at T System.Text.Json.Serialization.JsonConverter<T>.ReadCore(ref Utf8JsonReader reader, JsonSerializerOptions options, ref ReadStack state)

The Solution is use Newtonsoft.Json instead System.Text.Json in https://github.com/pulumi/pulumi-dotnet/blob/main/sdk/Pulumi/Config.cs

Expected Behavior

Read an integer from config

Steps to reproduce

Give config with integer value

Output of pulumi about

CLI
Version      3.53.1
Go Version   go1.19.5
Go Compiler  gc

Host     
OS       Microsoft Windows 10 Enterprise
Version  10.0.19044 Build 19044
Arch     x86_64

Backend
Name           pulumi.com
URL            https://app.pulumi.com/lee-butn
User           lee-butn
Organizations  lee-butn, butn

Additional context

No response

Contributing

Vote on this issue by adding a 👍 reaction. To contribute a fix for this issue, leave a comment (and link to your pull request, if you've opened one already).

RobbieMcKinstry commented 1 year ago

Hi Lee, could you provide a concrete program that demonstrates this issue? It would help me rule out user error and quickly get to the bottom of the issue. You may be reading the number into a string value. For example, are you calling Config.Get or Config.GetInt32? 🙏🏻 Thank you very much! I appreciate you bringing this issue to our attention!