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

CadenceTypeConverter fix for decoding string value types #106

Merged
merged 1 commit into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 10 additions & 18 deletions src/Flow.Net.Sdk.Core/Cadence/Types/CadenceTypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,19 @@ public class CadenceTypeConverter : CustomCreationConverter<ICadenceType>

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
try
if ((reader.Path.Contains("type") || reader.Path == "value.staticType" || reader.Path == "value") && reader.TokenType == JsonToken.String)
{
if ((reader.Path.Contains("type") || reader.Path == "value.staticType") && reader.TokenType == JsonToken.String)
{
if (_compositeDictionary.TryGetValue((string)reader.Value, out var composite))
return composite;
if (_compositeDictionary.TryGetValue((string)reader.Value, out var composite))
return composite;

return new CadenceTypeValueAsString(reader.Value.ToString());
}
return new CadenceTypeValueAsString(reader.Value.ToString());
}

var jObject = JObject.Load(reader);
var target = Create(jObject);
HandleCadenceCompositeType(jObject, target);
serializer.Populate(jObject.CreateReader(), target);
return target;
}
catch(Exception ex)
{
var t = ex;
throw new Exception("", ex);
}
var jObject = JObject.Load(reader);
var target = Create(jObject);
HandleCadenceCompositeType(jObject, target);
serializer.Populate(jObject.CreateReader(), target);
return target;
}

private static ICadenceType Create(JObject jObject)
Expand Down
34 changes: 34 additions & 0 deletions tests/Flow.Net.SDK.Tests/CadenceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text.Encodings.Web;
using Xunit;

namespace Flow.Net.Sdk.Tests
Expand Down Expand Up @@ -1304,6 +1305,39 @@ public void TestCadenceTypeValueComplex()
TestEncodeAndDecode(testItems);
}

[Fact]
public void TestCadenceTypeValueAsString()
{
var barType = new Core.Cadence.Types.CadenceCompositeType(CadenceCompositeTypeKind.Resource)
{
TypeId = "A.2d4c3caffbeab845.FLOAT.FLOATEvent",
Fields = new List<CadenceFieldType>
{
new CadenceFieldType
{
Id = "currentHolders",
Type = new CadenceDictionaryType
{
Key = new CadenceType
{
Kind = "Int"
},
Value = new CadenceTypeValueAsString
{
Value = "A.2d4c3caffbeab845.FLOAT.TokenIdentifier"
}
}
}
}
};

var expectedJson = "{\"kind\":\"Resource\",\"type\":\"\",\"typeID\":\"A.2d4c3caffbeab845.FLOAT.FLOATEvent\",\"initializers\":[],\"fields\":[{\"id\":\"currentHolders\",\"type\":{\"kind\":\"Dictionary\",\"key\":{\"kind\":\"Int\"},\"value\":\"A.2d4c3caffbeab845.FLOAT.TokenIdentifier\"}}]}";

TestEncode(barType, expectedJson);
TestDecode(expectedJson, barType);
}


private void TestEncodeAndDecode(IEnumerable<CadenceTestItem> testItems)
{
foreach (var testItem in testItems)
Expand Down